Skip to content

Commit

Permalink
feat: code-eval
Browse files Browse the repository at this point in the history
  • Loading branch information
Airkro committed May 30, 2024
1 parent 89ab699 commit 215be4c
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 1 deletion.
9 changes: 9 additions & 0 deletions index.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { getValue, parse, stringify } from 'markdown-code-block-meta';
import { fromMarkdown } from 'mdast-util-from-markdown';
import { toMarkdown } from 'mdast-util-to-markdown';
import { visit } from 'unist-util-visit';

Expand All @@ -15,6 +16,12 @@ function visitCode(tree, key, visitor) {

const lang = getValue(meta.get(key)) || 'markdown';

if (key === 'code-eval' && ['md', 'markdown'].includes(lang)) {
parent.children.splice(index, 1, ...fromMarkdown(node.value).children);

return;
}

meta.delete(key);

const copyToBefore = meta.has('copy-to-before');
Expand Down Expand Up @@ -101,5 +108,7 @@ export function remarkCodeExample({ metas = {} } = {}) {

node.meta = stringify(new Map([...extra, ...newMeta]));
});

visitCode(tree, 'code-eval');
};
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "remark-code-example",
"version": "0.5.4",
"version": "0.5.5",
"description": "Remark plugin to copy live code block as code example",
"license": "MIT",
"author": {
Expand Down Expand Up @@ -39,6 +39,7 @@
},
"dependencies": {
"markdown-code-block-meta": "^0.0.2",
"mdast-util-from-markdown": "^2.0.1",
"mdast-util-to-markdown": "^2.1.0",
"unist-util-visit": "^5.0.0"
},
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 33 additions & 0 deletions test/code-eval.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import test from 'ava';

import { transform } from './helper/lib.mjs';

test('code-eval | match', async (t) => {
const input = `
\`\`\`md code-eval
> [!NOTE]
> Useful information that users should know, even when skimming content.
:::tip[label]
Some **content** with _markdown_ .
:::
\`\`\`
`;

const output = await transform(input);

t.snapshot(input);
t.snapshot(output);
});

test('code-eval | none', async (t) => {
const input = `
\`\`\`js code-eval
\`\`\`
`;

const output = await transform(input);

t.snapshot(input);
t.snapshot(output);
});
Binary file modified test/snapshots/as-markdown.mjs.snap
Binary file not shown.
43 changes: 43 additions & 0 deletions test/snapshots/code-eval.mjs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Snapshot report for `test/code-eval.mjs`

The actual snapshot is saved in `code-eval.mjs.snap`.

Generated by [AVA](https://avajs.dev).

## code-eval | match

> Snapshot 1
`␊
\`\`\`md code-eval␊
> [!NOTE]␊
> Useful information that users should know, even when skimming content.␊
:::tip[label]␊
Some **content** with _markdown_ .␊
:::␊
\`\`\`␊
`

> Snapshot 2
`> \\[!NOTE]␊
> Useful information that users should know, even when skimming content.␊
:::tip\\[label]␊
Some **content** with *markdown* .␊
:::␊
`

## code-eval | none

> Snapshot 1
`␊
\`\`\`js code-eval␊
\`\`\`␊
`

> Snapshot 2
''
Binary file added test/snapshots/code-eval.mjs.snap
Binary file not shown.

0 comments on commit 215be4c

Please sign in to comment.