Skip to content

Commit

Permalink
feat(mdsvex): allow user provided code plugins to run before builtin …
Browse files Browse the repository at this point in the history
…highlighting
  • Loading branch information
pngwn committed Mar 21, 2021
1 parent 3dc7958 commit ab0a0fe
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 17 deletions.
1 change: 1 addition & 0 deletions packages/mdsvex/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"front-matter": "^4.0.0",
"js-yaml": "^3.13.1",
"node-fetch": "^2.6.0",
"rehype-shiki": "^0.0.9",
"rehype-slug": "^3.0.0",
"rehype-toc": "^3.0.1",
"remark-autolink-headings": "^6.0.0",
Expand Down
9 changes: 5 additions & 4 deletions packages/mdsvex/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,7 @@ export function transform(
.use(external, { target: false, rel: ['nofollow'] })
.use(escape_code, { blocks: !!highlight })
.use(extract_frontmatter, [{ type: fm_opts.type, marker: fm_opts.marker }])
.use(parse_frontmatter, { parse: fm_opts.parse, type: fm_opts.type })
.use(highlight_blocks, highlight || {});
.use(parse_frontmatter, { parse: fm_opts.parse, type: fm_opts.type });

if (smartypants) {
toMDAST.use(
Expand All @@ -87,7 +86,7 @@ export function transform(
);
}

apply_plugins(remarkPlugins, toMDAST);
apply_plugins(remarkPlugins, toMDAST).use(highlight_blocks, highlight || {});

const toHAST = toMDAST
.use(remark2rehype, {
Expand Down Expand Up @@ -280,7 +279,9 @@ export const mdsvex = (options: MdsvexOptions = defaults): Preprocessor => {

return {
markup: async ({ content, filename }) => {
const extensionsParts = (extensions || [extension]).map(ext => ext.split('.').pop());
const extensionsParts = (extensions || [extension]).map((ext) =>
ext.split('.').pop()
);
if (!extensionsParts.includes(filename.split('.').pop())) return;

const parsed = await parser.process({ contents: content, filename });
Expand Down
34 changes: 34 additions & 0 deletions packages/mdsvex/test/it/mdsvex.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { suite } from 'uvu';
import * as assert from 'uvu/assert';
import { Node, Parent } from 'unist';

import { join } from 'path';
import { lines } from '../utils';
Expand All @@ -13,6 +14,7 @@ import toc from 'rehype-toc';
import rehype_slug from 'rehype-slug';
import toml from 'toml';
import VMessage, { VFileMessage } from 'vfile-message';
import { Transformer } from 'unified';

const mdsvex_it = suite('mdsvex');
const fix_dir = join(__dirname, '..', '_fixtures');
Expand Down Expand Up @@ -171,6 +173,38 @@ mdsvex_it('it should accept rehype plugins with options - plural', async () => {
);
});

mdsvex_it(
'it should accept remark plugins that modify code blocks',
async () => {
function code_plugin(): Transformer {
return function (tree: Node): void {
(tree as Parent).children.forEach((node) => {
if (node.type === 'code') {
node.type = 'html';
node.value = `<p>The Code is: <pre>${node.value}</pre></p>`;
}
});
};
}

const output = await mdsvex({
remarkPlugins: [code_plugin],
}).markup({
content: `
\`\`\`booboo
hello friends
\`\`\`
`,
filename: 'file.svx',
});

assert.equal(
lines(`<p>The Code is: <pre>hello friends</pre></p>`),
output && lines(output.code)
);
}
);

mdsvex_it('it should respect the smartypants option', async () => {
const output = await mdsvex({
smartypants: true,
Expand Down
116 changes: 103 additions & 13 deletions pnpm-lock.yaml

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

0 comments on commit ab0a0fe

Please sign in to comment.