From 1f97000ddda9fb74f5f9f304a8fa67480995805e Mon Sep 17 00:00:00 2001 From: ManUtopiK Date: Fri, 17 Nov 2023 01:25:29 +0100 Subject: [PATCH 1/2] Parse binding component AST to markdown --- src/to-markdown.ts | 7 +- .../inline-component.test.ts.snap | 130 ++++++++++++++++++ test/inline-component.test.ts | 6 + 3 files changed, 142 insertions(+), 1 deletion(-) diff --git a/src/to-markdown.ts b/src/to-markdown.ts index 743f687..1b3a0d1 100644 --- a/src/to-markdown.ts +++ b/src/to-markdown.ts @@ -61,7 +61,7 @@ export default (opts: RemarkMDCOptions = {}) => { return `#${(node as any).name}\n${content(node, context)}`.trim() } - type NodeTextComponent = Parents & { name: string; rawData: string } + type NodeTextComponent = Parents & { name: string; rawData: string; attributes: any } function textComponent (node: NodeTextComponent, _: any, context: any) { let value context.indexStack = context.stack @@ -75,6 +75,11 @@ export default (opts: RemarkMDCOptions = {}) => { value = ':' + (node.name || '') + label(node, context) + attributes(node, context) } + if (node.name === 'binding') { + const defaultValue = node.attributes.defaultValue ? `|| '${node.attributes.defaultValue}' ` : '' + value = `{{ ${node.attributes.value} ${defaultValue}}}` + } + exit() return value } diff --git a/test/__snapshots__/inline-component.test.ts.snap b/test/__snapshots__/inline-component.test.ts.snap index c4bcf46..cf61812 100644 --- a/test/__snapshots__/inline-component.test.ts.snap +++ b/test/__snapshots__/inline-component.test.ts.snap @@ -1,5 +1,135 @@ // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html +exports[`inline-component > binding 1`] = ` +{ + "children": [ + { + "children": [ + { + "attributes": { + "defaultValue": undefined, + "value": "$doc.variable", + }, + "data": { + "hName": "binding", + "hProperties": { + ":defaultValue": undefined, + "value": "$doc.variable", + }, + }, + "fmAttributes": {}, + "name": "binding", + "position": { + "end": { + "column": 18, + "line": 1, + "offset": 17, + }, + "start": { + "column": 3, + "line": 1, + "offset": 2, + }, + }, + "type": "textComponent", + }, + ], + "position": { + "end": { + "column": 20, + "line": 1, + "offset": 19, + }, + "start": { + "column": 1, + "line": 1, + "offset": 0, + }, + }, + "type": "paragraph", + }, + ], + "position": { + "end": { + "column": 20, + "line": 1, + "offset": 19, + }, + "start": { + "column": 1, + "line": 1, + "offset": 0, + }, + }, + "type": "root", +} +`; + +exports[`inline-component > bindingWithDefault 1`] = ` +{ + "children": [ + { + "children": [ + { + "attributes": { + "defaultValue": "mdc", + "value": "$doc.variable", + }, + "data": { + "hName": "binding", + "hProperties": { + "defaultValue": "mdc", + "value": "$doc.variable", + }, + }, + "fmAttributes": {}, + "name": "binding", + "position": { + "end": { + "column": 27, + "line": 1, + "offset": 26, + }, + "start": { + "column": 3, + "line": 1, + "offset": 2, + }, + }, + "type": "textComponent", + }, + ], + "position": { + "end": { + "column": 29, + "line": 1, + "offset": 28, + }, + "start": { + "column": 1, + "line": 1, + "offset": 0, + }, + }, + "type": "paragraph", + }, + ], + "position": { + "end": { + "column": 29, + "line": 1, + "offset": 28, + }, + "start": { + "column": 1, + "line": 1, + "offset": 0, + }, + }, + "type": "root", +} +`; + exports[`inline-component > empty 1`] = ` { "children": [ diff --git a/test/inline-component.test.ts b/test/inline-component.test.ts index 880e249..26ca026 100644 --- a/test/inline-component.test.ts +++ b/test/inline-component.test.ts @@ -21,6 +21,12 @@ describe('inline-component', () => { }, underlined: { markdown: '**:component[text]{.class}**' + }, + binding: { + markdown: '{{ $doc.variable }}' + }, + bindingWithDefault: { + markdown: "{{ $doc.variable || 'mdc' }}" } }) }) From 84d6dede8a4fcfeec637dd97595f268161b91c04 Mon Sep 17 00:00:00 2001 From: Farnabaz Date: Tue, 28 Nov 2023 11:39:32 +0100 Subject: [PATCH 2/2] up --- src/to-markdown.ts | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/to-markdown.ts b/src/to-markdown.ts index 6dc02a5..02d46e2 100644 --- a/src/to-markdown.ts +++ b/src/to-markdown.ts @@ -73,19 +73,14 @@ export default (opts: RemarkMDCOptions = {}) => { value = `[${content(node, context)}]${attributes(node, context)}` } else if (node.name === 'binding') { // Handle binding syntax - const attrs = (node as any).attributes || {} + const attrs = node.attributes || {} value = attrs.defaultValue - ? `{{ ${attrs.value} || ${JSON.stringify(attrs.defaultValue)} }}` + ? `{{ ${attrs.value} || '${attrs.defaultValue}' }}` : `{{ ${attrs.value} }}` } else { value = ':' + (node.name || '') + label(node, context) + attributes(node, context) } - if (node.name === 'binding') { - const defaultValue = node.attributes.defaultValue ? `|| '${node.attributes.defaultValue}' ` : '' - value = `{{ ${node.attributes.value} ${defaultValue}}}` - } - exit() return value }