vuepress-plugin-md-power 1.0.0-rc.125 → 1.0.0-rc.127

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/lib/node/index.js CHANGED
@@ -212,7 +212,7 @@ async function resolveImageSize(app, url, remote = false) {
212
212
 
213
213
  // src/node/plugin.ts
214
214
  import { addViteOptimizeDepsInclude } from "@vuepress/helper";
215
- import { isPackageExists as isPackageExists2 } from "local-pkg";
215
+ import { isPackageExists as isPackageExists3 } from "local-pkg";
216
216
 
217
217
  // src/node/container/index.ts
218
218
  import { isPlainObject as isPlainObject2 } from "@vuepress/helper";
@@ -352,6 +352,7 @@ var definitions = {
352
352
  "CSS": "vscode-icons:file-type-css",
353
353
  "less": "vscode-icons:file-type-less",
354
354
  "Less": "vscode-icons:file-type-less",
355
+ "LESS": "vscode-icons:file-type-less",
355
356
  "scss": "vscode-icons:file-type-scss",
356
357
  "Scss": "vscode-icons:file-type-scss",
357
358
  "SCSS": "vscode-icons:file-type-scss",
@@ -1824,6 +1825,686 @@ async function containerPlugin(app, md, options) {
1824
1825
  }
1825
1826
  }
1826
1827
 
1828
+ // src/node/demo/demo.ts
1829
+ import container5 from "markdown-it-container";
1830
+
1831
+ // src/node/embed/createEmbedRuleBlock.ts
1832
+ function createEmbedRuleBlock(md, {
1833
+ type: type2,
1834
+ name = type2,
1835
+ syntaxPattern,
1836
+ beforeName = "import_code",
1837
+ ruleOptions = { alt: ["paragraph", "reference", "blockquote", "list"] },
1838
+ meta,
1839
+ content
1840
+ }) {
1841
+ const MIN_LENGTH = type2.length + 5;
1842
+ const START_CODES = [64, 91, ...type2.split("").map((c) => c.charCodeAt(0))];
1843
+ md.block.ruler.before(
1844
+ beforeName,
1845
+ name,
1846
+ (state, startLine, endLine, silent) => {
1847
+ const pos = state.bMarks[startLine] + state.tShift[startLine];
1848
+ const max = state.eMarks[startLine];
1849
+ if (pos + MIN_LENGTH > max)
1850
+ return false;
1851
+ for (let i = 0; i < START_CODES.length; i += 1) {
1852
+ if (state.src.charCodeAt(pos + i) !== START_CODES[i])
1853
+ return false;
1854
+ }
1855
+ const content2 = state.src.slice(pos, max);
1856
+ const match = content2.match(syntaxPattern);
1857
+ if (!match)
1858
+ return false;
1859
+ if (silent)
1860
+ return true;
1861
+ const token = state.push(name, "", 0);
1862
+ token.meta = meta(match);
1863
+ token.content = content2;
1864
+ token.map = [startLine, startLine + 1];
1865
+ state.line = startLine + 1;
1866
+ return true;
1867
+ },
1868
+ ruleOptions
1869
+ );
1870
+ md.renderer.rules[name] = (tokens, index, _, env) => {
1871
+ const token = tokens[index];
1872
+ token.content = content(token.meta, token.content, env);
1873
+ return token.content;
1874
+ };
1875
+ }
1876
+
1877
+ // src/node/demo/supports/file.ts
1878
+ import fs3 from "node:fs";
1879
+ import { createRequire } from "node:module";
1880
+ import path3 from "node:path";
1881
+ import process from "node:process";
1882
+ var require2 = createRequire(process.cwd());
1883
+ function findFile(app, env, url) {
1884
+ if (url.startsWith("/"))
1885
+ return app.dir.source(url.slice(1));
1886
+ if (url.startsWith("./") || url.startsWith("../"))
1887
+ return app.dir.source(path3.dirname(env.filePathRelative), url);
1888
+ if (url.startsWith("@source/")) {
1889
+ return app.dir.source(url.slice("@source/".length));
1890
+ }
1891
+ try {
1892
+ return require2.resolve(url);
1893
+ } catch {
1894
+ return url;
1895
+ }
1896
+ }
1897
+ function readFileSync(filepath2) {
1898
+ try {
1899
+ return fs3.readFileSync(filepath2, "utf-8");
1900
+ } catch {
1901
+ return false;
1902
+ }
1903
+ }
1904
+ function writeFileSync(filepath2, content) {
1905
+ const dirname = path3.dirname(filepath2);
1906
+ fs3.mkdirSync(dirname, { recursive: true });
1907
+ fs3.writeFileSync(filepath2, content, "utf-8");
1908
+ }
1909
+
1910
+ // src/node/demo/markdown.ts
1911
+ function markdownEmbed(app, md, env, { url, title, desc, codeSetting = "", expanded = false }) {
1912
+ const filepath2 = findFile(app, env, url);
1913
+ const code = readFileSync(filepath2);
1914
+ if (code === false) {
1915
+ console.warn("[vuepress-plugin-md-power] Cannot read markdown file:", filepath2);
1916
+ return "";
1917
+ }
1918
+ const demo = { type: "markdown", path: filepath2 };
1919
+ env.demoFiles ??= [];
1920
+ if (!env.demoFiles.some((d) => d.path === filepath2)) {
1921
+ env.demoFiles.push(demo);
1922
+ }
1923
+ return `<VPDemoBasic type="markdown"${title ? ` title="${title}"` : ""}${desc ? ` desc="${desc}"` : ""}${expanded ? " expanded" : ""}>
1924
+ ${md.render(code, { filepath: env.filePath, filepathRelative: env.filePathRelative })}
1925
+ <template #code>
1926
+ ${md.render(`\`\`\`md ${codeSetting}
1927
+ ${code}
1928
+ \`\`\``, {})}
1929
+ </template>
1930
+ </VPDemoBasic>`;
1931
+ }
1932
+ var markdownContainerRender = {
1933
+ before(app, md, env, meta, codeMap) {
1934
+ const { title, desc, expanded = false } = meta;
1935
+ const code = codeMap.md || "";
1936
+ return `<VPDemoBasic type="markdown"${title ? ` title="${title}"` : ""}${desc ? ` desc="${desc}"` : ""}${expanded ? " expanded" : ""}>
1937
+ ${md.render(code, { filepath: env.filePath, filepathRelative: env.filePathRelative })}
1938
+ <template #code>`;
1939
+ },
1940
+ after: () => "</template></VPDemoBasic>"
1941
+ };
1942
+
1943
+ // src/node/demo/normal.ts
1944
+ import fs5 from "node:fs";
1945
+ import path4 from "node:path";
1946
+
1947
+ // src/node/demo/supports/compiler.ts
1948
+ import { isPackageExists } from "local-pkg";
1949
+ import { LRUCache } from "lru-cache";
1950
+
1951
+ // src/node/utils/package.ts
1952
+ async function interopDefault(m) {
1953
+ const resolved = await m;
1954
+ return resolved.default || resolved;
1955
+ }
1956
+
1957
+ // src/node/demo/supports/compiler.ts
1958
+ var cache2 = new LRUCache({ max: 64 });
1959
+ var compiler = {
1960
+ script: importer(async () => {
1961
+ const { transform } = await import("esbuild");
1962
+ return transform;
1963
+ }),
1964
+ less: importer(() => import("less")),
1965
+ sass: importer(async () => {
1966
+ if (isPackageExists("sass-embedded")) {
1967
+ return await import("sass-embedded");
1968
+ }
1969
+ return await import("sass");
1970
+ }),
1971
+ stylus: importer(() => import("stylus"))
1972
+ };
1973
+ async function compileScript(source, type2) {
1974
+ const key = `${type2}:::${source}`;
1975
+ if (cache2.has(key))
1976
+ return cache2.get(key);
1977
+ const transform = await compiler.script();
1978
+ const res = await transform(source, {
1979
+ target: "es2018",
1980
+ format: "cjs",
1981
+ loader: type2 === "ts" ? "ts" : "js",
1982
+ sourcemap: false
1983
+ });
1984
+ cache2.set(key, res.code);
1985
+ return res.code;
1986
+ }
1987
+ async function compileStyle(source, type2) {
1988
+ const key = `${type2}:::${source}`;
1989
+ if (cache2.has(key))
1990
+ return cache2.get(key);
1991
+ if (type2 === "css")
1992
+ return source;
1993
+ if (type2 === "less") {
1994
+ const less = await compiler.less();
1995
+ const res = await less.render(source);
1996
+ cache2.set(key, res.css);
1997
+ return res.css;
1998
+ }
1999
+ if (type2 === "scss") {
2000
+ const sass = await compiler.sass();
2001
+ const res = sass.compileString(source);
2002
+ cache2.set(key, res.css);
2003
+ return res.css;
2004
+ }
2005
+ if (type2 === "stylus") {
2006
+ const stylus = await compiler.stylus();
2007
+ const res = stylus.render(source);
2008
+ cache2.set(key, res);
2009
+ return res;
2010
+ }
2011
+ return source;
2012
+ }
2013
+ function importer(func) {
2014
+ let imported;
2015
+ return async () => {
2016
+ if (!imported) {
2017
+ imported = interopDefault(await func());
2018
+ }
2019
+ return imported;
2020
+ };
2021
+ }
2022
+
2023
+ // src/node/demo/supports/insertScript.ts
2024
+ var SCRIPT_RE = /<script.*?>/;
2025
+ function insertSetupScript({ export: name, path: path8 }, env) {
2026
+ const imports = `import ${name ? `${name} from ` : ""}'${path8}';`;
2027
+ const scriptSetup = env.sfcBlocks.scriptSetup ??= {
2028
+ type: "script",
2029
+ content: "<script setup>\n</script>",
2030
+ contentStripped: "",
2031
+ tagOpen: "<script setup>",
2032
+ tagClose: "</script>"
2033
+ };
2034
+ scriptSetup.contentStripped = `${imports}
2035
+ ${scriptSetup.contentStripped}`;
2036
+ scriptSetup.content = scriptSetup.content.replace(SCRIPT_RE, (matched) => `${matched}
2037
+ ${imports}`);
2038
+ }
2039
+
2040
+ // src/node/demo/watcher.ts
2041
+ import fs4 from "node:fs";
2042
+ import { watch } from "chokidar";
2043
+ var renderDone = null;
2044
+ var renderCount = 0;
2045
+ var renderPromise;
2046
+ function createDemoRender() {
2047
+ renderPromise = new Promise((resolve) => {
2048
+ renderDone = resolve;
2049
+ });
2050
+ }
2051
+ async function waitDemoRender() {
2052
+ if (renderCount === 0) {
2053
+ renderDone?.();
2054
+ renderDone = null;
2055
+ }
2056
+ await renderPromise;
2057
+ }
2058
+ function markDemoRender() {
2059
+ renderCount++;
2060
+ }
2061
+ function checkDemoRender() {
2062
+ if (renderCount > 0) {
2063
+ renderCount--;
2064
+ }
2065
+ if (renderCount === 0) {
2066
+ renderDone?.();
2067
+ renderDone = null;
2068
+ }
2069
+ }
2070
+ var watcher = null;
2071
+ var tasks = {};
2072
+ var target = "md-power/demo/watcher.txt";
2073
+ function demoWatcher(app, watchers) {
2074
+ if (!watcher) {
2075
+ watcher = watch([], { ignoreInitial: true });
2076
+ }
2077
+ Object.keys(tasks).forEach((path8) => {
2078
+ watcher.add(path8);
2079
+ });
2080
+ const code = readFileSync(app.dir.temp(target));
2081
+ if (code) {
2082
+ const paths = JSON.parse(code || "{}");
2083
+ Object.entries(paths).forEach(([path8, output]) => {
2084
+ watcher.add(path8);
2085
+ tasks[path8] = output;
2086
+ });
2087
+ }
2088
+ updateWatchFiles(app);
2089
+ watcher.on("change", (path8) => {
2090
+ if (tasks[path8]) {
2091
+ const code2 = readFileSync(path8);
2092
+ if (code2 === false)
2093
+ return;
2094
+ const source = parseEmbedCode(code2);
2095
+ compileCode(source, tasks[path8]);
2096
+ }
2097
+ });
2098
+ watcher.on("unlink", (path8) => {
2099
+ delete tasks[path8];
2100
+ watcher.unwatch(path8);
2101
+ });
2102
+ watchers.push(() => {
2103
+ watcher.close();
2104
+ watcher = null;
2105
+ });
2106
+ }
2107
+ function addTask(app, path8, output) {
2108
+ if (tasks[path8])
2109
+ return;
2110
+ tasks[path8] = output;
2111
+ if (watcher) {
2112
+ watcher.add(path8);
2113
+ }
2114
+ updateWatchFiles(app);
2115
+ }
2116
+ async function updateWatchFiles(app) {
2117
+ await fs4.promises.writeFile(app.dir.temp(target), JSON.stringify(tasks));
2118
+ }
2119
+
2120
+ // src/node/demo/normal.ts
2121
+ var CONFIG_RE = /<script type="config">([\s\S]*?)\n<\/script>/;
2122
+ var SCRIPT_RE2 = /<script\s?(?:lang="(\w+)")?>([\s\S]*?)\n<\/script>/;
2123
+ var STYLE_RE = /<style\s?(?:lang="(\w+)")?>([\s\S]*?)\n<\/style>/;
2124
+ var scriptSupported = ["ts", "js"];
2125
+ var styleSupported = ["css", "scss", "less", "stylus", "styl"];
2126
+ var target2 = "md-power/demo/normal";
2127
+ var FENCE = "```";
2128
+ function parseEmbedCode(code) {
2129
+ const res = { html: "", css: "", script: "", imports: "", jsType: "js", cssType: "css" };
2130
+ res.html = code.replace(CONFIG_RE, (_, config) => {
2131
+ res.imports = config;
2132
+ return "";
2133
+ }).replace(SCRIPT_RE2, (_, lang, script) => {
2134
+ res.script = script;
2135
+ res.jsType = scriptSupported.includes(lang) ? lang : "js";
2136
+ return "";
2137
+ }).replace(STYLE_RE, (_, lang, style) => {
2138
+ res.css = style;
2139
+ res.cssType = styleSupported.includes(lang) ? lang === "styl" ? "stylus" : lang : "css";
2140
+ return "";
2141
+ });
2142
+ return res;
2143
+ }
2144
+ function codeToHtml(md, source, info) {
2145
+ let content = "::: code-tabs\n";
2146
+ if (source.html)
2147
+ content += `@tab HTML
2148
+ ${FENCE}html ${info}
2149
+ ${source.html.trim()}
2150
+ ${FENCE}
2151
+ `;
2152
+ if (source.script) {
2153
+ const title = source.jsType === "ts" ? "Typescript" : "Javascript";
2154
+ content += `@tab ${title}
2155
+ ${FENCE}${source.jsType} ${info}
2156
+ ${source.script.trim()}
2157
+ ${FENCE}
2158
+ `;
2159
+ }
2160
+ if (source.css) {
2161
+ const title = source.cssType === "stylus" ? "Stylus" : source.cssType.toUpperCase();
2162
+ content += `@tab ${title}
2163
+ ${FENCE}${source.cssType} ${info}
2164
+ ${source.css.trim()}
2165
+ ${FENCE}
2166
+ `;
2167
+ }
2168
+ content += "\n:::";
2169
+ return md.render(content, {});
2170
+ }
2171
+ async function compileCode(code, output) {
2172
+ markDemoRender();
2173
+ const res = { jsLib: [], cssLib: [], script: "", css: "", html: "" };
2174
+ if (!fs5.existsSync(output))
2175
+ writeFileSync(output, `import { ref } from "vue"
2176
+ export default ref(${JSON.stringify(res, null, 2)})`);
2177
+ try {
2178
+ if (code.imports) {
2179
+ const imports = JSON.parse(code.imports);
2180
+ res.jsLib = imports.jsLib ?? [];
2181
+ res.cssLib = imports.cssLib ?? [];
2182
+ }
2183
+ if (code.script) {
2184
+ res.script = await compileScript(code.script.trim(), code.jsType);
2185
+ }
2186
+ if (code.css) {
2187
+ res.css = await compileStyle(code.css.trim(), code.cssType);
2188
+ }
2189
+ if (code.html) {
2190
+ res.html = code.html.trim();
2191
+ }
2192
+ } catch (e) {
2193
+ console.error("[vuepress-plugin-md-power] demo parse error: \n", e);
2194
+ }
2195
+ writeFileSync(output, `import { ref } from "vue"
2196
+ export default ref(${JSON.stringify(res, null, 2)})`);
2197
+ checkDemoRender();
2198
+ }
2199
+ function normalEmbed(app, md, env, { url, title, desc, codeSetting = "", expanded = false }) {
2200
+ const filepath2 = findFile(app, env, url);
2201
+ const code = readFileSync(filepath2);
2202
+ if (code === false) {
2203
+ console.warn("[vuepress-plugin-md-power] Cannot read demo file:", filepath2);
2204
+ return "";
2205
+ }
2206
+ const source = parseEmbedCode(code);
2207
+ const prefix = (env.filePathRelative || "").replace(/\.md$/, "").replace(/\//g, "-");
2208
+ const basename = path4.basename(filepath2).replace(/-|\./g, "_");
2209
+ const name = `Demo${basename[0].toUpperCase()}${basename.slice(1)}`;
2210
+ const demo = { type: "normal", export: name, path: filepath2 };
2211
+ const output = app.dir.temp(target2, `${prefix}-${name}.js`);
2212
+ compileCode(source, output);
2213
+ addTask(app, filepath2, output);
2214
+ env.demoFiles ??= [];
2215
+ if (!env.demoFiles.some((d) => d.path === filepath2)) {
2216
+ env.demoFiles.push(demo);
2217
+ insertSetupScript({ ...demo, path: output }, env);
2218
+ }
2219
+ return `<VPDemoNormal :config="${name}"${title ? ` title="${title}"` : ""}${desc ? ` desc="${desc}"` : ""}${expanded ? " expanded" : ""}>
2220
+ ${codeToHtml(md, source, codeSetting)}
2221
+ </VPDemoNormal>`;
2222
+ }
2223
+ var normalContainerRender = {
2224
+ before(app, md, env, meta, codeMap) {
2225
+ const { url, title, desc, expanded = false } = meta;
2226
+ const name = `DemoContainer${url}`;
2227
+ const prefix = (env.filePathRelative || "").replace(/\.md$/, "").replace(/\//g, "-");
2228
+ const output = app.dir.temp(path4.join(target2, `${prefix}-${name}.js`));
2229
+ env.demoFiles ??= [];
2230
+ if (!env.demoFiles.some((d) => d.path === output)) {
2231
+ const demo = { type: "normal", export: name, gitignore: true, path: output };
2232
+ env.demoFiles.push(demo);
2233
+ insertSetupScript(demo, env);
2234
+ }
2235
+ const source = parseContainerCode(codeMap);
2236
+ compileCode(source, output);
2237
+ return `<VPDemoNormal :config="${name}"${title ? ` title="${title}"` : ""}${desc ? ` desc="${desc}"` : ""}${expanded ? " expanded" : ""}>`;
2238
+ },
2239
+ after: () => "</VPDemoNormal>",
2240
+ token(token) {
2241
+ if (token.info.trim().startsWith("json")) {
2242
+ token.hidden = true;
2243
+ token.type = "text";
2244
+ token.content = "";
2245
+ }
2246
+ }
2247
+ };
2248
+ function parseContainerCode(map) {
2249
+ const res = { html: "", css: "", script: "", imports: "", jsType: "js", cssType: "css" };
2250
+ if (map.html) {
2251
+ res.html = map.html;
2252
+ }
2253
+ if (map.js) {
2254
+ res.script = map.js;
2255
+ res.jsType = "js";
2256
+ }
2257
+ if (map.ts) {
2258
+ res.script = map.ts;
2259
+ res.jsType = "ts";
2260
+ }
2261
+ if (map.css) {
2262
+ res.css = map.css;
2263
+ res.cssType = "css";
2264
+ }
2265
+ if (map.less) {
2266
+ res.css = map.less;
2267
+ res.cssType = "less";
2268
+ }
2269
+ if (map.scss) {
2270
+ res.css = map.scss;
2271
+ res.cssType = "scss";
2272
+ }
2273
+ if (map.styl || map.stylus) {
2274
+ res.css = map.styl || map.stylus;
2275
+ res.cssType = "stylus";
2276
+ }
2277
+ if (map.json) {
2278
+ res.imports = map.json;
2279
+ }
2280
+ return res;
2281
+ }
2282
+
2283
+ // src/node/demo/supports/alias.ts
2284
+ function normalizeAlias(info) {
2285
+ const [lang] = info.trim().split(/\s+|:|\{/);
2286
+ switch (lang) {
2287
+ case "vue":
2288
+ return "vue";
2289
+ case "js":
2290
+ case "javascript":
2291
+ return "js";
2292
+ case "ts":
2293
+ case "typescript":
2294
+ return "ts";
2295
+ case "stylus":
2296
+ case "styl":
2297
+ return "stylus";
2298
+ case "md":
2299
+ case "markdown":
2300
+ return "md";
2301
+ }
2302
+ return lang;
2303
+ }
2304
+
2305
+ // src/node/demo/vue.ts
2306
+ import path5 from "node:path";
2307
+ function vueEmbed(app, md, env, { url, title, desc, codeSetting = "", expanded = false }) {
2308
+ const filepath2 = findFile(app, env, url);
2309
+ const code = readFileSync(filepath2);
2310
+ if (code === false) {
2311
+ console.warn("[vuepress-plugin-md-power] Cannot read vue file:", filepath2);
2312
+ return "";
2313
+ }
2314
+ const basename = path5.basename(filepath2).replace(/-|\./g, "_");
2315
+ const ext = path5.extname(filepath2).slice(1);
2316
+ const name = `Demo${basename[0].toUpperCase()}${basename.slice(1)}`;
2317
+ const demo = { type: "vue", export: name, path: filepath2 };
2318
+ env.demoFiles ??= [];
2319
+ if (!env.demoFiles.some((d) => d.path === filepath2)) {
2320
+ env.demoFiles.push(demo);
2321
+ insertSetupScript(demo, env);
2322
+ }
2323
+ return `<VPDemoBasic type="vue"${title ? ` title="${title}"` : ""}${desc ? ` desc="${desc}"` : ""}${expanded ? " expanded" : ""}>
2324
+ <${name} />
2325
+ <template #code>
2326
+ ${md.render(`\`\`\`${ext}${codeSetting}
2327
+ ${code}
2328
+ \`\`\``, {})}
2329
+ </template>
2330
+ </VPDemoBasic>`;
2331
+ }
2332
+ var target3 = "md-power/demo/vue";
2333
+ var vueContainerRender = {
2334
+ before: (app, md, env, meta, codeMap) => {
2335
+ const { url, title, desc, expanded = false } = meta;
2336
+ const componentName2 = `DemoContainer${url}`;
2337
+ const prefix = (env.filePathRelative || "").replace(/\.md$/, "").replace(/\//g, "-");
2338
+ env.demoFiles ??= [];
2339
+ const output = app.dir.temp(path5.join(target3, `${prefix}-${componentName2}`));
2340
+ if (codeMap.vue || codeMap.js || codeMap.ts) {
2341
+ let scriptOutput = output;
2342
+ let content = "";
2343
+ if (codeMap.vue) {
2344
+ scriptOutput += ".vue";
2345
+ content = transformStyle(codeMap.vue);
2346
+ } else if (codeMap.ts) {
2347
+ scriptOutput += ".ts";
2348
+ content = codeMap.ts;
2349
+ } else if (codeMap.js) {
2350
+ scriptOutput += ".js";
2351
+ content = codeMap.js;
2352
+ }
2353
+ content = transformImports(content, env.filePath || "");
2354
+ const script = { type: "vue", export: componentName2, path: scriptOutput, gitignore: true };
2355
+ writeFileSync(scriptOutput, content);
2356
+ if (!env.demoFiles.some((d) => d.path === scriptOutput)) {
2357
+ env.demoFiles.push(script);
2358
+ insertSetupScript(script, env);
2359
+ }
2360
+ }
2361
+ if (codeMap.css || codeMap.scss || codeMap.less || codeMap.stylus) {
2362
+ let styleOutput = output;
2363
+ let content = "";
2364
+ if (codeMap.css) {
2365
+ styleOutput += ".css";
2366
+ content = codeMap.css;
2367
+ } else if (codeMap.scss) {
2368
+ styleOutput += ".scss";
2369
+ content = codeMap.scss;
2370
+ } else if (codeMap.less) {
2371
+ styleOutput += ".less";
2372
+ content = codeMap.less;
2373
+ } else if (codeMap.stylus) {
2374
+ styleOutput += ".styl";
2375
+ content = codeMap.stylus;
2376
+ }
2377
+ writeFileSync(styleOutput, content);
2378
+ const style = { type: "css", path: styleOutput, gitignore: true };
2379
+ if (!env.demoFiles.some((d) => d.path === styleOutput)) {
2380
+ env.demoFiles.push(style);
2381
+ insertSetupScript(style, env);
2382
+ }
2383
+ }
2384
+ return `<VPDemoBasic${title ? ` title="${title}"` : ""}${desc ? ` desc="${desc}"` : ""}${expanded ? " expanded" : ""}>
2385
+ <${componentName2} />
2386
+ <template #code>
2387
+ `;
2388
+ },
2389
+ after: () => "</template></VPDemoBasic>"
2390
+ };
2391
+ var IMPORT_RE = /import\s+(?:\w+\s+from\s+)?['"]([^'"]+)['"]/g;
2392
+ var STYLE_RE2 = /<style.*?>/;
2393
+ function transformImports(code, filepath2) {
2394
+ return code.replace(IMPORT_RE, (matched, url) => {
2395
+ if (url.startsWith("./") || url.startsWith("../")) {
2396
+ return matched.replace(url, `${path5.resolve(path5.dirname(filepath2), url)}`);
2397
+ }
2398
+ return matched;
2399
+ });
2400
+ }
2401
+ function transformStyle(code) {
2402
+ return code.replace(STYLE_RE2, (matched) => {
2403
+ if (matched.includes("scoped")) {
2404
+ return matched;
2405
+ }
2406
+ return matched.replace("<style", "<style scoped");
2407
+ });
2408
+ }
2409
+
2410
+ // src/node/demo/demo.ts
2411
+ function demoEmbed(app, md) {
2412
+ createEmbedRuleBlock(md, {
2413
+ type: "demo",
2414
+ syntaxPattern: /^@\[demo(?:\s(vue|normal|markdown))?\s?(.*)\]\((.*)\)/,
2415
+ meta: ([, type2, info, url]) => ({
2416
+ type: type2 || "normal",
2417
+ url,
2418
+ ...resolveAttrs(info).attrs
2419
+ }),
2420
+ content: (meta, content, env) => {
2421
+ const { url, type: type2 } = meta;
2422
+ if (!url) {
2423
+ console.warn("[vuepress-plugin-md-power] Invalid demo url: ", url);
2424
+ return content;
2425
+ }
2426
+ if (type2 === "vue") {
2427
+ return vueEmbed(app, md, env, meta);
2428
+ }
2429
+ if (type2 === "normal") {
2430
+ return normalEmbed(app, md, env, meta);
2431
+ }
2432
+ if (type2 === "markdown") {
2433
+ return markdownEmbed(app, md, env, meta);
2434
+ }
2435
+ return content;
2436
+ }
2437
+ });
2438
+ }
2439
+ var INFO_RE = /(vue|normal|markdown)?\s?(.*)/;
2440
+ var renderMap = {
2441
+ vue: vueContainerRender,
2442
+ normal: normalContainerRender,
2443
+ markdown: markdownContainerRender
2444
+ };
2445
+ function demoContainer(app, md) {
2446
+ let currentRender;
2447
+ const render = (tokens, index, _, env) => {
2448
+ const token = tokens[index];
2449
+ if (token.nesting === 1) {
2450
+ const meta = getContainerMeta(token.info);
2451
+ meta.url = `${index}`;
2452
+ currentRender = renderMap[meta.type];
2453
+ return currentRender?.before(
2454
+ app,
2455
+ md,
2456
+ env,
2457
+ meta,
2458
+ parseCodeMapping(tokens, index, currentRender.token)
2459
+ ) || "";
2460
+ } else {
2461
+ const res = currentRender?.after() || "";
2462
+ currentRender = void 0;
2463
+ return res;
2464
+ }
2465
+ };
2466
+ md.use(container5, "demo", { render });
2467
+ }
2468
+ function parseCodeMapping(tokens, index, cb) {
2469
+ const codeMap = {};
2470
+ for (let i = index + 1; !(tokens[i].nesting === -1 && tokens[i].type === "container_demo_close"); ++i) {
2471
+ const token = tokens[i];
2472
+ if (token.type === "fence") {
2473
+ codeMap[normalizeAlias(token.info)] = token.content.trim();
2474
+ cb?.(token, tokens, i);
2475
+ }
2476
+ }
2477
+ return codeMap;
2478
+ }
2479
+ function getContainerMeta(info) {
2480
+ const [, type2, raw] = (info.trim().slice(4).trim() || "").match(INFO_RE) || [];
2481
+ const { attrs: attrs2 } = resolveAttrs(raw);
2482
+ return {
2483
+ url: "",
2484
+ type: type2 || "normal",
2485
+ ...attrs2
2486
+ };
2487
+ }
2488
+
2489
+ // src/node/demo/extendPage.ts
2490
+ function extendsPageWithDemo(page) {
2491
+ const markdownEnv = page.markdownEnv;
2492
+ const demoFiles = markdownEnv.demoFiles ?? [];
2493
+ page.deps.push(
2494
+ ...demoFiles.filter(({ type: type2 }) => type2 === "markdown").map(({ path: path8 }) => path8)
2495
+ );
2496
+ (page.frontmatter.gitInclude ??= []).push(
2497
+ ...demoFiles.filter(({ gitignore }) => !gitignore).map(({ path: path8 }) => path8)
2498
+ );
2499
+ }
2500
+
2501
+ // src/node/demo/index.ts
2502
+ function demoPlugin(app, md) {
2503
+ createDemoRender();
2504
+ demoEmbed(app, md);
2505
+ demoContainer(app, md);
2506
+ }
2507
+
1827
2508
  // src/node/embed/audio/reader.ts
1828
2509
  var audioReader = (state, silent) => {
1829
2510
  const max = state.posMax;
@@ -1886,56 +2567,12 @@ var audioReader = (state, silent) => {
1886
2567
  var audioReaderPlugin = (md) => md.inline.ruler.before("link", "audio-reader", audioReader);
1887
2568
 
1888
2569
  // src/node/embed/caniuse.ts
1889
- import container5 from "markdown-it-container";
2570
+ import container6 from "markdown-it-container";
1890
2571
 
1891
2572
  // src/node/utils/nanoid.ts
1892
2573
  import { customAlphabet } from "nanoid";
1893
2574
  var nanoid = customAlphabet("abcdefghijklmnopqrstuvwxyz", 5);
1894
2575
 
1895
- // src/node/embed/createEmbedRuleBlock.ts
1896
- function createEmbedRuleBlock(md, {
1897
- type: type2,
1898
- name = type2,
1899
- syntaxPattern,
1900
- beforeName = "import_code",
1901
- ruleOptions = { alt: ["paragraph", "reference", "blockquote", "list"] },
1902
- meta,
1903
- content
1904
- }) {
1905
- const MIN_LENGTH = type2.length + 5;
1906
- const START_CODES = [64, 91, ...type2.split("").map((c) => c.charCodeAt(0))];
1907
- md.block.ruler.before(
1908
- beforeName,
1909
- name,
1910
- (state, startLine, endLine, silent) => {
1911
- const pos = state.bMarks[startLine] + state.tShift[startLine];
1912
- const max = state.eMarks[startLine];
1913
- if (pos + MIN_LENGTH > max)
1914
- return false;
1915
- for (let i = 0; i < START_CODES.length; i += 1) {
1916
- if (state.src.charCodeAt(pos + i) !== START_CODES[i])
1917
- return false;
1918
- }
1919
- const match = state.src.slice(pos, max).match(syntaxPattern);
1920
- if (!match)
1921
- return false;
1922
- if (silent)
1923
- return true;
1924
- const token = state.push(name, "", 0);
1925
- token.meta = meta(match);
1926
- token.map = [startLine, startLine + 1];
1927
- state.line = startLine + 1;
1928
- return true;
1929
- },
1930
- ruleOptions
1931
- );
1932
- md.renderer.rules[name] = (tokens, index) => {
1933
- const token = tokens[index];
1934
- token.content = content(token.meta);
1935
- return token.content;
1936
- };
1937
- }
1938
-
1939
2576
  // src/node/embed/caniuse.ts
1940
2577
  var UNDERLINE_RE = /_+/g;
1941
2578
  var caniusePlugin = (md, { mode: defaultMode = "embed" } = {}) => {
@@ -1970,7 +2607,7 @@ function legacyCaniuse(md, { mode = "embed" } = {}) {
1970
2607
  return "";
1971
2608
  }
1972
2609
  };
1973
- md.use(container5, type2, { validate, render });
2610
+ md.use(container6, type2, { validate, render });
1974
2611
  }
1975
2612
  function resolveCanIUse({ feature, mode, versions }) {
1976
2613
  if (!feature)
@@ -2124,7 +2761,7 @@ var replitPlugin = (md) => {
2124
2761
  };
2125
2762
 
2126
2763
  // src/node/embed/pdf.ts
2127
- import { path as path3 } from "vuepress/utils";
2764
+ import { path as path6 } from "vuepress/utils";
2128
2765
  var pdfPlugin = (md) => {
2129
2766
  createEmbedRuleBlock(md, {
2130
2767
  type: "pdf",
@@ -2140,7 +2777,7 @@ var pdfPlugin = (md) => {
2140
2777
  width: attrs2.width ? parseRect(attrs2.width) : "100%",
2141
2778
  height: attrs2.height ? parseRect(attrs2.height) : "",
2142
2779
  ratio: attrs2.ratio ? parseRect(attrs2.ratio) : "",
2143
- title: path3.basename(src || "")
2780
+ title: path6.basename(src || "")
2144
2781
  };
2145
2782
  },
2146
2783
  content({ title, src, page, noToolbar, width, height, ratio, zoom }) {
@@ -2150,12 +2787,12 @@ var pdfPlugin = (md) => {
2150
2787
  };
2151
2788
 
2152
2789
  // src/node/embed/video/artPlayer.ts
2153
- import { isPackageExists } from "local-pkg";
2790
+ import { isPackageExists as isPackageExists2 } from "local-pkg";
2154
2791
  import { colors } from "vuepress/utils";
2155
2792
  var installed = {
2156
- dashjs: isPackageExists("dashjs"),
2157
- hlsjs: isPackageExists("hls.js"),
2158
- mpegtsjs: isPackageExists("mpegts.js")
2793
+ dashjs: isPackageExists2("dashjs"),
2794
+ hlsjs: isPackageExists2("hls.js"),
2795
+ mpegtsjs: isPackageExists2("mpegts.js")
2159
2796
  };
2160
2797
  var SUPPORTED_VIDEO_TYPES = ["mp4", "mp3", "webm", "ogg", "mpd", "dash", "m3u8", "hls", "ts", "flv"];
2161
2798
  var artPlayerPlugin = (md) => {
@@ -2512,11 +3149,11 @@ function inlineSyntaxPlugin(md, options) {
2512
3149
 
2513
3150
  // src/node/prepareConfigFile.ts
2514
3151
  import { ensureEndingSlash } from "@vuepress/helper";
2515
- import { getDirname, path as path4 } from "vuepress/utils";
3152
+ import { getDirname, path as path7 } from "vuepress/utils";
2516
3153
  var { url: filepath } = import.meta;
2517
3154
  var __dirname = getDirname(filepath);
2518
3155
  var CLIENT_FOLDER = ensureEndingSlash(
2519
- path4.resolve(__dirname, "../client")
3156
+ path7.resolve(__dirname, "../client")
2520
3157
  );
2521
3158
  async function prepareConfigFile(app, options) {
2522
3159
  const imports = /* @__PURE__ */ new Set();
@@ -2569,6 +3206,12 @@ async function prepareConfigFile(app, options) {
2569
3206
  imports.add(`import AudioReader from '${CLIENT_FOLDER}components/AudioReader.vue'`);
2570
3207
  enhances.add(`app.component('AudioReader', AudioReader)`);
2571
3208
  }
3209
+ if (options.demo) {
3210
+ imports.add(`import VPDemoBasic from '${CLIENT_FOLDER}components/VPDemoBasic.vue'`);
3211
+ imports.add(`import VPDemoNormal from '${CLIENT_FOLDER}components/VPDemoNormal.vue'`);
3212
+ enhances.add(`app.component('VPDemoBasic', VPDemoBasic)`);
3213
+ enhances.add(`app.component('VPDemoNormal', VPDemoNormal)`);
3214
+ }
2572
3215
  return app.writeTemp(
2573
3216
  "md-power/config.js",
2574
3217
  `import { defineClientConfig } from 'vuepress/client'
@@ -2592,9 +3235,9 @@ function markdownPowerPlugin(options = {}) {
2592
3235
  clientConfigFile: (app) => prepareConfigFile(app, options),
2593
3236
  define: {
2594
3237
  __MD_POWER_INJECT_OPTIONS__: options,
2595
- __MD_POWER_DASHJS_INSTALLED__: isPackageExists2("dashjs"),
2596
- __MD_POWER_HLSJS_INSTALLED__: isPackageExists2("hls.js"),
2597
- __MD_POWER_MPEGTSJS_INSTALLED__: isPackageExists2("mpegts.js")
3238
+ __MD_POWER_DASHJS_INSTALLED__: isPackageExists3("dashjs"),
3239
+ __MD_POWER_HLSJS_INSTALLED__: isPackageExists3("hls.js"),
3240
+ __MD_POWER_MPEGTSJS_INSTALLED__: isPackageExists3("mpegts.js")
2598
3241
  },
2599
3242
  extendsBundlerOptions(bundlerOptions, app) {
2600
3243
  if (options.repl) {
@@ -2616,8 +3259,23 @@ function markdownPowerPlugin(options = {}) {
2616
3259
  docsTitlePlugin(md);
2617
3260
  embedSyntaxPlugin(md, options);
2618
3261
  inlineSyntaxPlugin(md, options);
3262
+ if (options.demo)
3263
+ demoPlugin(app, md);
2619
3264
  await containerPlugin(app, md, options);
2620
3265
  await imageSizePlugin(app, md, options.imageSize);
3266
+ },
3267
+ onPrepared: async () => {
3268
+ if (options.demo)
3269
+ await waitDemoRender();
3270
+ },
3271
+ onWatched(app, watchers) {
3272
+ if (options.demo) {
3273
+ demoWatcher(app, watchers);
3274
+ }
3275
+ },
3276
+ extendsPage: (page) => {
3277
+ if (options.demo)
3278
+ extendsPageWithDemo(page);
2621
3279
  }
2622
3280
  };
2623
3281
  }
@@ -2625,6 +3283,6 @@ export {
2625
3283
  markdownPowerPlugin,
2626
3284
  resolveImageSize
2627
3285
  };
2628
- /* istanbul ignore else -- @preserve */
2629
3286
  /* istanbul ignore if -- @preserve */
3287
+ /* istanbul ignore else -- @preserve */
2630
3288
  /* istanbul ignore next -- @preserve */