Skip to content

Commit

Permalink
fix(reader): support annotations placed table cells (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
P0lip authored Mar 11, 2022
1 parent cfb920d commit 204b070
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 4 deletions.
30 changes: 26 additions & 4 deletions src/plugins/run/annotations.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Dictionary } from '@stoplight/types';
import * as Yaml from '@stoplight/yaml';
import * as unified from 'unified';
import type { Node } from 'unist';

import { MDAST } from '../../ast-types';

Expand Down Expand Up @@ -34,10 +35,15 @@ export const smdAnnotations: unified.Attacher = function () {
],
};

const entries = nodes.entries()[Symbol.iterator]();
for (const [i, node] of entries) {
for (let i = 0; i < nodes.length; i++) {
const node = nodes[i];

if ('children' in node) {
node.children = transform(node).children;
}

// next node
const next = nodes[i + 1] ?? null;
const [skipped, next] = getNextNode(nodes, i);

// collect annotations, if this is an html node
const anno = captureAnnotations(node);
Expand Down Expand Up @@ -113,7 +119,7 @@ export const smdAnnotations: unified.Attacher = function () {
if (Object.keys(anno).length > 0 && next) {
// annotations apply to next node, process next node now and skip next iteration
root.push(processNode(next, anno));
entries.next();
i += skipped;
} else {
root.push(processNode(node));
}
Expand Down Expand Up @@ -223,3 +229,19 @@ function isHTMLComment(value: unknown): value is string {

return trimmedValue.startsWith('<!--') && trimmedValue.endsWith('-->');
}

function isEmptyNode(node: Node) {
return node.type === 'text' && String(node.value).trim().length === 0;
}

function getNextNode(nodes: MDAST.Content[], pos: number): [skipped: number, nextNode: MDAST.Content | null] {
let next: MDAST.Content | null = null;
let i = pos + 1;

while (i < nodes.length && (next === null || isEmptyNode(next))) {
next = nodes[i];
i++;
}

return [i - pos - 1, next];
}
3 changes: 3 additions & 0 deletions src/reader/__tests__/fixtures/table-cell-with-annotations.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
| foo | bar |
| --- | ----------------------------------------------------------------- |
| baz | <!-- focus: false --> ![picture](https://i.imgur.com/ueOOL8X.png) |
30 changes: 30 additions & 0 deletions src/reader/__tests__/reader.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,36 @@ var x = true;
`);
});

it('should support annotations placed within table cells', () => {
const input = fs.readFileSync(path.join(__dirname, 'fixtures/table-cell-with-annotations.md'), 'utf8');

const mdastTree = mdReader.fromLang(input);

expect(prettyStringify(mdastTree, 'html')).toMatchInlineSnapshot(`
"<table>
<thead>
<tr>
<th>foo</th>
<th>bar</th>
</tr>
</thead>
<tbody>
<tr>
<td>baz</td>
<td>
<img
src=\\"https://i.imgur.com/ueOOL8X.png\\"
alt=\\"picture\\"
focus=\\"false\\"
/>
</td>
</tr>
</tbody>
</table>
"
`);
});

it('should support kitchen sink smd', () => {
const input = fs.readFileSync(path.join(__dirname, 'fixtures/smd.md'), 'utf8');

Expand Down

0 comments on commit 204b070

Please sign in to comment.