Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: remove scaped char \ from attributes value #89

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/from-markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,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 @@ -72,6 +72,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 @@ -219,7 +219,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
Loading