@nuxtjs/mdc 0.1.0

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.
Files changed (109) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +64 -0
  3. package/dist/module.cjs +5 -0
  4. package/dist/module.d.mts +55 -0
  5. package/dist/module.d.ts +55 -0
  6. package/dist/module.json +5 -0
  7. package/dist/module.mjs +174 -0
  8. package/dist/runtime/components/MDC.vue +36 -0
  9. package/dist/runtime/components/MDCRenderer.vue +293 -0
  10. package/dist/runtime/components/MDCRenderer.vue.d.ts +92 -0
  11. package/dist/runtime/components/MDCSlot.vue +60 -0
  12. package/dist/runtime/components/MDCSlot.vue.d.ts +5 -0
  13. package/dist/runtime/components/prose/ProseA.vue +22 -0
  14. package/dist/runtime/components/prose/ProseBlockquote.vue +5 -0
  15. package/dist/runtime/components/prose/ProseCode.vue +3 -0
  16. package/dist/runtime/components/prose/ProseEm.vue +5 -0
  17. package/dist/runtime/components/prose/ProseH1.vue +20 -0
  18. package/dist/runtime/components/prose/ProseH2.vue +20 -0
  19. package/dist/runtime/components/prose/ProseH3.vue +20 -0
  20. package/dist/runtime/components/prose/ProseH4.vue +20 -0
  21. package/dist/runtime/components/prose/ProseH5.vue +20 -0
  22. package/dist/runtime/components/prose/ProseH6.vue +20 -0
  23. package/dist/runtime/components/prose/ProseHr.vue +3 -0
  24. package/dist/runtime/components/prose/ProseImg.vue +42 -0
  25. package/dist/runtime/components/prose/ProseLi.vue +3 -0
  26. package/dist/runtime/components/prose/ProseOl.vue +5 -0
  27. package/dist/runtime/components/prose/ProseP.vue +3 -0
  28. package/dist/runtime/components/prose/ProsePre.vue +39 -0
  29. package/dist/runtime/components/prose/ProseStrong.vue +5 -0
  30. package/dist/runtime/components/prose/ProseTable.vue +5 -0
  31. package/dist/runtime/components/prose/ProseTbody.vue +5 -0
  32. package/dist/runtime/components/prose/ProseTd.vue +5 -0
  33. package/dist/runtime/components/prose/ProseTh.vue +5 -0
  34. package/dist/runtime/components/prose/ProseThead.vue +5 -0
  35. package/dist/runtime/components/prose/ProseTr.vue +5 -0
  36. package/dist/runtime/components/prose/ProseUl.vue +5 -0
  37. package/dist/runtime/index.d.ts +3 -0
  38. package/dist/runtime/index.mjs +3 -0
  39. package/dist/runtime/parser/compiler.d.ts +1 -0
  40. package/dist/runtime/parser/compiler.mjs +78 -0
  41. package/dist/runtime/parser/handlers/code.d.ts +5 -0
  42. package/dist/runtime/parser/handlers/code.mjs +31 -0
  43. package/dist/runtime/parser/handlers/containerComponent.d.ts +10 -0
  44. package/dist/runtime/parser/handlers/containerComponent.mjs +15 -0
  45. package/dist/runtime/parser/handlers/emphasis.d.ts +6 -0
  46. package/dist/runtime/parser/handlers/emphasis.mjs +10 -0
  47. package/dist/runtime/parser/handlers/html.d.ts +3 -0
  48. package/dist/runtime/parser/handlers/html.mjs +14 -0
  49. package/dist/runtime/parser/handlers/image.d.ts +6 -0
  50. package/dist/runtime/parser/handlers/image.mjs +13 -0
  51. package/dist/runtime/parser/handlers/index.d.ts +22 -0
  52. package/dist/runtime/parser/handlers/index.mjs +22 -0
  53. package/dist/runtime/parser/handlers/inlineCode.d.ts +6 -0
  54. package/dist/runtime/parser/handlers/inlineCode.mjs +12 -0
  55. package/dist/runtime/parser/handlers/link.d.ts +6 -0
  56. package/dist/runtime/parser/handlers/link.mjs +18 -0
  57. package/dist/runtime/parser/handlers/list.d.ts +4 -0
  58. package/dist/runtime/parser/handlers/list.mjs +26 -0
  59. package/dist/runtime/parser/handlers/paragraph.d.ts +4 -0
  60. package/dist/runtime/parser/handlers/paragraph.mjs +19 -0
  61. package/dist/runtime/parser/handlers/strong.d.ts +6 -0
  62. package/dist/runtime/parser/handlers/strong.mjs +10 -0
  63. package/dist/runtime/parser/handlers/utils.d.ts +19 -0
  64. package/dist/runtime/parser/handlers/utils.mjs +33 -0
  65. package/dist/runtime/parser/index.d.ts +11 -0
  66. package/dist/runtime/parser/index.mjs +60 -0
  67. package/dist/runtime/parser/options.d.ts +2 -0
  68. package/dist/runtime/parser/options.mjs +50 -0
  69. package/dist/runtime/parser/shiki.d.ts +8 -0
  70. package/dist/runtime/parser/shiki.mjs +55 -0
  71. package/dist/runtime/parser/toc.d.ts +3 -0
  72. package/dist/runtime/parser/toc.mjs +59 -0
  73. package/dist/runtime/parser/utils/html-tags-list.d.ts +2 -0
  74. package/dist/runtime/parser/utils/html-tags-list.mjs +119 -0
  75. package/dist/runtime/parser/utils/plugins.d.ts +3 -0
  76. package/dist/runtime/parser/utils/plugins.mjs +10 -0
  77. package/dist/runtime/parser/utils/props.d.ts +5 -0
  78. package/dist/runtime/parser/utils/props.mjs +33 -0
  79. package/dist/runtime/shiki/event-handler.d.ts +8 -0
  80. package/dist/runtime/shiki/event-handler.mjs +15 -0
  81. package/dist/runtime/shiki/highlighter.d.ts +25 -0
  82. package/dist/runtime/shiki/highlighter.mjs +298 -0
  83. package/dist/runtime/shiki/mdc.tmLanguage.d.ts +323 -0
  84. package/dist/runtime/shiki/mdc.tmLanguage.mjs +582 -0
  85. package/dist/runtime/shiki/types.d.ts +32 -0
  86. package/dist/runtime/shiki/types.mjs +0 -0
  87. package/dist/runtime/types/hast.d.ts +255 -0
  88. package/dist/runtime/types/hast.mjs +0 -0
  89. package/dist/runtime/types/index.d.ts +3 -0
  90. package/dist/runtime/types/index.mjs +3 -0
  91. package/dist/runtime/types/parser.d.ts +30 -0
  92. package/dist/runtime/types/parser.mjs +0 -0
  93. package/dist/runtime/types/toc.d.ts +12 -0
  94. package/dist/runtime/types/toc.mjs +0 -0
  95. package/dist/runtime/types/tree.d.ts +17 -0
  96. package/dist/runtime/types/tree.mjs +0 -0
  97. package/dist/runtime/types/unist.d.ts +107 -0
  98. package/dist/runtime/types/unist.mjs +0 -0
  99. package/dist/runtime/utils/ast.d.ts +6 -0
  100. package/dist/runtime/utils/ast.mjs +29 -0
  101. package/dist/runtime/utils/node.d.ts +37 -0
  102. package/dist/runtime/utils/node.mjs +75 -0
  103. package/dist/runtime/utils/slot.d.ts +3 -0
  104. package/dist/runtime/utils/slot.mjs +8 -0
  105. package/dist/runtime/utils/ssrSlot.d.ts +1 -0
  106. package/dist/runtime/utils/ssrSlot.mjs +8 -0
  107. package/dist/types.d.mts +7 -0
  108. package/dist/types.d.ts +7 -0
  109. package/package.json +99 -0
