vuepress-plugin-md-power 1.0.0-rc.126 → 1.0.0-rc.128

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,688 @@ 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 path5 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: path9 }, env) {
2026
+ const imports = `import ${name ? `${name} from ` : ""}'${path9}';`;
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 path4 from "node:path";
2043
+ import { watch } from "chokidar";
2044
+ var renderDone = null;
2045
+ var renderCount = 0;
2046
+ var renderPromise;
2047
+ function createDemoRender() {
2048
+ renderPromise = new Promise((resolve) => {
2049
+ renderDone = resolve;
2050
+ });
2051
+ }
2052
+ async function waitDemoRender() {
2053
+ if (renderCount === 0) {
2054
+ renderDone?.();
2055
+ renderDone = null;
2056
+ }
2057
+ await renderPromise;
2058
+ }
2059
+ function markDemoRender() {
2060
+ renderCount++;
2061
+ }
2062
+ function checkDemoRender() {
2063
+ if (renderCount > 0) {
2064
+ renderCount--;
2065
+ }
2066
+ if (renderCount === 0) {
2067
+ renderDone?.();
2068
+ renderDone = null;
2069
+ }
2070
+ }
2071
+ var watcher = null;
2072
+ var tasks = {};
2073
+ var target = "md-power/demo/watcher.txt";
2074
+ function demoWatcher(app, watchers) {
2075
+ if (!watcher) {
2076
+ watcher = watch([], { ignoreInitial: true });
2077
+ }
2078
+ Object.keys(tasks).forEach((path9) => {
2079
+ watcher.add(path9);
2080
+ });
2081
+ const code = readFileSync(app.dir.temp(target));
2082
+ if (code) {
2083
+ const paths = JSON.parse(code || "{}");
2084
+ Object.entries(paths).forEach(([path9, output]) => {
2085
+ watcher.add(path9);
2086
+ tasks[path9] = output;
2087
+ });
2088
+ }
2089
+ updateWatchFiles(app);
2090
+ watcher.on("change", (path9) => {
2091
+ if (tasks[path9]) {
2092
+ const code2 = readFileSync(path9);
2093
+ if (code2 === false)
2094
+ return;
2095
+ const source = parseEmbedCode(code2);
2096
+ compileCode(source, tasks[path9]);
2097
+ }
2098
+ });
2099
+ watcher.on("unlink", (path9) => {
2100
+ delete tasks[path9];
2101
+ watcher.unwatch(path9);
2102
+ });
2103
+ watchers.push(() => {
2104
+ watcher.close();
2105
+ watcher = null;
2106
+ });
2107
+ }
2108
+ function addTask(app, path9, output) {
2109
+ if (tasks[path9])
2110
+ return;
2111
+ tasks[path9] = output;
2112
+ if (watcher) {
2113
+ watcher.add(path9);
2114
+ }
2115
+ updateWatchFiles(app);
2116
+ }
2117
+ async function updateWatchFiles(app) {
2118
+ await fs4.promises.mkdir(app.dir.temp(path4.dirname(target)), { recursive: true });
2119
+ await fs4.promises.writeFile(app.dir.temp(target), JSON.stringify(tasks));
2120
+ }
2121
+
2122
+ // src/node/demo/normal.ts
2123
+ var CONFIG_RE = /<script type="config">([\s\S]*?)\n<\/script>/;
2124
+ var SCRIPT_RE2 = /<script\s?(?:lang="(\w+)")?>([\s\S]*?)\n<\/script>/;
2125
+ var STYLE_RE = /<style\s?(?:lang="(\w+)")?>([\s\S]*?)\n<\/style>/;
2126
+ var scriptSupported = ["ts", "js"];
2127
+ var styleSupported = ["css", "scss", "less", "stylus", "styl"];
2128
+ var target2 = "md-power/demo/normal";
2129
+ var FENCE = "```";
2130
+ function parseEmbedCode(code) {
2131
+ const res = { html: "", css: "", script: "", imports: "", jsType: "js", cssType: "css" };
2132
+ res.html = code.replace(CONFIG_RE, (_, config) => {
2133
+ res.imports = config;
2134
+ return "";
2135
+ }).replace(SCRIPT_RE2, (_, lang, script) => {
2136
+ res.script = script;
2137
+ res.jsType = scriptSupported.includes(lang) ? lang : "js";
2138
+ return "";
2139
+ }).replace(STYLE_RE, (_, lang, style) => {
2140
+ res.css = style;
2141
+ res.cssType = styleSupported.includes(lang) ? lang === "styl" ? "stylus" : lang : "css";
2142
+ return "";
2143
+ });
2144
+ return res;
2145
+ }
2146
+ function codeToHtml(md, source, info) {
2147
+ let content = "::: code-tabs\n";
2148
+ if (source.html)
2149
+ content += `@tab HTML
2150
+ ${FENCE}html ${info}
2151
+ ${source.html.trim()}
2152
+ ${FENCE}
2153
+ `;
2154
+ if (source.script) {
2155
+ const title = source.jsType === "ts" ? "Typescript" : "Javascript";
2156
+ content += `@tab ${title}
2157
+ ${FENCE}${source.jsType} ${info}
2158
+ ${source.script.trim()}
2159
+ ${FENCE}
2160
+ `;
2161
+ }
2162
+ if (source.css) {
2163
+ const title = source.cssType === "stylus" ? "Stylus" : source.cssType.toUpperCase();
2164
+ content += `@tab ${title}
2165
+ ${FENCE}${source.cssType} ${info}
2166
+ ${source.css.trim()}
2167
+ ${FENCE}
2168
+ `;
2169
+ }
2170
+ content += "\n:::";
2171
+ return md.render(content, {});
2172
+ }
2173
+ async function compileCode(code, output) {
2174
+ markDemoRender();
2175
+ const res = { jsLib: [], cssLib: [], script: "", css: "", html: "" };
2176
+ if (!fs5.existsSync(output))
2177
+ writeFileSync(output, `import { ref } from "vue"
2178
+ export default ref(${JSON.stringify(res, null, 2)})`);
2179
+ try {
2180
+ if (code.imports) {
2181
+ const imports = JSON.parse(code.imports);
2182
+ res.jsLib = imports.jsLib ?? [];
2183
+ res.cssLib = imports.cssLib ?? [];
2184
+ }
2185
+ if (code.script) {
2186
+ res.script = await compileScript(code.script.trim(), code.jsType);
2187
+ }
2188
+ if (code.css) {
2189
+ res.css = await compileStyle(code.css.trim(), code.cssType);
2190
+ }
2191
+ if (code.html) {
2192
+ res.html = code.html.trim();
2193
+ }
2194
+ } catch (e) {
2195
+ console.error("[vuepress-plugin-md-power] demo parse error: \n", e);
2196
+ }
2197
+ writeFileSync(output, `import { ref } from "vue"
2198
+ export default ref(${JSON.stringify(res, null, 2)})`);
2199
+ checkDemoRender();
2200
+ }
2201
+ function normalEmbed(app, md, env, { url, title, desc, codeSetting = "", expanded = false }) {
2202
+ const filepath2 = findFile(app, env, url);
2203
+ const code = readFileSync(filepath2);
2204
+ if (code === false) {
2205
+ console.warn("[vuepress-plugin-md-power] Cannot read demo file:", filepath2);
2206
+ return "";
2207
+ }
2208
+ const source = parseEmbedCode(code);
2209
+ const prefix = (env.filePathRelative || "").replace(/\.md$/, "").replace(/\//g, "-");
2210
+ const basename = path5.basename(filepath2).replace(/-|\./g, "_");
2211
+ const name = `Demo${basename[0].toUpperCase()}${basename.slice(1)}`;
2212
+ const demo = { type: "normal", export: name, path: filepath2 };
2213
+ const output = app.dir.temp(target2, `${prefix}-${name}.js`);
2214
+ compileCode(source, output);
2215
+ addTask(app, filepath2, output);
2216
+ env.demoFiles ??= [];
2217
+ if (!env.demoFiles.some((d) => d.path === filepath2)) {
2218
+ env.demoFiles.push(demo);
2219
+ insertSetupScript({ ...demo, path: output }, env);
2220
+ }
2221
+ return `<VPDemoNormal :config="${name}"${title ? ` title="${title}"` : ""}${desc ? ` desc="${desc}"` : ""}${expanded ? " expanded" : ""}>
2222
+ ${codeToHtml(md, source, codeSetting)}
2223
+ </VPDemoNormal>`;
2224
+ }
2225
+ var normalContainerRender = {
2226
+ before(app, md, env, meta, codeMap) {
2227
+ const { url, title, desc, expanded = false } = meta;
2228
+ const name = `DemoContainer${url}`;
2229
+ const prefix = (env.filePathRelative || "").replace(/\.md$/, "").replace(/\//g, "-");
2230
+ const output = app.dir.temp(path5.join(target2, `${prefix}-${name}.js`));
2231
+ env.demoFiles ??= [];
2232
+ if (!env.demoFiles.some((d) => d.path === output)) {
2233
+ const demo = { type: "normal", export: name, gitignore: true, path: output };
2234
+ env.demoFiles.push(demo);
2235
+ insertSetupScript(demo, env);
2236
+ }
2237
+ const source = parseContainerCode(codeMap);
2238
+ compileCode(source, output);
2239
+ return `<VPDemoNormal :config="${name}"${title ? ` title="${title}"` : ""}${desc ? ` desc="${desc}"` : ""}${expanded ? " expanded" : ""}>`;
2240
+ },
2241
+ after: () => "</VPDemoNormal>",
2242
+ token(token) {
2243
+ if (token.info.trim().startsWith("json")) {
2244
+ token.hidden = true;
2245
+ token.type = "text";
2246
+ token.content = "";
2247
+ }
2248
+ }
2249
+ };
2250
+ function parseContainerCode(map) {
2251
+ const res = { html: "", css: "", script: "", imports: "", jsType: "js", cssType: "css" };
2252
+ if (map.html) {
2253
+ res.html = map.html;
2254
+ }
2255
+ if (map.js) {
2256
+ res.script = map.js;
2257
+ res.jsType = "js";
2258
+ }
2259
+ if (map.ts) {
2260
+ res.script = map.ts;
2261
+ res.jsType = "ts";
2262
+ }
2263
+ if (map.css) {
2264
+ res.css = map.css;
2265
+ res.cssType = "css";
2266
+ }
2267
+ if (map.less) {
2268
+ res.css = map.less;
2269
+ res.cssType = "less";
2270
+ }
2271
+ if (map.scss) {
2272
+ res.css = map.scss;
2273
+ res.cssType = "scss";
2274
+ }
2275
+ if (map.styl || map.stylus) {
2276
+ res.css = map.styl || map.stylus;
2277
+ res.cssType = "stylus";
2278
+ }
2279
+ if (map.json) {
2280
+ res.imports = map.json;
2281
+ }
2282
+ return res;
2283
+ }
2284
+
2285
+ // src/node/demo/supports/alias.ts
2286
+ function normalizeAlias(info) {
2287
+ const [lang] = info.trim().split(/\s+|:|\{/);
2288
+ switch (lang) {
2289
+ case "vue":
2290
+ return "vue";
2291
+ case "js":
2292
+ case "javascript":
2293
+ return "js";
2294
+ case "ts":
2295
+ case "typescript":
2296
+ return "ts";
2297
+ case "stylus":
2298
+ case "styl":
2299
+ return "stylus";
2300
+ case "md":
2301
+ case "markdown":
2302
+ return "md";
2303
+ }
2304
+ return lang;
2305
+ }
2306
+
2307
+ // src/node/demo/vue.ts
2308
+ import path6 from "node:path";
2309
+ function vueEmbed(app, md, env, { url, title, desc, codeSetting = "", expanded = false }) {
2310
+ const filepath2 = findFile(app, env, url);
2311
+ const code = readFileSync(filepath2);
2312
+ if (code === false) {
2313
+ console.warn("[vuepress-plugin-md-power] Cannot read vue file:", filepath2);
2314
+ return "";
2315
+ }
2316
+ const basename = path6.basename(filepath2).replace(/-|\./g, "_");
2317
+ const ext = path6.extname(filepath2).slice(1);
2318
+ const name = `Demo${basename[0].toUpperCase()}${basename.slice(1)}`;
2319
+ const demo = { type: "vue", export: name, path: filepath2 };
2320
+ env.demoFiles ??= [];
2321
+ if (!env.demoFiles.some((d) => d.path === filepath2)) {
2322
+ env.demoFiles.push(demo);
2323
+ insertSetupScript(demo, env);
2324
+ }
2325
+ return `<VPDemoBasic type="vue"${title ? ` title="${title}"` : ""}${desc ? ` desc="${desc}"` : ""}${expanded ? " expanded" : ""}>
2326
+ <${name} />
2327
+ <template #code>
2328
+ ${md.render(`\`\`\`${ext}${codeSetting}
2329
+ ${code}
2330
+ \`\`\``, {})}
2331
+ </template>
2332
+ </VPDemoBasic>`;
2333
+ }
2334
+ var target3 = "md-power/demo/vue";
2335
+ var vueContainerRender = {
2336
+ before: (app, md, env, meta, codeMap) => {
2337
+ const { url, title, desc, expanded = false } = meta;
2338
+ const componentName2 = `DemoContainer${url}`;
2339
+ const prefix = (env.filePathRelative || "").replace(/\.md$/, "").replace(/\//g, "-");
2340
+ env.demoFiles ??= [];
2341
+ const output = app.dir.temp(path6.join(target3, `${prefix}-${componentName2}`));
2342
+ if (codeMap.vue || codeMap.js || codeMap.ts) {
2343
+ let scriptOutput = output;
2344
+ let content = "";
2345
+ if (codeMap.vue) {
2346
+ scriptOutput += ".vue";
2347
+ content = transformStyle(codeMap.vue);
2348
+ } else if (codeMap.ts) {
2349
+ scriptOutput += ".ts";
2350
+ content = codeMap.ts;
2351
+ } else if (codeMap.js) {
2352
+ scriptOutput += ".js";
2353
+ content = codeMap.js;
2354
+ }
2355
+ content = transformImports(content, env.filePath || "");
2356
+ const script = { type: "vue", export: componentName2, path: scriptOutput, gitignore: true };
2357
+ writeFileSync(scriptOutput, content);
2358
+ if (!env.demoFiles.some((d) => d.path === scriptOutput)) {
2359
+ env.demoFiles.push(script);
2360
+ insertSetupScript(script, env);
2361
+ }
2362
+ }
2363
+ if (codeMap.css || codeMap.scss || codeMap.less || codeMap.stylus) {
2364
+ let styleOutput = output;
2365
+ let content = "";
2366
+ if (codeMap.css) {
2367
+ styleOutput += ".css";
2368
+ content = codeMap.css;
2369
+ } else if (codeMap.scss) {
2370
+ styleOutput += ".scss";
2371
+ content = codeMap.scss;
2372
+ } else if (codeMap.less) {
2373
+ styleOutput += ".less";
2374
+ content = codeMap.less;
2375
+ } else if (codeMap.stylus) {
2376
+ styleOutput += ".styl";
2377
+ content = codeMap.stylus;
2378
+ }
2379
+ writeFileSync(styleOutput, content);
2380
+ const style = { type: "css", path: styleOutput, gitignore: true };
2381
+ if (!env.demoFiles.some((d) => d.path === styleOutput)) {
2382
+ env.demoFiles.push(style);
2383
+ insertSetupScript(style, env);
2384
+ }
2385
+ }
2386
+ return `<VPDemoBasic${title ? ` title="${title}"` : ""}${desc ? ` desc="${desc}"` : ""}${expanded ? " expanded" : ""}>
2387
+ <${componentName2} />
2388
+ <template #code>
2389
+ `;
2390
+ },
2391
+ after: () => "</template></VPDemoBasic>"
2392
+ };
2393
+ var IMPORT_RE = /import\s+(?:\w+\s+from\s+)?['"]([^'"]+)['"]/g;
2394
+ var STYLE_RE2 = /<style.*?>/;
2395
+ function transformImports(code, filepath2) {
2396
+ return code.replace(IMPORT_RE, (matched, url) => {
2397
+ if (url.startsWith("./") || url.startsWith("../")) {
2398
+ return matched.replace(url, `${path6.resolve(path6.dirname(filepath2), url)}`);
2399
+ }
2400
+ return matched;
2401
+ });
2402
+ }
2403
+ function transformStyle(code) {
2404
+ return code.replace(STYLE_RE2, (matched) => {
2405
+ if (matched.includes("scoped")) {
2406
+ return matched;
2407
+ }
2408
+ return matched.replace("<style", "<style scoped");
2409
+ });
2410
+ }
2411
+
2412
+ // src/node/demo/demo.ts
2413
+ function demoEmbed(app, md) {
2414
+ createEmbedRuleBlock(md, {
2415
+ type: "demo",
2416
+ syntaxPattern: /^@\[demo(?:\s(vue|normal|markdown))?\s?(.*)\]\((.*)\)/,
2417
+ meta: ([, type2, info, url]) => ({
2418
+ type: type2 || "normal",
2419
+ url,
2420
+ ...resolveAttrs(info).attrs
2421
+ }),
2422
+ content: (meta, content, env) => {
2423
+ const { url, type: type2 } = meta;
2424
+ if (!url) {
2425
+ console.warn("[vuepress-plugin-md-power] Invalid demo url: ", url);
2426
+ return content;
2427
+ }
2428
+ if (type2 === "vue") {
2429
+ return vueEmbed(app, md, env, meta);
2430
+ }
2431
+ if (type2 === "normal") {
2432
+ return normalEmbed(app, md, env, meta);
2433
+ }
2434
+ if (type2 === "markdown") {
2435
+ return markdownEmbed(app, md, env, meta);
2436
+ }
2437
+ return content;
2438
+ }
2439
+ });
2440
+ }
2441
+ var INFO_RE = /(vue|normal|markdown)?\s?(.*)/;
2442
+ var renderMap = {
2443
+ vue: vueContainerRender,
2444
+ normal: normalContainerRender,
2445
+ markdown: markdownContainerRender
2446
+ };
2447
+ function demoContainer(app, md) {
2448
+ let currentRender;
2449
+ const render = (tokens, index, _, env) => {
2450
+ const token = tokens[index];
2451
+ if (token.nesting === 1) {
2452
+ const meta = getContainerMeta(token.info);
2453
+ meta.url = `${index}`;
2454
+ currentRender = renderMap[meta.type];
2455
+ return currentRender?.before(
2456
+ app,
2457
+ md,
2458
+ env,
2459
+ meta,
2460
+ parseCodeMapping(tokens, index, currentRender.token)
2461
+ ) || "";
2462
+ } else {
2463
+ const res = currentRender?.after() || "";
2464
+ currentRender = void 0;
2465
+ return res;
2466
+ }
2467
+ };
2468
+ md.use(container5, "demo", { render });
2469
+ }
2470
+ function parseCodeMapping(tokens, index, cb) {
2471
+ const codeMap = {};
2472
+ for (let i = index + 1; !(tokens[i].nesting === -1 && tokens[i].type === "container_demo_close"); ++i) {
2473
+ const token = tokens[i];
2474
+ if (token.type === "fence") {
2475
+ codeMap[normalizeAlias(token.info)] = token.content.trim();
2476
+ cb?.(token, tokens, i);
2477
+ }
2478
+ }
2479
+ return codeMap;
2480
+ }
2481
+ function getContainerMeta(info) {
2482
+ const [, type2, raw] = (info.trim().slice(4).trim() || "").match(INFO_RE) || [];
2483
+ const { attrs: attrs2 } = resolveAttrs(raw);
2484
+ return {
2485
+ url: "",
2486
+ type: type2 || "normal",
2487
+ ...attrs2
2488
+ };
2489
+ }
2490
+
2491
+ // src/node/demo/extendPage.ts
2492
+ function extendsPageWithDemo(page) {
2493
+ const markdownEnv = page.markdownEnv;
2494
+ const demoFiles = markdownEnv.demoFiles ?? [];
2495
+ page.deps.push(
2496
+ ...demoFiles.filter(({ type: type2 }) => type2 === "markdown").map(({ path: path9 }) => path9)
2497
+ );
2498
+ (page.frontmatter.gitInclude ??= []).push(
2499
+ ...demoFiles.filter(({ gitignore }) => !gitignore).map(({ path: path9 }) => path9)
2500
+ );
2501
+ }
2502
+
2503
+ // src/node/demo/index.ts
2504
+ function demoPlugin(app, md) {
2505
+ createDemoRender();
2506
+ demoEmbed(app, md);
2507
+ demoContainer(app, md);
2508
+ }
2509
+
1827
2510
  // src/node/embed/audio/reader.ts
1828
2511
  var audioReader = (state, silent) => {
1829
2512
  const max = state.posMax;
@@ -1886,56 +2569,12 @@ var audioReader = (state, silent) => {
1886
2569
  var audioReaderPlugin = (md) => md.inline.ruler.before("link", "audio-reader", audioReader);
1887
2570
 
1888
2571
  // src/node/embed/caniuse.ts
1889
- import container5 from "markdown-it-container";
2572
+ import container6 from "markdown-it-container";
1890
2573
 
1891
2574
  // src/node/utils/nanoid.ts
1892
2575
  import { customAlphabet } from "nanoid";
1893
2576
  var nanoid = customAlphabet("abcdefghijklmnopqrstuvwxyz", 5);
1894
2577
 
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
2578
  // src/node/embed/caniuse.ts
1940
2579
  var UNDERLINE_RE = /_+/g;
1941
2580
  var caniusePlugin = (md, { mode: defaultMode = "embed" } = {}) => {
@@ -1970,7 +2609,7 @@ function legacyCaniuse(md, { mode = "embed" } = {}) {
1970
2609
  return "";
1971
2610
  }
1972
2611
  };
1973
- md.use(container5, type2, { validate, render });
2612
+ md.use(container6, type2, { validate, render });
1974
2613
  }
1975
2614
  function resolveCanIUse({ feature, mode, versions }) {
1976
2615
  if (!feature)
@@ -2124,7 +2763,7 @@ var replitPlugin = (md) => {
2124
2763
  };
2125
2764
 
2126
2765
  // src/node/embed/pdf.ts
2127
- import { path as path3 } from "vuepress/utils";
2766
+ import { path as path7 } from "vuepress/utils";
2128
2767
  var pdfPlugin = (md) => {
2129
2768
  createEmbedRuleBlock(md, {
2130
2769
  type: "pdf",
@@ -2140,7 +2779,7 @@ var pdfPlugin = (md) => {
2140
2779
  width: attrs2.width ? parseRect(attrs2.width) : "100%",
2141
2780
  height: attrs2.height ? parseRect(attrs2.height) : "",
2142
2781
  ratio: attrs2.ratio ? parseRect(attrs2.ratio) : "",
2143
- title: path3.basename(src || "")
2782
+ title: path7.basename(src || "")
2144
2783
  };
2145
2784
  },
2146
2785
  content({ title, src, page, noToolbar, width, height, ratio, zoom }) {
@@ -2150,12 +2789,12 @@ var pdfPlugin = (md) => {
2150
2789
  };
2151
2790
 
2152
2791
  // src/node/embed/video/artPlayer.ts
2153
- import { isPackageExists } from "local-pkg";
2792
+ import { isPackageExists as isPackageExists2 } from "local-pkg";
2154
2793
  import { colors } from "vuepress/utils";
2155
2794
  var installed = {
2156
- dashjs: isPackageExists("dashjs"),
2157
- hlsjs: isPackageExists("hls.js"),
2158
- mpegtsjs: isPackageExists("mpegts.js")
2795
+ dashjs: isPackageExists2("dashjs"),
2796
+ hlsjs: isPackageExists2("hls.js"),
2797
+ mpegtsjs: isPackageExists2("mpegts.js")
2159
2798
  };
2160
2799
  var SUPPORTED_VIDEO_TYPES = ["mp4", "mp3", "webm", "ogg", "mpd", "dash", "m3u8", "hls", "ts", "flv"];
2161
2800
  var artPlayerPlugin = (md) => {
@@ -2512,11 +3151,11 @@ function inlineSyntaxPlugin(md, options) {
2512
3151
 
2513
3152
  // src/node/prepareConfigFile.ts
2514
3153
  import { ensureEndingSlash } from "@vuepress/helper";
2515
- import { getDirname, path as path4 } from "vuepress/utils";
3154
+ import { getDirname, path as path8 } from "vuepress/utils";
2516
3155
  var { url: filepath } = import.meta;
2517
3156
  var __dirname = getDirname(filepath);
2518
3157
  var CLIENT_FOLDER = ensureEndingSlash(
2519
- path4.resolve(__dirname, "../client")
3158
+ path8.resolve(__dirname, "../client")
2520
3159
  );
2521
3160
  async function prepareConfigFile(app, options) {
2522
3161
  const imports = /* @__PURE__ */ new Set();
@@ -2569,6 +3208,12 @@ async function prepareConfigFile(app, options) {
2569
3208
  imports.add(`import AudioReader from '${CLIENT_FOLDER}components/AudioReader.vue'`);
2570
3209
  enhances.add(`app.component('AudioReader', AudioReader)`);
2571
3210
  }
3211
+ if (options.demo) {
3212
+ imports.add(`import VPDemoBasic from '${CLIENT_FOLDER}components/VPDemoBasic.vue'`);
3213
+ imports.add(`import VPDemoNormal from '${CLIENT_FOLDER}components/VPDemoNormal.vue'`);
3214
+ enhances.add(`app.component('VPDemoBasic', VPDemoBasic)`);
3215
+ enhances.add(`app.component('VPDemoNormal', VPDemoNormal)`);
3216
+ }
2572
3217
  return app.writeTemp(
2573
3218
  "md-power/config.js",
2574
3219
  `import { defineClientConfig } from 'vuepress/client'
@@ -2592,9 +3237,9 @@ function markdownPowerPlugin(options = {}) {
2592
3237
  clientConfigFile: (app) => prepareConfigFile(app, options),
2593
3238
  define: {
2594
3239
  __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")
3240
+ __MD_POWER_DASHJS_INSTALLED__: isPackageExists3("dashjs"),
3241
+ __MD_POWER_HLSJS_INSTALLED__: isPackageExists3("hls.js"),
3242
+ __MD_POWER_MPEGTSJS_INSTALLED__: isPackageExists3("mpegts.js")
2598
3243
  },
2599
3244
  extendsBundlerOptions(bundlerOptions, app) {
2600
3245
  if (options.repl) {
@@ -2616,8 +3261,23 @@ function markdownPowerPlugin(options = {}) {
2616
3261
  docsTitlePlugin(md);
2617
3262
  embedSyntaxPlugin(md, options);
2618
3263
  inlineSyntaxPlugin(md, options);
3264
+ if (options.demo)
3265
+ demoPlugin(app, md);
2619
3266
  await containerPlugin(app, md, options);
2620
3267
  await imageSizePlugin(app, md, options.imageSize);
3268
+ },
3269
+ onPrepared: async () => {
3270
+ if (options.demo)
3271
+ await waitDemoRender();
3272
+ },
3273
+ onWatched(app, watchers) {
3274
+ if (options.demo) {
3275
+ demoWatcher(app, watchers);
3276
+ }
3277
+ },
3278
+ extendsPage: (page) => {
3279
+ if (options.demo)
3280
+ extendsPageWithDemo(page);
2621
3281
  }
2622
3282
  };
2623
3283
  }
@@ -2625,6 +3285,6 @@ export {
2625
3285
  markdownPowerPlugin,
2626
3286
  resolveImageSize
2627
3287
  };
2628
- /* istanbul ignore else -- @preserve */
2629
3288
  /* istanbul ignore if -- @preserve */
3289
+ /* istanbul ignore else -- @preserve */
2630
3290
  /* istanbul ignore next -- @preserve */