@oiij/markdown-it 0.0.10 → 0.0.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.mts CHANGED
@@ -1,19 +1,21 @@
1
1
  import markdownIt, { Options } from "markdown-it";
2
2
  import * as vue0 from "vue";
3
- import { Ref, TemplateRef } from "vue";
3
+ import { MaybeRefOrGetter, TemplateRef } from "vue";
4
4
 
5
5
  //#region src/index.d.ts
6
- type MarkDownItOptions = Options & {
6
+ type UseMarkDownItOptions = {
7
+ value?: MaybeRefOrGetter<string>;
7
8
  manual?: boolean;
8
9
  domPurify?: boolean;
10
+ markdownItOptions?: Options;
9
11
  };
10
- declare function useMarkdownIt(templateRef?: TemplateRef<HTMLElement>, defaultValue?: Ref<string> | string, options?: MarkDownItOptions): {
11
- value: Ref<string | undefined, string | undefined>;
12
- html: Ref<string, string>;
12
+ declare function useMarkdownIt(templateRef?: TemplateRef<HTMLElement>, options?: UseMarkDownItOptions): {
13
13
  templateRef: Readonly<vue0.ShallowRef<HTMLElement | null>> | undefined;
14
- md: markdownIt;
15
- render: (value: string) => string;
14
+ valueRef: vue0.Ref<string | undefined, string | undefined>;
15
+ htmlRef: vue0.Ref<string, string>;
16
+ markdownItInst: markdownIt;
17
+ render: (value?: string) => string;
16
18
  };
17
19
  type MarkdownItReturns = ReturnType<typeof useMarkdownIt>;
18
20
  //#endregion
19
- export { MarkDownItOptions, MarkdownItReturns, useMarkdownIt };
21
+ export { MarkdownItReturns, UseMarkDownItOptions, useMarkdownIt };
package/dist/index.mjs CHANGED
@@ -1,33 +1,47 @@
1
1
  import DOMPurify from "dompurify";
2
2
  import markdownIt from "markdown-it";
3
- import { isReactive, isRef, ref, toValue, watch, watchEffect } from "vue";
3
+ import { onMounted, onUnmounted, ref, toValue, watch, watchEffect } from "vue";
4
+ import "@vueuse/core";
4
5
 
6
+ //#region ../_utils/custom-watch.ts
7
+ function watchRefOrGetter(value, callback) {
8
+ const refValue = ref(toValue(value));
9
+ watchEffect(() => {
10
+ refValue.value = toValue(value);
11
+ });
12
+ watch(refValue, (val) => {
13
+ callback?.(val);
14
+ });
15
+ return refValue;
16
+ }
17
+
18
+ //#endregion
5
19
  //#region src/index.ts
6
- function useMarkdownIt(templateRef, defaultValue, options) {
7
- const { manual = false, domPurify = true, ..._options } = options ?? {};
8
- const value = ref(isRef(defaultValue) ? toValue(defaultValue.value) : isReactive(defaultValue) ? toValue(defaultValue) : defaultValue);
9
- if (isRef(defaultValue)) watchEffect(() => {
10
- value.value = toValue(defaultValue.value);
20
+ function useMarkdownIt(templateRef, options) {
21
+ const { value, manual = false, domPurify = true, markdownItOptions } = options ?? {};
22
+ const valueRef = watchRefOrGetter(value, () => {
23
+ if (!manual) render();
11
24
  });
12
- const html = ref("");
13
- const md = markdownIt({ ..._options });
14
- function render(value$1) {
15
- const mdValue = md.render(value$1);
16
- html.value = domPurify ? DOMPurify.sanitize(mdValue) : mdValue;
17
- if (templateRef && templateRef.value) templateRef.value.innerHTML = html.value;
18
- return html.value;
19
- }
20
- if (!manual) {
21
- render(value.value ?? "");
22
- watch(value, (v) => {
23
- render(v ?? "");
24
- });
25
+ const htmlRef = ref("");
26
+ const markdownItInst = markdownIt({ ...markdownItOptions });
27
+ function render(value) {
28
+ if (value) valueRef.value = value;
29
+ const mdValue = markdownItInst.render(valueRef.value ?? "");
30
+ htmlRef.value = domPurify ? DOMPurify.sanitize(mdValue) : mdValue;
31
+ if (templateRef?.value) templateRef.value.innerHTML = htmlRef.value;
32
+ return htmlRef.value;
25
33
  }
34
+ onMounted(() => {
35
+ if (!manual) render();
36
+ });
37
+ onUnmounted(() => {
38
+ if (templateRef?.value) templateRef.value.innerHTML = "";
39
+ });
26
40
  return {
27
- value,
28
- html,
29
41
  templateRef,
30
- md,
42
+ valueRef,
43
+ htmlRef,
44
+ markdownItInst,
31
45
  render
32
46
  };
33
47
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@oiij/markdown-it",
3
3
  "type": "module",
4
- "version": "0.0.10",
4
+ "version": "0.0.11",
5
5
  "description": "A Vue Composable for markdown-it",
6
6
  "author": "oiij",
7
7
  "license": "MIT",
@@ -29,14 +29,14 @@
29
29
  "@vueuse/core": "^14.1.0",
30
30
  "dompurify": "^3.3.1",
31
31
  "markdown-it": "^14.1.0",
32
- "vue": "^3.5.26"
32
+ "vue": "^3.5.27"
33
33
  },
34
34
  "devDependencies": {
35
35
  "@types/markdown-it": "^14.1.2",
36
36
  "@vueuse/core": "^14.1.0",
37
37
  "dompurify": "^3.3.1",
38
38
  "markdown-it": "^14.1.0",
39
- "vue": "^3.5.26"
39
+ "vue": "^3.5.27"
40
40
  },
41
41
  "publishConfig": {
42
42
  "access": "public"