From 65def9b3045d598e202bf904e23968be96dc34fb Mon Sep 17 00:00:00 2001 From: Farnabaz Date: Fri, 8 Mar 2024 10:35:49 +0100 Subject: [PATCH] fix(attributes): handle attribute of elements in tableCell --- src/from-markdown.ts | 4 + test/__snapshots__/attributes.test.ts.snap | 229 +++++++++++++++++++++ test/attributes.test.ts | 12 ++ 3 files changed, 245 insertions(+) diff --git a/src/from-markdown.ts b/src/from-markdown.ts index 8162125a..0d96ba2e 100644 --- a/src/from-markdown.ts +++ b/src/from-markdown.ts @@ -269,6 +269,10 @@ export default (opts: RemarkMDCOptions = {}) => { stackTop = stackTop.children[stackTop.children.length - 1] } + if (stackTop.type === 'tableCell') { + stackTop = stackTop.children[stackTop.children.length - 1] + } + (stackTop as any).attributes = cleaned } diff --git a/test/__snapshots__/attributes.test.ts.snap b/test/__snapshots__/attributes.test.ts.snap index 804db3d0..93a1377c 100644 --- a/test/__snapshots__/attributes.test.ts.snap +++ b/test/__snapshots__/attributes.test.ts.snap @@ -824,6 +824,235 @@ exports[`Attributes > link 1`] = ` } `; +exports[`Attributes > nested-in-table 1`] = ` +{ + "children": [ + { + "align": [ + null, + null, + ], + "children": [ + { + "children": [ + { + "children": [ + { + "position": { + "end": { + "column": 7, + "line": 1, + "offset": 6, + }, + "start": { + "column": 3, + "line": 1, + "offset": 2, + }, + }, + "type": "text", + "value": "Col1", + }, + ], + "position": { + "end": { + "column": 8, + "line": 1, + "offset": 7, + }, + "start": { + "column": 1, + "line": 1, + "offset": 0, + }, + }, + "type": "tableCell", + }, + { + "children": [ + { + "position": { + "end": { + "column": 19, + "line": 1, + "offset": 18, + }, + "start": { + "column": 15, + "line": 1, + "offset": 14, + }, + }, + "type": "text", + "value": "Col2", + }, + ], + "position": { + "end": { + "column": 26, + "line": 1, + "offset": 25, + }, + "start": { + "column": 8, + "line": 1, + "offset": 7, + }, + }, + "type": "tableCell", + }, + ], + "position": { + "end": { + "column": 26, + "line": 1, + "offset": 25, + }, + "start": { + "column": 1, + "line": 1, + "offset": 0, + }, + }, + "type": "tableRow", + }, + { + "children": [ + { + "children": [ + { + "position": { + "end": { + "column": 6, + "line": 3, + "offset": 57, + }, + "start": { + "column": 4, + "line": 3, + "offset": 55, + }, + }, + "type": "text", + "value": "aa", + }, + ], + "position": { + "end": { + "column": 8, + "line": 3, + "offset": 59, + }, + "start": { + "column": 1, + "line": 3, + "offset": 52, + }, + }, + "type": "tableCell", + }, + { + "children": [ + { + "attributes": { + "a": "a", + }, + "children": [ + { + "position": { + "end": { + "column": 12, + "line": 3, + "offset": 63, + }, + "start": { + "column": 11, + "line": 3, + "offset": 62, + }, + }, + "type": "text", + "value": "a", + }, + ], + "position": { + "end": { + "column": 17, + "line": 3, + "offset": 68, + }, + "start": { + "column": 10, + "line": 3, + "offset": 61, + }, + }, + "title": null, + "type": "link", + "url": "/a", + }, + ], + "position": { + "end": { + "column": 26, + "line": 3, + "offset": 77, + }, + "start": { + "column": 8, + "line": 3, + "offset": 59, + }, + }, + "type": "tableCell", + }, + ], + "position": { + "end": { + "column": 26, + "line": 3, + "offset": 77, + }, + "start": { + "column": 1, + "line": 3, + "offset": 52, + }, + }, + "type": "tableRow", + }, + ], + "position": { + "end": { + "column": 26, + "line": 3, + "offset": 77, + }, + "start": { + "column": 1, + "line": 1, + "offset": 0, + }, + }, + "type": "table", + }, + ], + "position": { + "end": { + "column": 26, + "line": 3, + "offset": 77, + }, + "start": { + "column": 1, + "line": 1, + "offset": 0, + }, + }, + "type": "root", +} +`; + exports[`Attributes > strong 1`] = ` { "children": [ diff --git a/test/attributes.test.ts b/test/attributes.test.ts index 200362c4..b34058bd 100644 --- a/test/attributes.test.ts +++ b/test/attributes.test.ts @@ -48,6 +48,18 @@ describe('Attributes', () => { }, emphasis: { markdown: '_emphasis_{#id .class}' + }, + 'nested-in-table': { + markdown: [ + '| Col1 | Col2 |', + '| -- | ----- |', + '| aa | [a](/a){a="a"} |' + ].join('\n'), + expected: [ + '| Col1 | Col2 |', + '| ---- | -------------- |', + '| aa | [a](/a){a="a"} |' + ].join('\n') } }) })