Skip to content

Commit

Permalink
refactor: use composition instead of options api
Browse files Browse the repository at this point in the history
  • Loading branch information
p-kuen committed Oct 13, 2023
1 parent 74e562f commit 1058256
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 49 deletions.
4 changes: 2 additions & 2 deletions dist/VueMarkdown.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Component } from 'vue';
export type { Options } from 'markdown-it';
import { Component } from "vue";
export type { Options } from "markdown-it";
declare const VueMarkdown: Component;
export default VueMarkdown;
31 changes: 9 additions & 22 deletions dist/VueMarkdown.js

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

2 changes: 1 addition & 1 deletion dist/VueMarkdown.js.map

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

42 changes: 18 additions & 24 deletions src/VueMarkdown.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { h, PropType, VNode, Component, defineComponent } from 'vue'
import MarkdownIt, { Options as MarkdownItOptions, PluginSimple } from 'markdown-it'
export type { Options } from 'markdown-it'
import MarkdownIt, {
Options as MarkdownItOptions,
PluginSimple,
} from "markdown-it";
import { Component, PropType, computed, defineComponent, h, ref } from "vue";
export type { Options } from "markdown-it";

const VueMarkdown: Component = defineComponent({
name: 'VueMarkdown',
name: "VueMarkdown",
props: {
source: {
type: String,
Expand All @@ -16,28 +19,19 @@ const VueMarkdown: Component = defineComponent({
plugins: {
type: Array as PropType<PluginSimple[]>,
required: false,
}
},
data() {
return {
md: null as MarkdownIt | null,
}
},
computed: {
content(): string | undefined {
const src = this.source
return this.md?.render(src)
},
},
created() {
this.md = new MarkdownIt(this.options ?? {})
for (const plugin of this.plugins ?? []) {
this.md.use(plugin)
setup(props) {
const md = ref<MarkdownIt>(new MarkdownIt(props.options ?? {}));

for (const plugin of props.plugins ?? []) {
md.value.use(plugin);
}

const content = computed(() => md.value.render(props.source));

return () => h("div", { innerHTML: content.value });
},
render(): VNode {
return h('div', { innerHTML: this.content })
},
})
});

export default VueMarkdown
export default VueMarkdown;

0 comments on commit 1058256

Please sign in to comment.