@@ -0,0 +1,39 @@
1
+ <template>
2
+ <pre :class="$props.class"><slot /></pre>
3
+ </template>
4
+
5
+ <script setup lang="ts">
6
+ defineProps({
7
+ code: {
8
+ type: String,
9
+ default: ''
10
+ },
11
+ language: {
12
+ type: String,
13
+ default: null
14
+ },
15
+ filename: {
16
+ type: String,
17
+ default: null
18
+ },
19
+ highlights: {
20
+ type: Array as () => number[],
21
+ default: () => []
22
+ },
23
+ meta: {
24
+ type: String,
25
+ default: null
26
+ },
27
+ class: {
28
+ type: String,
29
+ default: null
30
+ }
31
+ })
32
+ </script>
33
+
34
+ <style>
35
+ pre code .line {
36
+ display: block;
37
+ min-height: 1rem;
38
+ }
39
+ </style>
@@ -0,0 +1,5 @@
1
+ <template>
2
+ <strong>
3
+ <slot />
4
+ </strong>
5
+ </template>
@@ -0,0 +1,5 @@
1
+ <template>
2
+ <table>
3
+ <slot />
4
+ </table>
5
+ </template>
@@ -0,0 +1,5 @@
1
+ <template>
2
+ <tbody>
3
+ <slot />
4
+ </tbody>
5
+ </template>
@@ -0,0 +1,5 @@
1
+ <template>
2
+ <td>
3
+ <slot />
4
+ </td>
5
+ </template>
@@ -0,0 +1,5 @@
1
+ <template>
2
+ <th>
3
+ <slot />
4
+ </th>
5
+ </template>
@@ -0,0 +1,5 @@
1
+ <template>
2
+ <thead>
3
+ <slot />
4
+ </thead>
5
+ </template>
@@ -0,0 +1,5 @@
1
+ <template>
2
+ <tr>
3
+ <slot />
4
+ </tr>
5
+ </template>
@@ -0,0 +1,5 @@
1
+ <template>
2
+ <ul>
3
+ <slot />
4
+ </ul>
5
+ </template>
@@ -0,0 +1,3 @@
1
+ export { parseMarkdown } from './parser';
2
+ export { useShikiHighlighter } from './shiki/highlighter';
3
+ export * from './utils/node';
@@ -0,0 +1,3 @@
1
+ export { parseMarkdown } from "./parser/index.mjs";
2
+ export { useShikiHighlighter } from "./shiki/highlighter.mjs";
3
+ export * from "./utils/node.mjs";
@@ -0,0 +1 @@
1
+ export declare function compileHast(this: any): void;
@@ -0,0 +1,78 @@
1
+ import { toString } from "hast-util-to-string";
2
+ import Slugger from "github-slugger";
3
+ import { validateProps } from "./utils/props.mjs";
4
+ export function compileHast() {
5
+ const slugs = new Slugger();
6
+ function compileToJSON(node) {
7
+ if (node.type === "root") {
8
+ return {
9
+ type: "root",
10
+ children: node.children.map(compileToJSON).filter(Boolean)
11
+ };
12
+ }
13
+ if (node.type === "element") {
14
+ if (node.tagName === "p" && node.children.every((child) => child.type === "text" && /^\s*$/.test(child.value))) {
15
+ return null;
16
+ }
17
+ if (node.tagName === "li") {
18
+ let hasPreviousParagraph = false;
19
+ node.children = node.children?.flatMap((child) => {
20
+ if (child.type === "element" && child.tagName === "p") {
21
+ if (hasPreviousParagraph) {
22
+ child.children.unshift({
23
+ type: "element",
24
+ tagName: "br",
25
+ properties: {},
26
+ children: []
27
+ });
28
+ }
29
+ hasPreviousParagraph = true;
30
+ return child.children;
31
+ }
32
+ return child;
33
+ });
34
+ }
35
+ if (node.tagName.startsWith("h")) {
36
+ node.properties = node.properties || {};
37
+ node.properties.id = String(node.properties?.id || slugs.slug(toString(node))).replace(/-+/g, "-").replace(/^-|-$/g, "").replace(/^(\d)/, "_$1");
38
+ }
39
+ if (node.tagName === "component-slot") {
40
+ node.tagName = "template";
41
+ }
42
+ return {
43
+ type: "element",
44
+ tag: node.tagName,
45
+ props: validateProps(node.properties),
46
+ children: node.children.map(compileToJSON).filter(Boolean)
47
+ };
48
+ }
49
+ if (node.type === "text" && node.value !== "\n") {
50
+ return {
51
+ type: "text",
52
+ value: node.value
53
+ };
54
+ }
55
+ return null;
56
+ }
57
+ this.Compiler = (tree) => {
58
+ const body = compileToJSON(tree);
59
+ let excerpt = void 0;
60
+ const excerptIndex = tree.children.findIndex((node) => node.type === "comment" && node.value?.trim() === "more");
61
+ if (excerptIndex !== -1) {
62
+ excerpt = compileToJSON({
63
+ type: "root",
64
+ children: tree.children.slice(0, excerptIndex)
65
+ });
66
+ if (excerpt.children.find((node) => node.type === "element" && node.tag === "pre")) {
67
+ const lastChild = body.children[body.children.length - 1];
68
+ if (lastChild.type === "element" && lastChild.tag === "style") {
69
+ excerpt.children.push(lastChild);
70
+ }
71
+ }
72
+ }
73
+ return {
74
+ body,
75
+ excerpt
76
+ };
77
+ };
78
+ }
@@ -0,0 +1,5 @@
1
+ import { type State } from 'mdast-util-to-hast';
2
+ import { type Element } from 'hast';
3
+ import { type Code } from 'mdast';
4
+ declare const _default: (state: State, node: Code) => Element;
5
+ export default _default;
@@ -0,0 +1,31 @@
1
+ import { detab } from "detab";
2
+ import { parseThematicBlock } from "./utils.mjs";
3
+ export default (state, node) => {
4
+ const lang = (node.lang || "") + " " + (node.meta || "");
5
+ const { language, highlights, filename, meta } = parseThematicBlock(lang);
6
+ const value = node.value ? detab(node.value + "\n") : "";
7
+ let result = {
8
+ type: "element",
9
+ tagName: "code",
10
+ properties: { __ignoreMap: "" },
11
+ children: [{ type: "text", value }]
12
+ };
13
+ if (node.meta) {
14
+ result.data = { meta: node.meta };
15
+ }
16
+ state.patch(node, result);
17
+ result = state.applyData(node, result);
18
+ const properties = {
19
+ language,
20
+ filename,
21
+ highlights,
22
+ meta,
23
+ code: value
24
+ };
25
+ if (node.lang) {
26
+ properties.className = ["language-" + node.lang];
27
+ }
28
+ result = { type: "element", tagName: "pre", properties, children: [result] };
29
+ state.patch(node, result);
30
+ return result;
31
+ };
@@ -0,0 +1,10 @@
1
+ import { type State } from 'mdast-util-to-hast';
2
+ import { type Element, type Properties } from 'hast';
3
+ import { type Nodes as MdastContent } from 'mdast';
4
+ type Node = MdastContent & {
5
+ name: string;
6
+ attributes?: Properties;
7
+ fmAttributes?: Properties;
8
+ };
9
+ export default function containerComponent(state: State, node: Node): Element;
10
+ export {};
@@ -0,0 +1,15 @@
1
+ export default function containerComponent(state, node) {
2
+ const result = {
3
+ type: "element",
4
+ tagName: node.name,
5
+ properties: {
6
+ ...node.attributes,
7
+ ...node.data?.hProperties
8
+ },
9
+ children: state.all(node)
10
+ };
11
+ state.patch(node, result);
12
+ result.attributes = node.attributes;
13
+ result.fmAttributes = node.fmAttributes;
14
+ return result;
15
+ }
@@ -0,0 +1,6 @@
1
+ import { type State } from 'mdast-util-to-hast';
2
+ import { type Element, type Properties } from 'hast';
3
+ import { type Emphasis } from 'mdast';
4
+ export default function emphasis(state: State, node: Emphasis & {
5
+ attributes?: Properties;
6
+ }): Element;
@@ -0,0 +1,10 @@
1
+ export default function emphasis(state, node) {
2
+ const result = {
3
+ type: "element",
4
+ tagName: "em",
5
+ properties: node.attributes || {},
6
+ children: state.all(node)
7
+ };
8
+ state.patch(node, result);
9
+ return state.applyData(node, result);
10
+ }
@@ -0,0 +1,3 @@
1
+ import { type State, type Raw } from 'mdast-util-to-hast';
2
+ import { type Html } from 'mdast';
3
+ export default function html(state: State, node: Html): import("hast").Element | Raw | undefined;
@@ -0,0 +1,14 @@
1
+ import { kebabCase } from "scule";
2
+ import { getTagName } from "./utils.mjs";
3
+ export default function html(state, node) {
4
+ const tagName = getTagName(node.value);
5
+ if (tagName && /[A-Z]/.test(tagName)) {
6
+ node.value = node.value.replace(tagName, kebabCase(tagName));
7
+ }
8
+ if (state.dangerous || state.options?.allowDangerousHtml) {
9
+ const result = { type: "raw", value: node.value };
10
+ state.patch(node, result);
11
+ return state.applyData(node, result);
12
+ }
13
+ return void 0;
14
+ }
@@ -0,0 +1,6 @@
1
+ import { type State } from 'mdast-util-to-hast';
2
+ import { type Element, type Properties } from 'hast';
3
+ import { type Image } from 'mdast';
4
+ export default function image(state: State, node: Image & {
5
+ attributes?: Properties;
6
+ }): Element;
@@ -0,0 +1,13 @@
1
+ import { normalizeUri } from "micromark-util-sanitize-uri";
2
+ export default function image(state, node) {
3
+ const properties = { ...node.attributes, src: normalizeUri(node.url) };
4
+ if (node.alt !== null && node.alt !== void 0) {
5
+ properties.alt = node.alt;
6
+ }
7
+ if (node.title !== null && node.title !== void 0) {
8
+ properties.title = node.title;
9
+ }
10
+ const result = { type: "element", tagName: "img", properties, children: [] };
11
+ state.patch(node, result);
12
+ return state.applyData(node, result);
13
+ }
@@ -0,0 +1,22 @@
1
+ import emphasis from './emphasis';
2
+ import html from './html';
3
+ import link from './link';
4
+ import list from './list';
5
+ import paragraph from './paragraph';
6
+ import image from './image';
7
+ import strong from './strong';
8
+ import inlineCode from './inlineCode';
9
+ import containerComponent from './containerComponent';
10
+ declare const _default: {
11
+ emphasis: typeof emphasis;
12
+ code: (state: import("mdast-util-to-hast").State, node: import("mdast").Code) => import("hast").Element;
13
+ link: typeof link;
14
+ paragraph: typeof paragraph;
15
+ html: typeof html;
16
+ list: typeof list;
17
+ image: typeof image;
18
+ strong: typeof strong;
19
+ inlineCode: typeof inlineCode;
20
+ containerComponent: typeof containerComponent;
21
+ };
22
+ export default _default;
@@ -0,0 +1,22 @@
1
+ import emphasis from "./emphasis.mjs";
2
+ import code from "./code.mjs";
3
+ import html from "./html.mjs";
4
+ import link from "./link.mjs";
5
+ import list from "./list.mjs";
6
+ import paragraph from "./paragraph.mjs";
7
+ import image from "./image.mjs";
8
+ import strong from "./strong.mjs";
9
+ import inlineCode from "./inlineCode.mjs";
10
+ import containerComponent from "./containerComponent.mjs";
11
+ export default {
12
+ emphasis,
13
+ code,
14
+ link,
15
+ paragraph,
16
+ html,
17
+ list,
18
+ image,
19
+ strong,
20
+ inlineCode,
21
+ containerComponent
22
+ };
@@ -0,0 +1,6 @@
1
+ import { type State } from 'mdast-util-to-hast';
2
+ import { type Element, type Properties } from 'hast';
3
+ import { type InlineCode } from 'mdast';
4
+ export default function inlineCode(state: State, node: InlineCode & {
5
+ attributes?: Properties;
6
+ }): Element;
@@ -0,0 +1,12 @@
1
+ export default function inlineCode(state, node) {
2
+ const text = { type: "text", value: node.value.replace(/\r?\n|\r/g, " ") };
3
+ state.patch(node, text);
4
+ const result = {
5
+ type: "element",
6
+ tagName: "code",
7
+ properties: node.attributes || {},
8
+ children: [text]
9
+ };
10
+ state.patch(node, result);
11
+ return state.applyData(node, result);
12
+ }
@@ -0,0 +1,6 @@
1
+ import { type State } from 'mdast-util-to-hast';
2
+ import { type Element, type Properties } from 'hast';
3
+ import { type Link } from 'mdast';
4
+ export default function link(state: State, node: Link & {
5
+ attributes?: Properties;
6
+ }): Element;
@@ -0,0 +1,18 @@
1
+ import { normalizeUri } from "micromark-util-sanitize-uri";
2
+ export default function link(state, node) {
3
+ const properties = {
4
+ ...node.attributes || {},
5
+ href: normalizeUri(node.url)
6
+ };
7
+ if (node.title !== null && node.title !== void 0) {
8
+ properties.title = node.title;
9
+ }
10
+ const result = {
11
+ type: "element",
12
+ tagName: "a",
13
+ properties,
14
+ children: state.all(node)
15
+ };
16
+ state.patch(node, result);
17
+ return state.applyData(node, result);
18
+ }
@@ -0,0 +1,4 @@
1
+ import { type State } from 'mdast-util-to-hast';
2
+ import { type Element } from 'hast';
3
+ import { type List } from 'mdast';
4
+ export default function list(state: State, node: List): Element;
@@ -0,0 +1,26 @@
1
+ export default function list(state, node) {
2
+ const properties = {};
3
+ const results = state.all(node);
4
+ let index = -1;
5
+ if (typeof node.start === "number" && node.start !== 1) {
6
+ properties.start = node.start;
7
+ }
8
+ while (++index < results.length) {
9
+ const child = results[index];
10
+ if (child.type === "element" && child.tagName === "li" && child.properties && Array.isArray(child.properties.className) && child.properties.className.includes("task-list-item")) {
11
+ properties.className = ["contains-task-list"];
12
+ break;
13
+ }
14
+ }
15
+ if ((node.children || []).some((child) => typeof child.checked === "boolean")) {
16
+ properties.className = ["contains-task-list"];
17
+ }
18
+ const result = {
19
+ type: "element",
20
+ tagName: node.ordered ? "ol" : "ul",
21
+ properties,
22
+ children: state.wrap(results, true)
23
+ };
24
+ state.patch(node, result);
25
+ return state.applyData(node, result);
26
+ }
@@ -0,0 +1,4 @@
1
+ import { type State } from 'mdast-util-to-hast';
2
+ import { type Element } from 'hast';
3
+ import { type Paragraph } from 'mdast';
4
+ export default function paragraph(state: State, node: Paragraph): Element | import("hast").ElementContent[];
@@ -0,0 +1,19 @@
1
+ import { kebabCase } from "scule";
2
+ import htmlTags from "../utils/html-tags-list.mjs";
3
+ import { getTagName } from "./utils.mjs";
4
+ export default function paragraph(state, node) {
5
+ if (node.children && node.children[0] && node.children[0].type === "html") {
6
+ const tagName = kebabCase(getTagName(node.children[0].value) || "div");
7
+ if (!htmlTags.includes(tagName)) {
8
+ return state.all(node);
9
+ }
10
+ }
11
+ const result = {
12
+ type: "element",
13
+ tagName: "p",
14
+ properties: {},
15
+ children: state.all(node)
16
+ };
17
+ state.patch(node, result);
18
+ return state.applyData(node, result);
19
+ }
@@ -0,0 +1,6 @@
1
+ import { type State } from 'mdast-util-to-hast';
2
+ import { type Element, type Properties } from 'hast';
3
+ import { type Strong } from 'mdast';
4
+ export default function strong(state: State, node: Strong & {
5
+ attributes?: Properties;
6
+ }): Element;
@@ -0,0 +1,10 @@
1
+ export default function strong(state, node) {
2
+ const result = {
3
+ type: "element",
4
+ tagName: "strong",
5
+ properties: node.attributes || {},
6
+ children: state.all(node)
7
+ };
8
+ state.patch(node, result);
9
+ return state.applyData(node, result);
10
+ }
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Parses the value defined next to 3 back ticks
3
+ * in a codeblock and set line-highlights or
4
+ * filename from it
5
+ */
6
+ export declare function parseThematicBlock(lang: string): {
7
+ language: undefined;
8
+ highlights: undefined;
9
+ fileName: undefined;
10
+ meta: undefined;
11
+ filename?: undefined;
12
+ } | {
13
+ language: string | undefined;
14
+ highlights: number[] | undefined;
15
+ filename: string | undefined;
16
+ meta: string;
17
+ fileName?: undefined;
18
+ };
19
+ export declare function getTagName(value: string): string | null;
@@ -0,0 +1,33 @@
1
+ export function parseThematicBlock(lang) {
2
+ if (!lang?.trim()) {
3
+ return {
4
+ language: void 0,
5
+ highlights: void 0,
6
+ fileName: void 0,
7
+ meta: void 0
8
+ };
9
+ }
10
+ const languageMatches = lang.replace(/[{|[](.+)/, "").match(/^[^ \t]+(?=[ \t]|$)/);
11
+ const highlightTokensMatches = lang.match(/{([^}]*)}/);
12
+ const filenameMatches = lang.match(/\[((\\]|[^\]])*)\]/);
13
+ const meta = lang.replace(languageMatches?.[0] ?? "", "").replace(highlightTokensMatches?.[0] ?? "", "").replace(filenameMatches?.[0] ?? "", "").trim();
14
+ return {
15
+ language: languageMatches?.[0] || void 0,
16
+ highlights: parseHighlightedLines(highlightTokensMatches?.[1] || void 0),
17
+ // https://github.com/nuxt/content/pull/2169
18
+ filename: filenameMatches?.[1].replace(/\\]/g, "]") || void 0,
19
+ meta
20
+ };
21
+ }
22
+ function parseHighlightedLines(lines) {
23
+ const lineArray = String(lines || "").split(",").filter(Boolean).flatMap((line) => {
24
+ const [start, end] = line.trim().split("-").map((a) => Number(a.trim()));
25
+ return Array.from({ length: (end || start) - start + 1 }).map((_, i) => start + i);
26
+ });
27
+ return lineArray.length ? lineArray : void 0;
28
+ }
29
+ const TAG_NAME_REGEXP = /^<\/?([A-Za-z0-9-_]+) ?[^>]*>/;
30
+ export function getTagName(value) {
31
+ const result = String(value).match(TAG_NAME_REGEXP);
32
+ return result && result[1];
33
+ }
@@ -0,0 +1,11 @@
1
+ import type { MDCData, MDCParseOptions, MDCRoot, Toc } from '../types';
2
+ export declare const parseMarkdown: (md: string, opts?: MDCParseOptions) => Promise<{
3
+ data: MDCData;
4
+ body: MDCRoot;
5
+ excerpt: MDCRoot | undefined;
6
+ toc: Toc | undefined;
7
+ }>;
8
+ export declare function contentHeading(body: MDCRoot): {
9
+ title: string;
10
+ description: string;
11
+ };
@@ -0,0 +1,60 @@
1
+ import { unified } from "unified";
2
+ import remarkParse from "remark-parse";
3
+ import remark2rehype from "remark-rehype";
4
+ import remarkMDC, { parseFrontMatter } from "remark-mdc";
5
+ import { defu } from "defu";
6
+ import { useProcessorPlugins } from "./utils/plugins.mjs";
7
+ import { compileHast } from "./compiler.mjs";
8
+ import { defaults } from "./options.mjs";
9
+ import { rehypeShiki } from "./shiki.mjs";
10
+ import { generateToc } from "./toc.mjs";
11
+ import { nodeTextContent } from "../utils/node.mjs";
12
+ export const parseMarkdown = async (md, opts = {}) => {
13
+ const options = defu(opts, defaults);
14
+ const { content, data: frontmatter } = await parseFrontMatter(md);
15
+ const processor = unified();
16
+ processor.use(remarkParse);
17
+ processor.use(remarkMDC);
18
+ await useProcessorPlugins(processor, options.remark?.plugins);
19
+ processor.use(remark2rehype, options.rehype?.options);
20
+ if (options.highlight) {
21
+ processor.use(rehypeShiki, options.highlight);
22
+ }
23
+ await useProcessorPlugins(processor, options.rehype?.plugins);
24
+ processor.use(compileHast);
25
+ const processedFile = await processor.process({ value: content, data: frontmatter });
26
+ const result = processedFile.result;
27
+ const data = Object.assign(
28
+ contentHeading(result.body),
29
+ frontmatter,
30
+ processedFile?.data || {}
31
+ );
32
+ let toc;
33
+ if (data.toc !== false) {
34
+ const tocOption = defu(data.toc || {}, options.toc);
35
+ toc = generateToc(result.body, tocOption);
36
+ }
37
+ return {
38
+ data,
39
+ body: result.body,
40
+ excerpt: result.excerpt,
41
+ toc
42
+ };
43
+ };
44
+ export function contentHeading(body) {
45
+ let title = "";
46
+ let description = "";
47
+ const children = body.children.filter((node) => node.type !== "text" && node.tag !== "hr");
48
+ if (children.length && children[0].tag === "h1") {
49
+ const node = children.shift();
50
+ title = nodeTextContent(node);
51
+ }
52
+ if (children.length && children[0].tag === "p") {
53
+ const node = children.shift();
54
+ description = nodeTextContent(node);
55
+ }
56
+ return {
57
+ title,
58
+ description
59
+ };
60
+ }
@@ -0,0 +1,2 @@
1
+ import type { MDCParseOptions } from '../types';
2
+ export declare const defaults: MDCParseOptions;