Skip to content

Commit

Permalink
fix: Parse binding component AST to markdown (#68)
Browse files Browse the repository at this point in the history

Co-authored-by: Farnabaz <[email protected]>
  • Loading branch information
ManUtopiK and farnabaz authored Nov 28, 2023
1 parent bd3f4c4 commit e776b04
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/to-markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -73,9 +73,9 @@ 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)
Expand Down
130 changes: 130 additions & 0 deletions test/__snapshots__/inline-component.test.ts.snap
Original file line number Diff line number Diff line change
@@ -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": [
Expand Down
6 changes: 6 additions & 0 deletions test/inline-component.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ describe('inline-component', () => {
},
underlined: {
markdown: '**:component[text]{.class}**'
},
binding: {
markdown: '{{ $doc.variable }}'
},
bindingWithDefault: {
markdown: "{{ $doc.variable || 'mdc' }}"
}
})
})

0 comments on commit e776b04

Please sign in to comment.