@plexui/vue 0.41.0 → 0.41.1
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/es/components/Markdown/Markdown.js +10 -1
- package/dist/es/components/Markdown/Markdown.js.map +1 -1
- package/dist/es/components/Markdown/directives.js +52 -11
- package/dist/es/components/Markdown/directives.js.map +1 -1
- package/dist/types/components/Markdown/directives.d.ts +11 -2
- package/package.json +1 -1
|
@@ -135,7 +135,16 @@ export const Markdown = defineComponent({
|
|
|
135
135
|
const mergedRemarkPlugins = computed(() => {
|
|
136
136
|
const baseRemarkPlugins = [
|
|
137
137
|
remarkDirective,
|
|
138
|
-
|
|
138
|
+
// The directives this Markdown renders as elements: its own and the components' tags
|
|
139
|
+
[
|
|
140
|
+
markdownRemarkDirective,
|
|
141
|
+
{
|
|
142
|
+
names: [
|
|
143
|
+
...(props.directives?.map((directive) => directive.directiveName) ?? []),
|
|
144
|
+
...Object.keys(props.components ?? {}),
|
|
145
|
+
],
|
|
146
|
+
},
|
|
147
|
+
],
|
|
139
148
|
...mathPlugins.value.remarkPlugins,
|
|
140
149
|
];
|
|
141
150
|
if (props.breakNewLines) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Markdown.js","sourceRoot":"","sources":["../../../../src/components/Markdown/Markdown.ts"],"names":[],"mappings":"AACA,OAAO,YAAY,MAAM,eAAe,CAAA;AACxC,OAAO,eAAe,MAAM,kBAAkB,CAAA;AAC9C,OAAO,SAAS,MAAM,YAAY,CAAA;AAClC,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,eAAe,EAAE,CAAC,EAAE,OAAO,EAAE,cAAc,EAAiC,MAAM,KAAK,CAAA;AAChH,OAAO,EAAE,SAAS,EAAE,aAAa,EAAE,iBAAiB,EAAE,WAAW,EAAE,MAAM,2BAA2B,CAAA;AACpG,OAAO,EAAE,oBAAoB,EAAE,MAAM,sCAAsC,CAAA;AAC3E,OAAO,EAAE,Q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U;aACpB,CAAA;YACD,MAAM,UAAU,GACd,KAAK,CAAC,UAAU,EAAE,MAAM,CAAC,CAAC,GAAG,EAAE,SAAS,EAAE,EAAE;gBAC1C,GAAG,CAAC,SAAS,CAAC,aAAa,CAAC,GAAG,SAAS,CAAC,MAAM,CAAA;gBAC/C,OAAO,GAAG,CAAA;YACZ,CAAC,EAAE,cAAc,CAAC,IAAI,cAAc,CAAA;YAEtC,OAAO,cAAc,CAAC;gBACpB,QAAQ,EAAE,MAAM;gBAChB,SAAS,EAAE,cAAc,CAAC,CAAC,CAAC,CAAC,eAAe,EAAE,KAAK,CAAC,KAAK,CAAC,CAAC;gBAC3D,aAAa,EAAE,mBAAmB,CAAC,KAAK;gBACxC,aAAa,EAAE,CAAC,GAAG,CAAC,KAAK,CAAC,aAAa,IAAI,EAAE,CAAC,EAAE,GAAG,WAAW,CAAC,KAAK,CAAC,aAAa,CAAC;gBACnF,UAAU;gBACV,eAAe,EAAE,KAAK,CAAC,eAAe;gBACtC,kBAAkB,EAAE,KAAK,CAAC,kBAAkB;gBAC5C,QAAQ,EAAE,KAAK,CAAC,QAAQ;gBACxB,YAAY,EAAE,KAAK,CAAC,YAAY;aACjC,CAAC,CAAA;QACJ,CAAC,CAAA;IACH,CAAC;CACF,CAAC,CAAA","sourcesContent":["import type { PluggableList } from \"unified\"\nimport remarkBreaks from \"remark-breaks\"\nimport remarkDirective from \"remark-directive\"\nimport remarkGfm from \"remark-gfm\"\nimport { Text, computed, defineComponent, h, isVNode, normalizeClass, type Component, type PropType } from \"vue\"\nimport { CodeBlock, CodeBlockBase, CodeBlockBaseCode, getSlotText } from \"../CodeBlock/CodeBlock.js\"\nimport { CollapsibleCodeBlock } from \"../CodeBlock/CollapsibleCodeBlock.js\"\nimport { TextLink } from \"../TextLink/index.js\"\nimport { markdownRemarkDirective, type MarkdownDirective } from \"./directives.js\"\nimport { renderMarkdown, sanitizeUrl, type MarkdownComponents } from \"./pipeline.js\"\nimport { defaultUrlTransform, type UrlTransform } from \"./urlTransform.js\"\nimport { useMathPlugins } from \"./useMathPlugins.js\"\nimport { useParseMarkdownPre } from \"./useParseMarkdownPre.js\"\nimport s from \"@plexui/styles/components/Markdown/Markdown.module.css\"\n\n// Vue port of packages/ui/src/components/Markdown/Markdown.tsx. React's\n// react-markdown is replaced by the same unified pipeline rendered through\n// Vue's JSX runtime (./pipeline.ts), so the elements, attributes and keys\n// match. The markdown is the text of the default slot (interpolate a string,\n// `{{ md }}`, so its newlines are kept) or, when the slot is empty, the\n// `source` prop.\n\nconst supportsLookbehind = (() => {\n try {\n new RegExp(\"(?<=a)b\")\n return true\n } catch {\n return false\n }\n})()\n\n/**\n * A component that renders one markdown element: it gets the hast `node`\n * prop, the element's attributes (`class`, `href`, ...) and its content as\n * the default slot. A tag name (`\"span\"`) works too.\n */\nexport type MarkdownComponent = Component | string\n\n// Every markdown component gets the hast element as `node`; declaring it\n// keeps it off the DOM\nconst nodeProps = { node: Object as PropType<Record<string, unknown>> }\n\nconst Pre = defineComponent({\n name: \"MarkdownPre\",\n inheritAttrs: false,\n props: nodeProps,\n setup(_props, { slots, attrs }) {\n return () => {\n const { code, language } = useParseMarkdownPre(slots.default?.())\n return h(CodeBlock, { language, class: attrs.class, code })\n }\n },\n})\n\nconst InlineCode = defineComponent({\n name: \"MarkdownInlineCode\",\n inheritAttrs: false,\n props: nodeProps,\n setup(_props, { slots, attrs }) {\n return () => h(\"code\", { class: [s.InlineCode, attrs.class] }, slots.default?.())\n },\n})\n\nconst Anchor = defineComponent({\n name: \"MarkdownAnchor\",\n inheritAttrs: false,\n props: nodeProps,\n setup(_props, { slots, attrs }) {\n // React DOM sanitizes the href TextLink renders; Vue does not, so it happens here\n return () =>\n h(\n TextLink,\n { as: \"a\", ...attrs, ...(typeof attrs.href === \"string\" ? { href: sanitizeUrl(attrs.href) } : null) },\n { default: () => slots.default?.() },\n )\n },\n})\n\n// Sourced from common markdown implementations\nconst TableCell = defineComponent({\n name: \"MarkdownTableCell\",\n props: nodeProps,\n setup(_props, { slots }) {\n return () =>\n h(\n \"td\",\n null,\n (slots.default?.() ?? []).map((child) =>\n // Markdown does not support <br> tags in tables without parsing raw\n // HTML (which may not be safe), so we need to manually handle them\n isVNode(child) && child.type === Text && child.children === \"<br>\" ? h(\"br\") : child,\n ),\n )\n },\n})\n\nconst COMMON_COMPONENTS: MarkdownComponents = {\n code: InlineCode,\n pre: Pre,\n a: Anchor,\n td: TableCell,\n}\n\nconst NonCopyableCodeBlock = defineComponent({\n name: \"MarkdownNonCopyableCodeBlock\",\n inheritAttrs: false,\n props: nodeProps,\n setup(_props, { slots, attrs }) {\n return () => {\n const { code, language } = useParseMarkdownPre(slots.default?.())\n return h(CodeBlockBase, { class: attrs.class }, { default: () => [h(CodeBlockBaseCode, { language, code })] })\n }\n },\n})\n\nconst NON_COPYABLE_CODE_BLOCK_COMPONENTS: MarkdownComponents = {\n pre: NonCopyableCodeBlock,\n}\n\nconst CollapsiblePre = defineComponent({\n name: \"MarkdownCollapsiblePre\",\n inheritAttrs: false,\n props: nodeProps,\n setup(_props, { slots, attrs }) {\n return () => {\n const { code, language } = useParseMarkdownPre(slots.default?.())\n return h(CollapsibleCodeBlock, { language, class: attrs.class, code })\n }\n },\n})\n\nconst COLLAPSIBLE_CODE_BLOCK_COMPONENTS: MarkdownComponents = {\n pre: CollapsiblePre,\n}\n\n/** Renders markdown: GFM, directives, optional LaTeX, Plex code blocks and links. */\nexport const Markdown = defineComponent({\n name: \"Markdown\",\n // Like React, only `class` reaches the DOM (on the wrapping div)\n inheritAttrs: false,\n props: {\n /** Markdown source, used when the default slot is empty */\n source: String,\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n directives: Array as PropType<MarkdownDirective<any>[]>,\n /** Determines if remark-math plugins should be loaded for rendering LaTeX. @default false */\n includeMath: { type: Boolean, default: false },\n /** Determines whether single newlines should insert <br> tags. @default false */\n breakNewLines: { type: Boolean, default: false },\n /** Tag name → component overrides, applied over the built-in ones */\n components: Object as PropType<Record<string, MarkdownComponent>>,\n remarkPlugins: Array as PropType<PluggableList>,\n rehypePlugins: Array as PropType<PluggableList>,\n allowedElements: Array as PropType<ReadonlyArray<string>>,\n disallowedElements: Array as PropType<ReadonlyArray<string>>,\n /** Rewrites URLs; the default drops unsafe protocols (`javascript:`), keeps `tel:` and `sms:` */\n urlTransform: { type: Function as PropType<UrlTransform | null>, default: defaultUrlTransform },\n skipHtml: { type: Boolean, default: false },\n /** @default true */\n copyableCodeBlocks: { type: Boolean, default: true },\n /** Determines if code blocks should use the collapsible view with \"View Code\" button. @default true */\n collapsibleCodeBlocks: { type: Boolean, default: true },\n },\n setup(props, { slots, attrs }) {\n const mathPlugins = useMathPlugins(() => props.includeMath)\n\n const mergedRemarkPlugins = computed(() => {\n const baseRemarkPlugins: PluggableList = [\n remarkDirective,\n markdownRemarkDirective,\n ...mathPlugins.value.remarkPlugins,\n ]\n\n if (props.breakNewLines) {\n baseRemarkPlugins.push(remarkBreaks)\n }\n\n // remark-gfm requires lookbehind support, which will throw in <= Safari 16.4\n // with no easily available polyfill.\n if (supportsLookbehind) {\n // Do not treat single tilde as strikethrough to allow for more\n // conversational use cases such as \"~$20-$30\"\n baseRemarkPlugins.push([remarkGfm, { singleTilde: false }])\n }\n\n return [...baseRemarkPlugins, ...(props.remarkPlugins ?? [])]\n })\n\n return () => {\n const slotText = getSlotText(slots.default?.())\n const source = slotText !== \"\" ? slotText : (props.source ?? \"\")\n\n const baseComponents: MarkdownComponents = {\n ...COMMON_COMPONENTS,\n ...(props.collapsibleCodeBlocks ? COLLAPSIBLE_CODE_BLOCK_COMPONENTS : null),\n ...(props.copyableCodeBlocks ? null : NON_COPYABLE_CODE_BLOCK_COMPONENTS),\n ...props.components,\n }\n const components =\n props.directives?.reduce((acc, directive) => {\n acc[directive.directiveName] = directive.render\n return acc\n }, baseComponents) ?? baseComponents\n\n return renderMarkdown({\n children: source,\n className: normalizeClass([s.MarkdownContent, attrs.class]),\n remarkPlugins: mergedRemarkPlugins.value,\n rehypePlugins: [...(props.rehypePlugins ?? []), ...mathPlugins.value.rehypePlugins],\n components,\n allowedElements: props.allowedElements,\n disallowedElements: props.disallowedElements,\n skipHtml: props.skipHtml,\n urlTransform: props.urlTransform,\n })\n }\n },\n})\n\nexport type MarkdownProps = InstanceType<typeof Markdown>[\"$props\"]\n"]}
|
|
1
|
+
{"version":3,"file":"Markdown.js","sourceRoot":"","sources":["../../../../src/components/Markdown/Markdown.ts"],"names":[],"mappings":"AACA,OAAO,YAAY,MAAM,eAAe,CAAA;AACxC,OAAO,eAAe,MAAM,kBAAkB,CAAA;AAC9C,OAAO,SAAS,MAAM,YAAY,CAAA;AAClC,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,eAAe,EAAE,CAAC,EAAE,OAAO,EAAE,cAAc,EAAiC,MAAM,KAAK,CAAA;AAChH,OAAO,EAAE,SAAS,EAAE,aAAa,EAAE,iBAAiB,EAAE,WAAW,EAAE,MAAM,2BAA2B,CAAA;AACpG,OAAO,EAAE,oBAAoB,EAAE,MAAM,sCAAsC,CAAA;AAC3E,OAAO,EAAE,QAAQ,EAAE,MAAM,sBAAsB,CAAA;AAC/C,OAAO,EAAE,uBAAuB,EAA0B,MAAM,iBAAiB,CAAA;AACjF,OAAO,EAAE,cAAc,EAAE,WAAW,EAA2B,MAAM,eAAe,CAAA;AACpF,OAAO,EAAE,mBAAmB,EAAqB,MAAM,mBAAmB,CAAA;AAC1E,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAA;AACpD,OAAO,EAAE,mBAAmB,EAAE,MAAM,0BAA0B,CAAA;AAC9D,OAAO,CAAC,MAAM,wDAAwD,CAAA;AAEtE,wEAAwE;AACxE,2EAA2E;AAC3E,0EAA0E;AAC1E,6EAA6E;AAC7E,wEAAwE;AACxE,iBAAiB;AAEjB,MAAM,kBAAkB,GAAG,CAAC,GAAG,EAAE;IAC/B,IAAI,CAAC;QACH,IAAI,MAAM,CAAC,SAAS,CAAC,CAAA;QACrB,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pB,QAAQ,EAAE,MAAM;gBAChB,SAAS,EAAE,cAAc,CAAC,CAAC,CAAC,CAAC,eAAe,EAAE,KAAK,CAAC,KAAK,CAAC,CAAC;gBAC3D,aAAa,EAAE,mBAAmB,CAAC,KAAK;gBACxC,aAAa,EAAE,CAAC,GAAG,CAAC,KAAK,CAAC,aAAa,IAAI,EAAE,CAAC,EAAE,GAAG,WAAW,CAAC,KAAK,CAAC,aAAa,CAAC;gBACnF,UAAU;gBACV,eAAe,EAAE,KAAK,CAAC,eAAe;gBACtC,kBAAkB,EAAE,KAAK,CAAC,kBAAkB;gBAC5C,QAAQ,EAAE,KAAK,CAAC,QAAQ;gBACxB,YAAY,EAAE,KAAK,CAAC,YAAY;aACjC,CAAC,CAAA;QACJ,CAAC,CAAA;IACH,CAAC;CACF,CAAC,CAAA","sourcesContent":["import type { PluggableList } from \"unified\"\nimport remarkBreaks from \"remark-breaks\"\nimport remarkDirective from \"remark-directive\"\nimport remarkGfm from \"remark-gfm\"\nimport { Text, computed, defineComponent, h, isVNode, normalizeClass, type Component, type PropType } from \"vue\"\nimport { CodeBlock, CodeBlockBase, CodeBlockBaseCode, getSlotText } from \"../CodeBlock/CodeBlock.js\"\nimport { CollapsibleCodeBlock } from \"../CodeBlock/CollapsibleCodeBlock.js\"\nimport { TextLink } from \"../TextLink/index.js\"\nimport { markdownRemarkDirective, type MarkdownDirective } from \"./directives.js\"\nimport { renderMarkdown, sanitizeUrl, type MarkdownComponents } from \"./pipeline.js\"\nimport { defaultUrlTransform, type UrlTransform } from \"./urlTransform.js\"\nimport { useMathPlugins } from \"./useMathPlugins.js\"\nimport { useParseMarkdownPre } from \"./useParseMarkdownPre.js\"\nimport s from \"@plexui/styles/components/Markdown/Markdown.module.css\"\n\n// Vue port of packages/ui/src/components/Markdown/Markdown.tsx. React's\n// react-markdown is replaced by the same unified pipeline rendered through\n// Vue's JSX runtime (./pipeline.ts), so the elements, attributes and keys\n// match. The markdown is the text of the default slot (interpolate a string,\n// `{{ md }}`, so its newlines are kept) or, when the slot is empty, the\n// `source` prop.\n\nconst supportsLookbehind = (() => {\n try {\n new RegExp(\"(?<=a)b\")\n return true\n } catch {\n return false\n }\n})()\n\n/**\n * A component that renders one markdown element: it gets the hast `node`\n * prop, the element's attributes (`class`, `href`, ...) and its content as\n * the default slot. A tag name (`\"span\"`) works too.\n */\nexport type MarkdownComponent = Component | string\n\n// Every markdown component gets the hast element as `node`; declaring it\n// keeps it off the DOM\nconst nodeProps = { node: Object as PropType<Record<string, unknown>> }\n\nconst Pre = defineComponent({\n name: \"MarkdownPre\",\n inheritAttrs: false,\n props: nodeProps,\n setup(_props, { slots, attrs }) {\n return () => {\n const { code, language } = useParseMarkdownPre(slots.default?.())\n return h(CodeBlock, { language, class: attrs.class, code })\n }\n },\n})\n\nconst InlineCode = defineComponent({\n name: \"MarkdownInlineCode\",\n inheritAttrs: false,\n props: nodeProps,\n setup(_props, { slots, attrs }) {\n return () => h(\"code\", { class: [s.InlineCode, attrs.class] }, slots.default?.())\n },\n})\n\nconst Anchor = defineComponent({\n name: \"MarkdownAnchor\",\n inheritAttrs: false,\n props: nodeProps,\n setup(_props, { slots, attrs }) {\n // React DOM sanitizes the href TextLink renders; Vue does not, so it happens here\n return () =>\n h(\n TextLink,\n { as: \"a\", ...attrs, ...(typeof attrs.href === \"string\" ? { href: sanitizeUrl(attrs.href) } : null) },\n { default: () => slots.default?.() },\n )\n },\n})\n\n// Sourced from common markdown implementations\nconst TableCell = defineComponent({\n name: \"MarkdownTableCell\",\n props: nodeProps,\n setup(_props, { slots }) {\n return () =>\n h(\n \"td\",\n null,\n (slots.default?.() ?? []).map((child) =>\n // Markdown does not support <br> tags in tables without parsing raw\n // HTML (which may not be safe), so we need to manually handle them\n isVNode(child) && child.type === Text && child.children === \"<br>\" ? h(\"br\") : child,\n ),\n )\n },\n})\n\nconst COMMON_COMPONENTS: MarkdownComponents = {\n code: InlineCode,\n pre: Pre,\n a: Anchor,\n td: TableCell,\n}\n\nconst NonCopyableCodeBlock = defineComponent({\n name: \"MarkdownNonCopyableCodeBlock\",\n inheritAttrs: false,\n props: nodeProps,\n setup(_props, { slots, attrs }) {\n return () => {\n const { code, language } = useParseMarkdownPre(slots.default?.())\n return h(CodeBlockBase, { class: attrs.class }, { default: () => [h(CodeBlockBaseCode, { language, code })] })\n }\n },\n})\n\nconst NON_COPYABLE_CODE_BLOCK_COMPONENTS: MarkdownComponents = {\n pre: NonCopyableCodeBlock,\n}\n\nconst CollapsiblePre = defineComponent({\n name: \"MarkdownCollapsiblePre\",\n inheritAttrs: false,\n props: nodeProps,\n setup(_props, { slots, attrs }) {\n return () => {\n const { code, language } = useParseMarkdownPre(slots.default?.())\n return h(CollapsibleCodeBlock, { language, class: attrs.class, code })\n }\n },\n})\n\nconst COLLAPSIBLE_CODE_BLOCK_COMPONENTS: MarkdownComponents = {\n pre: CollapsiblePre,\n}\n\n/** Renders markdown: GFM, directives, optional LaTeX, Plex code blocks and links. */\nexport const Markdown = defineComponent({\n name: \"Markdown\",\n // Like React, only `class` reaches the DOM (on the wrapping div)\n inheritAttrs: false,\n props: {\n /** Markdown source, used when the default slot is empty */\n source: String,\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n directives: Array as PropType<MarkdownDirective<any>[]>,\n /** Determines if remark-math plugins should be loaded for rendering LaTeX. @default false */\n includeMath: { type: Boolean, default: false },\n /** Determines whether single newlines should insert <br> tags. @default false */\n breakNewLines: { type: Boolean, default: false },\n /** Tag name → component overrides, applied over the built-in ones */\n components: Object as PropType<Record<string, MarkdownComponent>>,\n remarkPlugins: Array as PropType<PluggableList>,\n rehypePlugins: Array as PropType<PluggableList>,\n allowedElements: Array as PropType<ReadonlyArray<string>>,\n disallowedElements: Array as PropType<ReadonlyArray<string>>,\n /** Rewrites URLs; the default drops unsafe protocols (`javascript:`), keeps `tel:` and `sms:` */\n urlTransform: { type: Function as PropType<UrlTransform | null>, default: defaultUrlTransform },\n skipHtml: { type: Boolean, default: false },\n /** @default true */\n copyableCodeBlocks: { type: Boolean, default: true },\n /** Determines if code blocks should use the collapsible view with \"View Code\" button. @default true */\n collapsibleCodeBlocks: { type: Boolean, default: true },\n },\n setup(props, { slots, attrs }) {\n const mathPlugins = useMathPlugins(() => props.includeMath)\n\n const mergedRemarkPlugins = computed(() => {\n const baseRemarkPlugins: PluggableList = [\n remarkDirective,\n // The directives this Markdown renders as elements: its own and the components' tags\n [\n markdownRemarkDirective,\n {\n names: [\n ...(props.directives?.map((directive) => directive.directiveName) ?? []),\n ...Object.keys(props.components ?? {}),\n ],\n },\n ],\n ...mathPlugins.value.remarkPlugins,\n ]\n\n if (props.breakNewLines) {\n baseRemarkPlugins.push(remarkBreaks)\n }\n\n // remark-gfm requires lookbehind support, which will throw in <= Safari 16.4\n // with no easily available polyfill.\n if (supportsLookbehind) {\n // Do not treat single tilde as strikethrough to allow for more\n // conversational use cases such as \"~$20-$30\"\n baseRemarkPlugins.push([remarkGfm, { singleTilde: false }])\n }\n\n return [...baseRemarkPlugins, ...(props.remarkPlugins ?? [])]\n })\n\n return () => {\n const slotText = getSlotText(slots.default?.())\n const source = slotText !== \"\" ? slotText : (props.source ?? \"\")\n\n const baseComponents: MarkdownComponents = {\n ...COMMON_COMPONENTS,\n ...(props.collapsibleCodeBlocks ? COLLAPSIBLE_CODE_BLOCK_COMPONENTS : null),\n ...(props.copyableCodeBlocks ? null : NON_COPYABLE_CODE_BLOCK_COMPONENTS),\n ...props.components,\n }\n const components =\n props.directives?.reduce((acc, directive) => {\n acc[directive.directiveName] = directive.render\n return acc\n }, baseComponents) ?? baseComponents\n\n return renderMarkdown({\n children: source,\n className: normalizeClass([s.MarkdownContent, attrs.class]),\n remarkPlugins: mergedRemarkPlugins.value,\n rehypePlugins: [...(props.rehypePlugins ?? []), ...mathPlugins.value.rehypePlugins],\n components,\n allowedElements: props.allowedElements,\n disallowedElements: props.disallowedElements,\n skipHtml: props.skipHtml,\n urlTransform: props.urlTransform,\n })\n }\n },\n})\n\nexport type MarkdownProps = InstanceType<typeof Markdown>[\"$props\"]\n"]}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Comment, Text, isVNode } from "vue";
|
|
2
|
-
import { visit } from "unist-util-visit";
|
|
2
|
+
import { SKIP, visit } from "unist-util-visit";
|
|
3
3
|
import { createId } from "../../lib/ids.js";
|
|
4
4
|
import { decodeBase64, encodeBase64 } from "../../lib/safeBase64.js";
|
|
5
5
|
/**
|
|
@@ -43,17 +43,58 @@ export function createMarkdownDirective({ render, name, mode, }) {
|
|
|
43
43
|
directive.render = renderDirective;
|
|
44
44
|
return directive;
|
|
45
45
|
}
|
|
46
|
-
/**
|
|
47
|
-
|
|
46
|
+
/**
|
|
47
|
+
* A directive's attribute reaches its element only as a plain HTML attribute: a lowercase name (`title`, `data-*`,
|
|
48
|
+
* `aria-*`), so never a DOM property such as `innerHTML` or React's `dangerouslySetInnerHTML`; never an event handler,
|
|
49
|
+
* an inline style, a frame's document or `is`.
|
|
50
|
+
*/
|
|
51
|
+
const ATTRIBUTE_NAME = /^[a-z][a-z0-9-]*$/;
|
|
52
|
+
const UNSAFE_ATTRIBUTE = /^(?:on|style$|srcdoc$|formaction$|is$)/;
|
|
53
|
+
/** The attributes that load or open a URL: one with a protocol other than Markdown's safe ones is dropped. */
|
|
54
|
+
const URL_ATTRIBUTES = new Set(["href", "src", "action", "cite", "poster", "background", "ping", "data", "codebase"]);
|
|
55
|
+
const SAFE_PROTOCOL = /^(?:https?|ircs?|mailto|xmpp|tel|sms)$/i;
|
|
56
|
+
/** react-markdown's `defaultUrlTransform` test, with `tel:` and `sms:` as the Markdown's own transform. */
|
|
57
|
+
const isSafeUrl = (value) => {
|
|
58
|
+
const colon = value.indexOf(":");
|
|
59
|
+
const firstOf = (char) => value.indexOf(char);
|
|
60
|
+
return (colon === -1 ||
|
|
61
|
+
[firstOf("/"), firstOf("?"), firstOf("#")].some((index) => index !== -1 && colon > index) ||
|
|
62
|
+
SAFE_PROTOCOL.test(value.slice(0, colon).trim()));
|
|
63
|
+
};
|
|
64
|
+
const safeAttributes = (attributes) => Object.fromEntries(Object.entries(attributes ?? {}).filter(([key, value]) => ATTRIBUTE_NAME.test(key) &&
|
|
65
|
+
!UNSAFE_ATTRIBUTE.test(key) &&
|
|
66
|
+
!(URL_ATTRIBUTES.has(key) && !isSafeUrl(String(value ?? "")))));
|
|
67
|
+
/**
|
|
68
|
+
* Directives become elements only when the Markdown knows them: a `directives` entry or a `components` key, with
|
|
69
|
+
* their attributes minus event handlers, `style` and `srcdoc`. Any other directive stays what was written: an inline
|
|
70
|
+
* or leaf one its source text, a container one its content in a plain `div`. Text from elsewhere (a server, a user)
|
|
71
|
+
* can't make an element of its choosing with attributes of its choosing.
|
|
72
|
+
*/
|
|
73
|
+
export function markdownRemarkDirective(options = {}) {
|
|
74
|
+
const names = new Set(options.names ?? []);
|
|
48
75
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
49
|
-
return (tree) => {
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
...node.data
|
|
55
|
-
|
|
56
|
-
|
|
76
|
+
return (tree, file) => {
|
|
77
|
+
const source = file?.value == null ? "" : String(file.value);
|
|
78
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
79
|
+
visit(tree, ["textDirective", "leafDirective", "containerDirective"], (node, index, parent) => {
|
|
80
|
+
if (names.has(node.name)) {
|
|
81
|
+
node.data = { hName: node.name, hProperties: safeAttributes(node.attributes), ...node.data };
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
if (node.type === "containerDirective") {
|
|
85
|
+
node.data = { hName: "div", hProperties: {}, ...node.data };
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
if (!parent || typeof index !== "number")
|
|
89
|
+
return;
|
|
90
|
+
const start = node.position?.start?.offset;
|
|
91
|
+
const end = node.position?.end?.offset;
|
|
92
|
+
const value = typeof start === "number" && typeof end === "number" && end <= source.length
|
|
93
|
+
? source.slice(start, end)
|
|
94
|
+
: `${node.type === "leafDirective" ? "::" : ":"}${node.name}`;
|
|
95
|
+
const text = { type: "text", value };
|
|
96
|
+
parent.children[index] = node.type === "leafDirective" ? { type: "paragraph", children: [text] } : text;
|
|
97
|
+
return SKIP;
|
|
57
98
|
});
|
|
58
99
|
};
|
|
59
100
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"directives.js","sourceRoot":"","sources":["../../../../src/components/Markdown/directives.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,OAAO,EAAkF,MAAM,KAAK,CAAA;AAC5H,OAAO,EAAE,KAAK,EAAE,MAAM,kBAAkB,CAAA;AACxC,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAA;AAC3C,OAAO,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAA;AAWpE;;;GAGG;AACH,MAAM,UAAU,GAAG,CAAC,KAAqC,EAAmD,EAAE;IAC5G,IAAI,CAAC,KAAK,IAAI,KAAK,CAAC,MAAM,KAAK,CAAC;QAAE,OAAO,SAAS,CAAA;IAClD,IAAI,KAAK,CAAC,MAAM,GAAG,CAAC;QAAE,OAAO,KAAK,CAAA;IAClC,MAAM,CAAC,IAAI,CAAC,GAAG,KAAK,CAAA;IACpB,IAAI,OAAO,CAAC,IAAI,CAAC,IAAI,IAAI,CAAC,IAAI,KAAK,IAAI;QAAE,OAAO,MAAM,CAAC,IAAI,CAAC,QAAQ,IAAI,EAAE,CAAC,CAAA;IAC3E,IAAI,OAAO,CAAC,IAAI,CAAC,IAAI,IAAI,CAAC,IAAI,KAAK,OAAO;QAAE,OAAO,SAAS,CAAA;IAC5D,OAAO,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,KAAK,CAAA;AACrC,CAAC,CAAA;AAED;;;;GAIG;AACH,MAAM,UAAU,uBAAuB,CAAgC,EACrE,MAAM,EAC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MAAM,UAAU,uBAAuB;IACrC,8DAA8D;IAC9D,OAAO,CAAC,IAAS,EAAE,EAAE;QACnB,KAAK,CAAC,IAAI,EAAE,CAAC,eAAe,EAAE,eAAe,EAAE,oBAAoB,CAAC,EAAE,CAAC,IAAI,EAAE,EAAE;YAC7E,IAAI,CAAC,IAAI,GAAG;gBACV,KAAK,EAAE,IAAI,CAAC,IAAI;gBAChB,WAAW,EAAE,IAAI,CAAC,UAAU;gBAC5B,GAAG,IAAI,CAAC,IAAI;aACb,CAAA;YACD,OAAO,IAAI,CAAA;QACb,CAAC,CAAC,CAAA;IACJ,CAAC,CAAA;AACH,CAAC","sourcesContent":["import { Comment, Text, isVNode, type FunctionalComponent, type VNode, type VNodeArrayChildren, type VNodeChild } from \"vue\"\nimport { visit } from \"unist-util-visit\"\nimport { createId } from \"../../lib/ids.js\"\nimport { decodeBase64, encodeBase64 } from \"../../lib/safeBase64.js\"\n\ntype ValidDirectiveProps = { children?: string } & Partial<Record<string, unknown>>\n\nexport interface MarkdownDirective<T extends ValidDirectiveProps = ValidDirectiveProps> {\n directiveName: string\n /** Functional component Markdown renders the directive with */\n render: FunctionalComponent<{ attrs?: string }>\n (props: T): string\n}\n\n/**\n * React's rendered `children`: nothing, the text of a single text child, the\n * single vnode, or the list.\n */\nconst toChildren = (nodes: VNodeArrayChildren | undefined): string | VNode | VNodeArrayChildren | undefined => {\n if (!nodes || nodes.length === 0) return undefined\n if (nodes.length > 1) return nodes\n const [node] = nodes\n if (isVNode(node) && node.type === Text) return String(node.children ?? \"\")\n if (isVNode(node) && node.type === Comment) return undefined\n return isVNode(node) ? node : nodes\n}\n\n/**\n * Vue port of packages/ui/src/components/Markdown/directives.ts. `render`\n * gets the decoded attributes plus `children`: the directive's label as a\n * string when it is plain text, otherwise its vnodes.\n */\nexport function createMarkdownDirective<T extends ValidDirectiveProps>({\n render,\n name,\n mode,\n}: {\n render: (props: T) => VNodeChild\n name: string\n mode: \"inline\" | \"block\"\n}): MarkdownDirective<T> {\n const prefix = `OaiMdDirective_${name}`\n const uniqueName = createId(prefix, prefix.length + 6)\n\n function directive(props: T) {\n const { children, ...attributes } = props\n // Always true, as in React: the attributes are encoded even when empty\n const attrs = Object.keys(attributes).length >= 0 ? `attrs=\"${encodeBase64(attributes)}\"` : \"\"\n const leader = mode === \"inline\" ? \" :\" : \"\\n\\n:::\"\n\n return `${leader}${uniqueName}${children ? `[${children}]` : \"\"}{${attrs}}`\n }\n\n const renderDirective: FunctionalComponent<{ attrs?: string }> = ({ attrs }, { slots }) => {\n const parsedAttrs = attrs ? decodeBase64<T>(attrs) : ({} as T)\n return render({ ...parsedAttrs, children: toChildren(slots.default?.()) })\n }\n // React's render reads only `attrs` and `children`: nothing falls through\n renderDirective.inheritAttrs = false\n\n directive.directiveName = uniqueName\n directive.render = renderDirective\n\n return directive\n}\n\n/** React's `reactMarkdownRemarkDirective`: directives become elements named after them. */\nexport function markdownRemarkDirective() {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n return (tree: any) => {\n visit(tree, [\"textDirective\", \"leafDirective\", \"containerDirective\"], (node) => {\n node.data = {\n hName: node.name,\n hProperties: node.attributes,\n ...node.data,\n }\n return node\n })\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"directives.js","sourceRoot":"","sources":["../../../../src/components/Markdown/directives.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,OAAO,EAAkF,MAAM,KAAK,CAAA;AAC5H,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,MAAM,kBAAkB,CAAA;AAC9C,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAA;AAC3C,OAAO,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAA;AAWpE;;;GAGG;AACH,MAAM,UAAU,GAAG,CAAC,KAAqC,EAAmD,EAAE;IAC5G,IAAI,CAAC,KAAK,IAAI,KAAK,CAAC,MAAM,KAAK,CAAC;QAAE,OAAO,SAAS,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,CAAC","sourcesContent":["import { Comment, Text, isVNode, type FunctionalComponent, type VNode, type VNodeArrayChildren, type VNodeChild } from \"vue\"\nimport { SKIP, visit } from \"unist-util-visit\"\nimport { createId } from \"../../lib/ids.js\"\nimport { decodeBase64, encodeBase64 } from \"../../lib/safeBase64.js\"\n\ntype ValidDirectiveProps = { children?: string } & Partial<Record<string, unknown>>\n\nexport interface MarkdownDirective<T extends ValidDirectiveProps = ValidDirectiveProps> {\n directiveName: string\n /** Functional component Markdown renders the directive with */\n render: FunctionalComponent<{ attrs?: string }>\n (props: T): string\n}\n\n/**\n * React's rendered `children`: nothing, the text of a single text child, the\n * single vnode, or the list.\n */\nconst toChildren = (nodes: VNodeArrayChildren | undefined): string | VNode | VNodeArrayChildren | undefined => {\n if (!nodes || nodes.length === 0) return undefined\n if (nodes.length > 1) return nodes\n const [node] = nodes\n if (isVNode(node) && node.type === Text) return String(node.children ?? \"\")\n if (isVNode(node) && node.type === Comment) return undefined\n return isVNode(node) ? node : nodes\n}\n\n/**\n * Vue port of packages/ui/src/components/Markdown/directives.ts. `render`\n * gets the decoded attributes plus `children`: the directive's label as a\n * string when it is plain text, otherwise its vnodes.\n */\nexport function createMarkdownDirective<T extends ValidDirectiveProps>({\n render,\n name,\n mode,\n}: {\n render: (props: T) => VNodeChild\n name: string\n mode: \"inline\" | \"block\"\n}): MarkdownDirective<T> {\n const prefix = `OaiMdDirective_${name}`\n const uniqueName = createId(prefix, prefix.length + 6)\n\n function directive(props: T) {\n const { children, ...attributes } = props\n // Always true, as in React: the attributes are encoded even when empty\n const attrs = Object.keys(attributes).length >= 0 ? `attrs=\"${encodeBase64(attributes)}\"` : \"\"\n const leader = mode === \"inline\" ? \" :\" : \"\\n\\n:::\"\n\n return `${leader}${uniqueName}${children ? `[${children}]` : \"\"}{${attrs}}`\n }\n\n const renderDirective: FunctionalComponent<{ attrs?: string }> = ({ attrs }, { slots }) => {\n const parsedAttrs = attrs ? decodeBase64<T>(attrs) : ({} as T)\n return render({ ...parsedAttrs, children: toChildren(slots.default?.()) })\n }\n // React's render reads only `attrs` and `children`: nothing falls through\n renderDirective.inheritAttrs = false\n\n directive.directiveName = uniqueName\n directive.render = renderDirective\n\n return directive\n}\n\n/**\n * A directive's attribute reaches its element only as a plain HTML attribute: a lowercase name (`title`, `data-*`,\n * `aria-*`), so never a DOM property such as `innerHTML` or React's `dangerouslySetInnerHTML`; never an event handler,\n * an inline style, a frame's document or `is`.\n */\nconst ATTRIBUTE_NAME = /^[a-z][a-z0-9-]*$/\nconst UNSAFE_ATTRIBUTE = /^(?:on|style$|srcdoc$|formaction$|is$)/\n\n/** The attributes that load or open a URL: one with a protocol other than Markdown's safe ones is dropped. */\nconst URL_ATTRIBUTES = new Set([\"href\", \"src\", \"action\", \"cite\", \"poster\", \"background\", \"ping\", \"data\", \"codebase\"])\nconst SAFE_PROTOCOL = /^(?:https?|ircs?|mailto|xmpp|tel|sms)$/i\n\n/** react-markdown's `defaultUrlTransform` test, with `tel:` and `sms:` as the Markdown's own transform. */\nconst isSafeUrl = (value: string) => {\n const colon = value.indexOf(\":\")\n const firstOf = (char: string) => value.indexOf(char)\n return (\n colon === -1 ||\n [firstOf(\"/\"), firstOf(\"?\"), firstOf(\"#\")].some((index) => index !== -1 && colon > index) ||\n SAFE_PROTOCOL.test(value.slice(0, colon).trim())\n )\n}\n\nconst safeAttributes = (attributes: Record<string, unknown> | null | undefined) =>\n Object.fromEntries(\n Object.entries(attributes ?? {}).filter(\n ([key, value]) =>\n ATTRIBUTE_NAME.test(key) &&\n !UNSAFE_ATTRIBUTE.test(key) &&\n !(URL_ATTRIBUTES.has(key) && !isSafeUrl(String(value ?? \"\"))),\n ),\n )\n\n/**\n * Directives become elements only when the Markdown knows them: a `directives` entry or a `components` key, with\n * their attributes minus event handlers, `style` and `srcdoc`. Any other directive stays what was written: an inline\n * or leaf one its source text, a container one its content in a plain `div`. Text from elsewhere (a server, a user)\n * can't make an element of its choosing with attributes of its choosing.\n */\nexport function markdownRemarkDirective(options: { names?: Iterable<string> } = {}) {\n const names = new Set(options.names ?? [])\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n return (tree: any, file?: { value?: unknown }) => {\n const source = file?.value == null ? \"\" : String(file.value)\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n visit(tree, [\"textDirective\", \"leafDirective\", \"containerDirective\"], (node: any, index, parent: any) => {\n if (names.has(node.name)) {\n node.data = { hName: node.name, hProperties: safeAttributes(node.attributes), ...node.data }\n return\n }\n if (node.type === \"containerDirective\") {\n node.data = { hName: \"div\", hProperties: {}, ...node.data }\n return\n }\n if (!parent || typeof index !== \"number\") return\n const start = node.position?.start?.offset\n const end = node.position?.end?.offset\n const value =\n typeof start === \"number\" && typeof end === \"number\" && end <= source.length\n ? source.slice(start, end)\n : `${node.type === \"leafDirective\" ? \"::\" : \":\"}${node.name}`\n const text = { type: \"text\", value }\n parent.children[index] = node.type === \"leafDirective\" ? { type: \"paragraph\", children: [text] } : text\n return SKIP\n })\n }\n}\n"]}
|
|
@@ -20,6 +20,15 @@ export declare function createMarkdownDirective<T extends ValidDirectiveProps>({
|
|
|
20
20
|
name: string;
|
|
21
21
|
mode: "inline" | "block";
|
|
22
22
|
}): MarkdownDirective<T>;
|
|
23
|
-
/**
|
|
24
|
-
|
|
23
|
+
/**
|
|
24
|
+
* Directives become elements only when the Markdown knows them: a `directives` entry or a `components` key, with
|
|
25
|
+
* their attributes minus event handlers, `style` and `srcdoc`. Any other directive stays what was written: an inline
|
|
26
|
+
* or leaf one its source text, a container one its content in a plain `div`. Text from elsewhere (a server, a user)
|
|
27
|
+
* can't make an element of its choosing with attributes of its choosing.
|
|
28
|
+
*/
|
|
29
|
+
export declare function markdownRemarkDirective(options?: {
|
|
30
|
+
names?: Iterable<string>;
|
|
31
|
+
}): (tree: any, file?: {
|
|
32
|
+
value?: unknown;
|
|
33
|
+
}) => void;
|
|
25
34
|
export {};
|