Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/brown-suns-sin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"svelte-exmarkdown": minor
---

Adds the ability for plugins to pass down handlers to remark2rehype
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"highlightjs-svelte": "^1.0.6",
"jsdom": "^24.0.0",
"katex": "^0.16.9",
"mdast-util-to-hast": "^13.2.0",
"mermaid": "^10.7.0",
"prettier": "^3.2.4",
"prettier-plugin-svelte": "^3.1.2",
Expand Down
27 changes: 8 additions & 19 deletions pnpm-lock.yaml

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

2 changes: 2 additions & 0 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { ComponentType, SvelteComponent } from 'svelte';
import type { Pluggable, Processor } from 'unified';
import type { Handlers } from 'mdast-util-to-hast';

export type Component = ComponentType<SvelteComponent>;
export type ComponentsMap = Record<
Expand All @@ -10,6 +11,7 @@ export type Plugin = {
remarkPlugin?: Pluggable;
rehypePlugin?: Pluggable;
renderer?: ComponentsMap;
handler?: Handlers
};

export type UnistNode = ReturnType<Processor['parse']> & {
Expand Down
5 changes: 4 additions & 1 deletion src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ export const createParser = (plugins: Plugin[]): Parser => {
const processor = unified()
.use(remarkParse)
.use(plugins.map((plugin) => plugin.remarkPlugin).filter(nonNullable))
.use(remarkRehype, { allowDangerousHtml: true })
.use(remarkRehype, {
allowDangerousHtml: true,
handlers: plugins.map((plugin) => plugin.handler).filter(nonNullable).reduce((memo, cur) => ({...memo, ...cur}), {})
})
.use(plugins.map((plugin) => plugin.rehypePlugin).filter(nonNullable))
.use(rehypeReactPropsToSvelteProps);
return (md: string) => processor.runSync(processor.parse(md), md);
Expand Down