Skip to content

Commit

Permalink
fix: fix preview html meta issue.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed Oct 29, 2022
1 parent 75fece7 commit cb704ee
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
2 changes: 1 addition & 1 deletion scripts/create.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ export function create(str = '', options = {}) {
});
// 放在 rehypeDocument 前面
dt.unshift(rehypeRaw);
dt.unshift(rehypePreviewHTML);
return dt;
}
return plugins;
},
rewrite: (node, index, parent) => {
rehypePreviewHTML(node, parent);
rehypeTitle(node, options.filename);
homeCardIcons(node, parent, options.isHome);
tooltips(node, index, parent);
Expand Down
27 changes: 18 additions & 9 deletions scripts/utils/rehypePreviewHTML.mjs
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
import { visit } from 'unist-util-visit';
import { getCodeString } from 'rehype-rewrite';
import { getVNode } from './getSVGNode.mjs';

export function rehypePreviewHTML(node, parent) {
if (node.type === 'element' && node.tagName === 'pre' && node.properties?.className?.includes('language-html')) {
const child = node.children[0];
if (child?.tagName === 'code' && child.data?.meta === 'preview') {
const code = getCodeString(node.children);
const vnode = getVNode(code || '');
node.children = vnode;
}
}
export function rehypePreviewHTML() {
return (tree) => {
visit(tree, (node, index, parent) => {
if (node.type === 'element' && node.tagName === 'pre') {
const child = node.children[0];
if (
child.properties?.className?.includes('language-html') &&
child?.tagName === 'code' &&
child.data?.meta === 'preview'
) {
const code = getCodeString(child.children);
const vnode = getVNode(code || '');
node.children = vnode;
}
}
});
};
}

0 comments on commit cb704ee

Please sign in to comment.