Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix reactivity #10

Closed
wants to merge 1 commit into from
Closed
Changes from all 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
47 changes: 27 additions & 20 deletions lib/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import remarkParse from "remark-parse";
import remarkRehype from "remark-rehype";
import { Component, createComponent, JSX, mergeProps } from "solid-js";
import { children, Component, createMemo, mergeProps } from "solid-js";
import { html } from "property-information";
import { PluggableList, unified } from "unified";
import { VFile } from "vfile";
Expand Down Expand Up @@ -43,34 +43,41 @@ const defaults: SolidMarkdownOptions = {
linkTarget: "_self",
components: {},
};

const SolidMarkdown: Component<Partial<SolidMarkdownOptions>> = (opts) => {
const options: SolidMarkdownOptions = mergeProps(defaults, opts);
const processor = unified()
.use(remarkParse)
.use(options.remarkPlugins || [])
.use(remarkRehype, { allowDangerousHtml: true })
.use(options.rehypePlugins || [])
.use(rehypeFilter, options);
const c = children(() => options.children);

const hastNode = createMemo(() => {
const processor = unified()
.use(remarkParse)
.use(options.remarkPlugins || [])
.use(remarkRehype, { allowDangerousHtml: true })
.use(options.rehypePlugins || [])
.use(rehypeFilter, options);

const file = new VFile();

const file = new VFile();
if (typeof c() === "string") {
file.value = c() as string;
} else if (c() !== undefined && options.children !== null) {
console.warn(
`[solid-markdown] Warning: please pass a string as \`children\` (not: \`${c()}\`)`
);
}

if (typeof options.children === "string") {
file.value = options.children;
} else if (options.children !== undefined && options.children !== null) {
console.warn(
`[solid-markdown] Warning: please pass a string as \`children\` (not: \`${options.children}\`)`
);
}
const hastNode = processor.runSync(processor.parse(file), file);

const hastNode = processor.runSync(processor.parse(file), file);
if (hastNode.type !== "root") {
throw new TypeError("Expected a `root` node");
}

if (hastNode.type !== "root") {
throw new TypeError("Expected a `root` node");
}
return hastNode;
});

return (
<div class={options.class}>
{childrenToSolid({ options, schema: html, listDepth: 0 }, hastNode)}
{childrenToSolid({ options, schema: html, listDepth: 0 }, hastNode())}
</div>
);
};
Expand Down