Skip to content

Commit

Permalink
output TextDirective and LeafDirective as HTML
Browse files Browse the repository at this point in the history
  • Loading branch information
yuki-takei committed Nov 12, 2024
1 parent 97e0e1c commit 9168acb
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
40 changes: 40 additions & 0 deletions apps/app/src/services/renderer/remark-plugins/echo-directive.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import type { ElementContent } from 'hast';
import { h } from 'hastscript';
import type { Text } from 'mdast';
import type { LeafDirective, TextDirective } from 'mdast-util-directive';
import type { Plugin } from 'unified';
import { visit } from 'unist-util-visit';


function echoDirective(node: TextDirective | LeafDirective): ElementContent[] {
const mark = node.type === 'textDirective' ? ':' : '::';

return [
h('span', `${mark}${node.name}`),
...(node.children ?? []).map((child: Text) => h('span', `[${child.value}]`)),
];
}

export const remarkPlugin: Plugin = () => {
return (tree) => {

visit(tree, 'textDirective', (node: TextDirective) => {
const tagName = 'span';

const data = node.data ?? (node.data = {});
data.hName = tagName;
data.hProperties = h(tagName, node.attributes ?? {}).properties;
data.hChildren = echoDirective(node);
});

visit(tree, 'leafDirective', (node: LeafDirective) => {
const tagName = 'div';

const data = node.data ?? (node.data = {});
data.hName = tagName;
data.hProperties = h(tagName, node.attributes ?? {}).properties;
data.hChildren = echoDirective(node);
});

};
};
2 changes: 2 additions & 0 deletions apps/app/src/services/renderer/renderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import * as addClass from './rehype-plugins/add-class';
import { relativeLinks } from './rehype-plugins/relative-links';
import { relativeLinksByPukiwikiLikeLinker } from './rehype-plugins/relative-links-by-pukiwiki-like-linker';
import * as codeBlock from './remark-plugins/codeblock';
import * as echoDirective from './remark-plugins/echo-directive';
import * as emoji from './remark-plugins/emoji';
import { pukiwikiLikeLinker } from './remark-plugins/pukiwiki-like-linker';
import * as xsvToTable from './remark-plugins/xsv-to-table';
Expand Down Expand Up @@ -101,6 +102,7 @@ export const generateCommonOptions = (pagePath: string|undefined): RendererOptio
pukiwikiLikeLinker,
growiDirective,
remarkDirective,
echoDirective.remarkPlugin,
remarkFrontmatter,
codeBlock.remarkPlugin,
],
Expand Down

0 comments on commit 9168acb

Please sign in to comment.