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 3f7e419
Show file tree
Hide file tree
Showing 8 changed files with 250 additions and 3 deletions.
34 changes: 33 additions & 1 deletion index.mjs
Original file line number Diff line number Diff line change
@@ -1,9 +1,27 @@
import { getValue, parse, stringify } from 'markdown-code-block-meta';
import {
directiveFromMarkdown,
directiveToMarkdown,
} from 'mdast-util-directive';
import { fromMarkdown } from 'mdast-util-from-markdown';
import { toMarkdown } from 'mdast-util-to-markdown';
import { directive } from 'micromark-extension-directive';
import { visit } from 'unist-util-visit';

function ast2md(ast) {
return toMarkdown({ type: 'root', children: [ast] }).trim();
return toMarkdown(
{ type: 'root', children: [ast] },
{
extensions: [directiveToMarkdown()],
},
).trim();
}

function md2ast(md) {
return fromMarkdown(md, {
extensions: [directive()],
mdastExtensions: [directiveFromMarkdown()],
});
}

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

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

if (key === 'code-eval-copy' && ['md', 'markdown'].includes(lang)) {
const list = md2ast(node.value).children;

parent.children.splice(index + 1, 0, ...list);

meta.delete(key);

node.meta = stringify(meta);

return;
}

meta.delete(key);

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

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

visitCode(tree, 'code-eval-copy');
};
}
11 changes: 9 additions & 2 deletions 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.6",
"description": "Remark plugin to copy live code block as code example",
"license": "MIT",
"author": {
Expand Down Expand Up @@ -39,7 +39,10 @@
},
"dependencies": {
"markdown-code-block-meta": "^0.0.2",
"mdast-util-directive": "^3.0.0",
"mdast-util-from-markdown": "^2.0.1",
"mdast-util-to-markdown": "^2.1.0",
"micromark-extension-directive": "^3.0.0",
"unist-util-visit": "^5.0.0"
},
"devDependencies": {
Expand All @@ -52,7 +55,8 @@
"eslint-plugin-ava": "^14.0.0",
"garou": "^0.7.5",
"prettier": "^3.2.5",
"remark": "^15.0.1"
"remark": "^15.0.1",
"remark-directive": "^3.0.0"
},
"packageManager": "pnpm@9.1.2",
"engines": {
Expand All @@ -62,6 +66,9 @@
"access": "public",
"registry": "https://registry.npmjs.org/"
},
"ava": {
"timeout": "20s"
},
"eslintConfig": {
"extends": "@nice-move/eslint-config-base"
},
Expand Down
113 changes: 113 additions & 0 deletions pnpm-lock.yaml

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

Loading

0 comments on commit 3f7e419

Please sign in to comment.