Skip to content

Commit

Permalink
fix: remove scaped char \ from attributes value (#89)
Browse files Browse the repository at this point in the history
  • Loading branch information
farnabaz authored Nov 15, 2024
1 parent 58ca74a commit 74cee31
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/from-markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,9 @@ export default (opts: RemarkMDCOptions = {}) => {

function exitAttributeValue (this: CompileContext, token: Token) {
const attributes = (this.data as any).componentAttributes
attributes[attributes.length - 1][1] = parseEntities(this.sliceSerialize(token))
const lastAttribute = attributes[attributes.length - 1]

lastAttribute[1] = (typeof lastAttribute[1] === 'string' ? lastAttribute[1] : '') + parseEntities(this.sliceSerialize(token))
}

function exitAttributeName (this: CompileContext, token: Token) {
Expand Down
17 changes: 17 additions & 0 deletions src/micromark-extension/factory-attributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,17 @@ export default function createAttributes (
}

function valueQuoted (code: number) {
if (code === Codes.backSlash) {
effects.exit(attributeValueData)
effects.exit(attributeValueType)
effects.enter('escapeCharacter')
effects.consume(code)
effects.exit('escapeCharacter')
effects.enter(attributeValueType)
effects.enter(attributeValueData)
return valueQuotedEscape
}

if (code === marker || code === Codes.EOF || markdownLineEnding(code)) {
effects.exit(attributeValueData)
return valueQuotedBetween(code)
Expand All @@ -305,6 +316,12 @@ export default function createAttributes (
return valueQuoted
}

function valueQuotedEscape (code: number) {
effects.consume(code)

return valueQuoted
}

function valueQuotedAfter (code: number) {
return code === Codes.closingCurlyBracket || markdownLineEndingOrSpace(code) ? between(code) : end(code)
}
Expand Down
2 changes: 2 additions & 0 deletions src/micromark-extension/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ declare module 'micromark-util-types' {
bindingContent: 'bindingContent',
bindingFence: 'bindingFence',

escapeCharacter: 'escapeCharacter',

// Component Text
componentText: 'componentText',
componentTextMarker: 'componentTextMarker',
Expand Down
2 changes: 1 addition & 1 deletion src/to-markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ export default (opts: RemarkMDCOptions = {}) => {
} else if (key.startsWith(':') && value === 'true') {
values.push(key.slice(1))
} else if (key.startsWith(':') && isValidJSON(value)) {
values.push(`${key}='${value}'`)
values.push(`${key}='${value.replace(/([^/])'/g, '$1\\\'')}'`)
} else {
values.push(quoted(key, value))
}
Expand Down
47 changes: 47 additions & 0 deletions test/__snapshots__/block-component.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1057,6 +1057,53 @@ exports[`block-component > ignore-code-fence 1`] = `
}
`;

exports[`block-component > jsonScapeAttr 1`] = `
{
"children": [
{
"attributes": {
":test": "{"foo":"I'd love to"}",
},
"children": [],
"data": {
"hName": "foo",
"hProperties": {
":test": "{"foo":"I'd love to"}",
},
},
"fmAttributes": {},
"name": "foo",
"position": {
"end": {
"column": 3,
"line": 2,
"offset": 40,
},
"start": {
"column": 1,
"line": 1,
"offset": 0,
},
},
"type": "containerComponent",
},
],
"position": {
"end": {
"column": 3,
"line": 2,
"offset": 40,
},
"start": {
"column": 1,
"line": 1,
"offset": 0,
},
},
"type": "root",
}
`;

exports[`block-component > nested-component 1`] = `
{
"children": [
Expand Down
6 changes: 6 additions & 0 deletions test/block-component.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ describe('block-component', () => {
markdown: '::with-frontmatter\n---\nkey: value\narray:\n - item\n - itemKey: value\n---\n::',
expected: '::with-frontmatter\n---\narray:\n - item\n - itemKey: value\nkey: value\n---\n::'
},
jsonScapeAttr: {
markdown: '::foo{:test=\'{"foo":"I\\\'d love to"}\'}\n::',
extra: (_md, ast) => {
expect(ast.children[0].type).toBe('containerComponent')
}
},
frontmatter1: {
markdown: [
'::with-frontmatter',
Expand Down

0 comments on commit 74cee31

Please sign in to comment.