diff --git a/src/plugins/run/annotations.ts b/src/plugins/run/annotations.ts index a103904..b4a021f 100644 --- a/src/plugins/run/annotations.ts +++ b/src/plugins/run/annotations.ts @@ -98,18 +98,24 @@ export const smdAnnotations: unified.Attacher = function () { } } + let root = processed; + if (inTab) { // if we're in a tab, push this node as a child of the last tab const size = tabPlaceholder.children.length; if (tabPlaceholder.children[size - 1]) { - (tabPlaceholder.children[size - 1].children as MDAST.Content[]).push(processNode(node, anno)); + root = tabPlaceholder.children[size - 1].children as MDAST.Content[]; + } else { + continue; } - } else if (Object.keys(anno).length > 0 && next) { + } + + if (Object.keys(anno).length > 0 && next) { // annotations apply to next node, process next node now and skip next iteration - processed.push(processNode(next, anno)); + root.push(processNode(next, anno)); entries.next(); } else { - processed.push(processNode(node)); + root.push(processNode(node)); } } diff --git a/src/reader/__tests__/fixtures/tabs-with-images.md b/src/reader/__tests__/fixtures/tabs-with-images.md new file mode 100644 index 0000000..f10f65c --- /dev/null +++ b/src/reader/__tests__/fixtures/tabs-with-images.md @@ -0,0 +1,14 @@ +# Article with tabs containing embedded annotations + + + +The contents of tab 1. + + + +![https://i.imgur.com/YCb6MWI.png](https://i.imgur.com/YCb6MWI.png) + + diff --git a/src/reader/__tests__/reader.spec.ts b/src/reader/__tests__/reader.spec.ts index acf4f0a..1894e96 100644 --- a/src/reader/__tests__/reader.spec.ts +++ b/src/reader/__tests__/reader.spec.ts @@ -8,9 +8,10 @@ import { Reader } from '../reader'; const prettier = require('prettier'); -const prettyStringify = (input: MDAST.Root) => { +const prettyStringify = (input: MDAST.Root, parser: 'babel' | 'html' = 'babel') => { const processor = unified().use([html]); - return prettier.format(`<>${processor.stringify(processor.runSync(input))}>`, { parser: 'babel' }); + const output = processor.stringify(processor.runSync(input)); + return prettier.format(parser === 'babel' ? `<>${output}>` : output, { parser }); }; describe('Reader', () => { @@ -264,6 +265,27 @@ var x = true; `); }); + it('should support annotations placed within tabs', () => { + const input = fs.readFileSync(path.join(__dirname, 'fixtures/tabs-with-images.md'), 'utf8'); + + const mdastTree = mdReader.fromLang(input); + + expect(prettyStringify(mdastTree, 'html')).toMatchInlineSnapshot(` +"
The contents of tab 1.
+