@ubean/markdown 0.1.13 → 0.2.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.
- package/dist/index-bC6p6XCn.d.ts +89 -0
- package/dist/index.d.ts +3 -45
- package/dist/index.js +2 -145
- package/dist/jsx-runtime.d.ts +13 -0
- package/dist/jsx-runtime.js +32 -0
- package/dist/mdx.d.ts +2 -0
- package/dist/mdx.js +2 -0
- package/dist/src-B81snjFd.js +388 -0
- package/dist/vite-plugin-DIL6Ty-2.d.ts +10535 -0
- package/dist/vite-plugin.d.ts +2 -0
- package/dist/vite-plugin.js +2 -0
- package/package.json +22 -3
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import "./vite-plugin-DIL6Ty-2.js";
|
|
2
|
+
//#region src/mdx.d.ts
|
|
3
|
+
interface MdxOptions {
|
|
4
|
+
/** File path (for error reporting and relative imports). */
|
|
5
|
+
filePath?: string;
|
|
6
|
+
/** Additional remark plugins. */
|
|
7
|
+
remarkPlugins?: any[];
|
|
8
|
+
/** Additional rehype plugins. */
|
|
9
|
+
rehypePlugins?: any[];
|
|
10
|
+
/** Whether to output development or production code. */
|
|
11
|
+
development?: boolean;
|
|
12
|
+
}
|
|
13
|
+
interface MdxCompileResult {
|
|
14
|
+
/** Compiled JavaScript module string. */
|
|
15
|
+
code: string;
|
|
16
|
+
/** Parsed frontmatter. */
|
|
17
|
+
frontmatter: MarkdownFrontmatter;
|
|
18
|
+
/** Whether MDX compiler was available (true) or fell back to plain markdown. */
|
|
19
|
+
compiled: boolean;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Check if `@mdx-js/mdx` is available for real MDX compilation.
|
|
23
|
+
*/
|
|
24
|
+
declare function isMdxAvailable(): Promise<boolean>;
|
|
25
|
+
/**
|
|
26
|
+
* Compile MDX source to a Vue component JavaScript module.
|
|
27
|
+
*
|
|
28
|
+
* Uses `@mdx-js/mdx` to compile the MDX body to a JS module that imports
|
|
29
|
+
* from `@ubean/markdown/jsx-runtime` (which maps to Vue's `h()` function).
|
|
30
|
+
*
|
|
31
|
+
* If `@mdx-js/mdx` is not installed, falls back to plain markdown rendering
|
|
32
|
+
* using `markdown-exit`, wrapping the HTML output in a Vue component.
|
|
33
|
+
*
|
|
34
|
+
* @param source Raw MDX source string (with optional frontmatter)
|
|
35
|
+
* @param options Compilation options
|
|
36
|
+
* @returns Compilation result with code, frontmatter, and compiled flag
|
|
37
|
+
*/
|
|
38
|
+
declare function compileMdx(source: string, options?: MdxOptions): Promise<MdxCompileResult>;
|
|
39
|
+
/**
|
|
40
|
+
* Synchronously check if MDX compilation is available (without dynamic import).
|
|
41
|
+
* Uses the cached result from a previous `compileMdx()` or `isMdxAvailable()` call.
|
|
42
|
+
*/
|
|
43
|
+
declare function isMdxAvailableSync(): boolean;
|
|
44
|
+
//#endregion
|
|
45
|
+
//#region src/index.d.ts
|
|
46
|
+
interface MarkdownFrontmatter {
|
|
47
|
+
title?: string;
|
|
48
|
+
description?: string;
|
|
49
|
+
date?: string;
|
|
50
|
+
layout?: string | false;
|
|
51
|
+
path?: string;
|
|
52
|
+
seo?: Record<string, unknown>;
|
|
53
|
+
meta?: Record<string, unknown>;
|
|
54
|
+
head?: Record<string, unknown>;
|
|
55
|
+
[key: string]: unknown;
|
|
56
|
+
}
|
|
57
|
+
interface ParsedMarkdown {
|
|
58
|
+
frontmatter: MarkdownFrontmatter;
|
|
59
|
+
content: string;
|
|
60
|
+
excerpt?: string;
|
|
61
|
+
headings: MarkdownHeading[];
|
|
62
|
+
html?: string;
|
|
63
|
+
}
|
|
64
|
+
interface MarkdownHeading {
|
|
65
|
+
level: number;
|
|
66
|
+
text: string;
|
|
67
|
+
id: string;
|
|
68
|
+
}
|
|
69
|
+
interface MarkdownOptions {
|
|
70
|
+
html?: boolean;
|
|
71
|
+
linkify?: boolean;
|
|
72
|
+
breaks?: boolean;
|
|
73
|
+
typographer?: boolean;
|
|
74
|
+
excerpt?: boolean;
|
|
75
|
+
excerptSeparator?: string;
|
|
76
|
+
headingIds?: boolean;
|
|
77
|
+
highlighter?: (code: string, lang: string) => string;
|
|
78
|
+
}
|
|
79
|
+
declare function parseFrontmatter(source: string): {
|
|
80
|
+
data: MarkdownFrontmatter;
|
|
81
|
+
content: string;
|
|
82
|
+
};
|
|
83
|
+
declare function markdownToHtml(markdown: string, options?: MarkdownOptions): string;
|
|
84
|
+
declare function extractHeadings(markdown: string): MarkdownHeading[];
|
|
85
|
+
declare function extractExcerpt(markdown: string, separator?: string): string | undefined;
|
|
86
|
+
declare function parseMarkdown(source: string, options?: MarkdownOptions): ParsedMarkdown;
|
|
87
|
+
declare function defineMarkdownPage(frontmatter: MarkdownFrontmatter & Record<string, unknown>): MarkdownFrontmatter & Record<string, unknown>;
|
|
88
|
+
//#endregion
|
|
89
|
+
export { defineMarkdownPage as a, markdownToHtml as c, MdxCompileResult as d, MdxOptions as f, isMdxAvailableSync as h, ParsedMarkdown as i, parseFrontmatter as l, isMdxAvailable as m, MarkdownHeading as n, extractExcerpt as o, compileMdx as p, MarkdownOptions as r, extractHeadings as s, MarkdownFrontmatter as t, parseMarkdown as u };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,45 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
description?: string;
|
|
5
|
-
date?: string;
|
|
6
|
-
layout?: string | false;
|
|
7
|
-
path?: string;
|
|
8
|
-
seo?: Record<string, unknown>;
|
|
9
|
-
meta?: Record<string, unknown>;
|
|
10
|
-
head?: Record<string, unknown>;
|
|
11
|
-
[key: string]: unknown;
|
|
12
|
-
}
|
|
13
|
-
interface ParsedMarkdown {
|
|
14
|
-
frontmatter: MarkdownFrontmatter;
|
|
15
|
-
content: string;
|
|
16
|
-
excerpt?: string;
|
|
17
|
-
headings: MarkdownHeading[];
|
|
18
|
-
html?: string;
|
|
19
|
-
}
|
|
20
|
-
interface MarkdownHeading {
|
|
21
|
-
level: number;
|
|
22
|
-
text: string;
|
|
23
|
-
id: string;
|
|
24
|
-
}
|
|
25
|
-
interface MarkdownOptions {
|
|
26
|
-
html?: boolean;
|
|
27
|
-
linkify?: boolean;
|
|
28
|
-
breaks?: boolean;
|
|
29
|
-
typographer?: boolean;
|
|
30
|
-
excerpt?: boolean;
|
|
31
|
-
excerptSeparator?: string;
|
|
32
|
-
headingIds?: boolean;
|
|
33
|
-
highlighter?: (code: string, lang: string) => string;
|
|
34
|
-
}
|
|
35
|
-
declare function parseFrontmatter(source: string): {
|
|
36
|
-
data: MarkdownFrontmatter;
|
|
37
|
-
content: string;
|
|
38
|
-
};
|
|
39
|
-
declare function markdownToHtml(markdown: string, options?: MarkdownOptions): string;
|
|
40
|
-
declare function extractHeadings(markdown: string): MarkdownHeading[];
|
|
41
|
-
declare function extractExcerpt(markdown: string, separator?: string): string | undefined;
|
|
42
|
-
declare function parseMarkdown(source: string, options?: MarkdownOptions): ParsedMarkdown;
|
|
43
|
-
declare function defineMarkdownPage(frontmatter: MarkdownFrontmatter & Record<string, unknown>): MarkdownFrontmatter & Record<string, unknown>;
|
|
44
|
-
//#endregion
|
|
45
|
-
export { MarkdownFrontmatter, MarkdownHeading, MarkdownOptions, ParsedMarkdown, defineMarkdownPage, extractExcerpt, extractHeadings, markdownToHtml, parseFrontmatter, parseMarkdown };
|
|
1
|
+
import { a as defineMarkdownPage, c as markdownToHtml, d as MdxCompileResult, f as MdxOptions, h as isMdxAvailableSync, i as ParsedMarkdown, l as parseFrontmatter, m as isMdxAvailable, n as MarkdownHeading, o as extractExcerpt, p as compileMdx, r as MarkdownOptions, s as extractHeadings, t as MarkdownFrontmatter, u as parseMarkdown } from "./index-bC6p6XCn.js";
|
|
2
|
+
import { n as ubeanMdxPlugin, t as MdxVitePluginOptions } from "./vite-plugin-DIL6Ty-2.js";
|
|
3
|
+
export { MarkdownFrontmatter, MarkdownHeading, MarkdownOptions, type MdxCompileResult, type MdxOptions, type MdxVitePluginOptions, ParsedMarkdown, compileMdx, defineMarkdownPage, extractExcerpt, extractHeadings, isMdxAvailable, isMdxAvailableSync, markdownToHtml, parseFrontmatter, parseMarkdown, ubeanMdxPlugin };
|
package/dist/index.js
CHANGED
|
@@ -1,145 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
const FRONTMATTER_REGEX = /^---\s*\n([\s\S]*?)\n---\s*\n?/;
|
|
4
|
-
function parseFrontmatter(source) {
|
|
5
|
-
const match = source.match(FRONTMATTER_REGEX);
|
|
6
|
-
if (!match) return {
|
|
7
|
-
data: {},
|
|
8
|
-
content: source
|
|
9
|
-
};
|
|
10
|
-
const raw = match[1];
|
|
11
|
-
const content = source.slice(match[0].length);
|
|
12
|
-
return {
|
|
13
|
-
data: parseYamlSimple(raw),
|
|
14
|
-
content
|
|
15
|
-
};
|
|
16
|
-
}
|
|
17
|
-
function parseYamlSimple(yaml) {
|
|
18
|
-
const result = {};
|
|
19
|
-
const lines = yaml.split("\n");
|
|
20
|
-
for (const line of lines) {
|
|
21
|
-
const trimmed = line.trimEnd();
|
|
22
|
-
if (!trimmed || trimmed.startsWith("#")) continue;
|
|
23
|
-
const colonIdx = trimmed.indexOf(":");
|
|
24
|
-
if (colonIdx === -1) continue;
|
|
25
|
-
const key = trimmed.slice(0, colonIdx).trim();
|
|
26
|
-
let value = trimmed.slice(colonIdx + 1).trim();
|
|
27
|
-
if (!key) continue;
|
|
28
|
-
if (value === "") value = true;
|
|
29
|
-
else if (value === "true" || value === "false") value = value === "true";
|
|
30
|
-
else if (value === "null" || value === "~") value = null;
|
|
31
|
-
else if (!isNaN(Number(value)) && value !== "") {
|
|
32
|
-
const num = Number(value);
|
|
33
|
-
if (isFinite(num)) value = num;
|
|
34
|
-
} else if (typeof value === "string" && (value.startsWith("\"") && value.endsWith("\"") || value.startsWith("'") && value.endsWith("'"))) value = value.slice(1, -1);
|
|
35
|
-
else if (typeof value === "string" && value.startsWith("[") && value.endsWith("]")) value = value.slice(1, -1).split(",").map((v) => v.trim()).filter((v) => v !== "").map((v) => {
|
|
36
|
-
if (v === "true") return true;
|
|
37
|
-
if (v === "false") return false;
|
|
38
|
-
if (v === "null") return null;
|
|
39
|
-
if (!isNaN(Number(v))) return Number(v);
|
|
40
|
-
return v.replace(/^["']|["']$/g, "");
|
|
41
|
-
});
|
|
42
|
-
const nestedKeys = key.split(".");
|
|
43
|
-
let current = result;
|
|
44
|
-
for (let i = 0; i < nestedKeys.length - 1; i++) {
|
|
45
|
-
const k = nestedKeys[i];
|
|
46
|
-
if (!(k in current) || typeof current[k] !== "object") current[k] = {};
|
|
47
|
-
current = current[k];
|
|
48
|
-
}
|
|
49
|
-
current[nestedKeys[nestedKeys.length - 1]] = value;
|
|
50
|
-
}
|
|
51
|
-
return result;
|
|
52
|
-
}
|
|
53
|
-
function slugify(text) {
|
|
54
|
-
return text.toLowerCase().replace(/<[^>]*>/g, "").replace(/[^\w\s\u4e00-\u9fa5-]/g, "").replace(/\s+/g, "-").replace(/-+/g, "-").replace(/^-|-$/g, "");
|
|
55
|
-
}
|
|
56
|
-
function applyHeadingIds(md) {
|
|
57
|
-
const headingOpenRule = md.renderer.rules.heading_open;
|
|
58
|
-
md.renderer.rules.heading_open = (tokens, idx, options, env, self) => {
|
|
59
|
-
const token = tokens[idx];
|
|
60
|
-
const nextToken = tokens[idx + 1];
|
|
61
|
-
if (nextToken && nextToken.type === "inline") {
|
|
62
|
-
const text = nextToken.children?.filter((t) => t.type === "text" || t.type === "code_inline").map((t) => t.content).join("") || "";
|
|
63
|
-
token.attrSet("id", slugify(text));
|
|
64
|
-
}
|
|
65
|
-
if (headingOpenRule) return headingOpenRule(tokens, idx, options, env, self);
|
|
66
|
-
return self.renderToken(tokens, idx, options);
|
|
67
|
-
};
|
|
68
|
-
}
|
|
69
|
-
function createInstance(options) {
|
|
70
|
-
const md = createMarkdownExit({
|
|
71
|
-
html: options.html ?? false,
|
|
72
|
-
linkify: options.linkify ?? true,
|
|
73
|
-
breaks: options.breaks ?? false,
|
|
74
|
-
typographer: options.typographer ?? false,
|
|
75
|
-
highlight: options.highlighter ? (str, lang) => {
|
|
76
|
-
const result = options.highlighter(str.trimEnd(), lang);
|
|
77
|
-
if (result.startsWith("<pre")) return result;
|
|
78
|
-
return `<pre><code class="language-${lang}">${result}</code></pre>`;
|
|
79
|
-
} : void 0
|
|
80
|
-
});
|
|
81
|
-
if (options.headingIds !== false) applyHeadingIds(md);
|
|
82
|
-
return md;
|
|
83
|
-
}
|
|
84
|
-
function markdownToHtml(markdown, options = {}) {
|
|
85
|
-
return createInstance(options).render(markdown).trim();
|
|
86
|
-
}
|
|
87
|
-
function extractHeadings(markdown) {
|
|
88
|
-
const headings = [];
|
|
89
|
-
const tokens = createMarkdownExit({ html: false }).parse(markdown, {});
|
|
90
|
-
function walkTokenList(tokenList) {
|
|
91
|
-
for (let i = 0; i < tokenList.length; i++) {
|
|
92
|
-
const token = tokenList[i];
|
|
93
|
-
if (token.type === "heading_open") {
|
|
94
|
-
const level = parseInt(token.tag.slice(1), 10);
|
|
95
|
-
const inlineToken = tokenList[i + 1];
|
|
96
|
-
if (inlineToken && inlineToken.type === "inline") {
|
|
97
|
-
const text = extractTextFromTokens(inlineToken.children || []);
|
|
98
|
-
headings.push({
|
|
99
|
-
level,
|
|
100
|
-
text,
|
|
101
|
-
id: slugify(text)
|
|
102
|
-
});
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
if (token.children) walkTokenList(token.children);
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
walkTokenList(tokens);
|
|
109
|
-
return headings;
|
|
110
|
-
}
|
|
111
|
-
function extractTextFromTokens(tokens) {
|
|
112
|
-
let text = "";
|
|
113
|
-
for (const token of tokens) if (token.type === "text" || token.type === "code_inline") text += token.content;
|
|
114
|
-
else if (token.children) text += extractTextFromTokens(token.children);
|
|
115
|
-
return text;
|
|
116
|
-
}
|
|
117
|
-
function extractExcerpt(markdown, separator = "<!-- more -->") {
|
|
118
|
-
const idx = markdown.indexOf(separator);
|
|
119
|
-
if (idx !== -1) return markdown.slice(0, idx).trim();
|
|
120
|
-
const paragraphs = markdown.split("\n\n").filter((p) => p.trim() !== "");
|
|
121
|
-
for (const para of paragraphs) {
|
|
122
|
-
const trimmed = para.trim();
|
|
123
|
-
if (trimmed.startsWith("#")) continue;
|
|
124
|
-
if (trimmed.startsWith("```")) continue;
|
|
125
|
-
if (trimmed.startsWith(">")) continue;
|
|
126
|
-
if (trimmed.startsWith("- ") || trimmed.startsWith("* ") || /^\d+\.\s/.test(trimmed)) continue;
|
|
127
|
-
if (trimmed.length < 500) return trimmed;
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
function parseMarkdown(source, options = {}) {
|
|
131
|
-
const { data: frontmatter, content } = parseFrontmatter(source);
|
|
132
|
-
const headings = extractHeadings(content);
|
|
133
|
-
return {
|
|
134
|
-
frontmatter,
|
|
135
|
-
content,
|
|
136
|
-
excerpt: options.excerpt !== false ? extractExcerpt(content, options.excerptSeparator) : void 0,
|
|
137
|
-
headings,
|
|
138
|
-
html: markdownToHtml(content, options)
|
|
139
|
-
};
|
|
140
|
-
}
|
|
141
|
-
function defineMarkdownPage(frontmatter) {
|
|
142
|
-
return frontmatter;
|
|
143
|
-
}
|
|
144
|
-
//#endregion
|
|
145
|
-
export { defineMarkdownPage, extractExcerpt, extractHeadings, markdownToHtml, parseFrontmatter, parseMarkdown };
|
|
1
|
+
import { a as parseFrontmatter, c as compileMdx, i as markdownToHtml, l as isMdxAvailable, n as extractExcerpt, o as parseMarkdown, r as extractHeadings, s as ubeanMdxPlugin, t as defineMarkdownPage, u as isMdxAvailableSync } from "./src-B81snjFd.js";
|
|
2
|
+
export { compileMdx, defineMarkdownPage, extractExcerpt, extractHeadings, isMdxAvailable, isMdxAvailableSync, markdownToHtml, parseFrontmatter, parseMarkdown, ubeanMdxPlugin };
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Component, Fragment, VNode } from "vue";
|
|
2
|
+
//#region src/jsx-runtime.d.ts
|
|
3
|
+
/**
|
|
4
|
+
* Create a Vue VNode from a JSX-like call.
|
|
5
|
+
* Maps MDX's `jsx(type, props)` to Vue's `h(type, props)`.
|
|
6
|
+
*/
|
|
7
|
+
declare function jsx(type: string | Component, props: Record<string, any> | null, key?: string | number): VNode;
|
|
8
|
+
/**
|
|
9
|
+
* Create a Vue VNode with multiple children (same as jsx for Vue).
|
|
10
|
+
*/
|
|
11
|
+
declare function jsxs(type: string | Component, props: Record<string, any> | null, key?: string | number): VNode;
|
|
12
|
+
//#endregion
|
|
13
|
+
export { Fragment, jsx, jsxs };
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { Fragment, h } from "vue";
|
|
2
|
+
//#region src/jsx-runtime.ts
|
|
3
|
+
/**
|
|
4
|
+
* P9-20: Vue JSX runtime for MDX compilation.
|
|
5
|
+
*
|
|
6
|
+
* MDX compiles to JavaScript modules that use a JSX runtime
|
|
7
|
+
* (`jsx()`, `jsxs()`, `Fragment`). This module provides a Vue-compatible
|
|
8
|
+
* runtime that maps these calls to Vue's `h()` function, allowing MDX
|
|
9
|
+
* content to render as Vue components.
|
|
10
|
+
*
|
|
11
|
+
* When `@mdx-js/mdx` compiles MDX with `jsxImportSource: '@ubean/markdown/jsx-runtime'`,
|
|
12
|
+
* the output imports from this module.
|
|
13
|
+
*/
|
|
14
|
+
/**
|
|
15
|
+
* Create a Vue VNode from a JSX-like call.
|
|
16
|
+
* Maps MDX's `jsx(type, props)` to Vue's `h(type, props)`.
|
|
17
|
+
*/
|
|
18
|
+
function jsx(type, props, key) {
|
|
19
|
+
const { children, ...rest } = props || {};
|
|
20
|
+
const vueProps = { ...rest };
|
|
21
|
+
if (key !== void 0) vueProps.key = key;
|
|
22
|
+
if (children !== void 0) return h(type, vueProps, children);
|
|
23
|
+
return h(type, vueProps);
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Create a Vue VNode with multiple children (same as jsx for Vue).
|
|
27
|
+
*/
|
|
28
|
+
function jsxs(type, props, key) {
|
|
29
|
+
return jsx(type, props, key);
|
|
30
|
+
}
|
|
31
|
+
//#endregion
|
|
32
|
+
export { Fragment, jsx, jsxs };
|
package/dist/mdx.d.ts
ADDED
package/dist/mdx.js
ADDED