-
Notifications
You must be signed in to change notification settings - Fork 220
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
output TextDirective and LeafDirective as HTML
- Loading branch information
1 parent
97e0e1c
commit 9168acb
Showing
2 changed files
with
42 additions
and
0 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
apps/app/src/services/renderer/remark-plugins/echo-directive.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
|
||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters