vuepress-plugin-md-power 1.0.0-rc.105 → 1.0.0-rc.107

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.
@@ -81,6 +81,11 @@ interface JSFiddleTokenMeta extends SizeOptions {
81
81
  tab?: string;
82
82
  }
83
83
 
84
+ type NpmToPackageManager = 'npm' | 'pnpm' | 'yarn' | 'bun' | 'deno';
85
+ type NpmToOptions = NpmToPackageManager[] | {
86
+ tabs?: NpmToPackageManager[];
87
+ };
88
+
84
89
  type PDFEmbedType = 'iframe' | 'embed' | 'pdfjs';
85
90
  interface PDFTokenMeta extends SizeOptions {
86
91
  page?: number;
@@ -151,6 +156,10 @@ interface MarkdownPowerPluginOptions {
151
156
  * 配置代码块分组
152
157
  */
153
158
  codeTabs?: CodeTabsOptions;
159
+ /**
160
+ * 是否启用 npm-to 容器
161
+ */
162
+ npmTo?: boolean | NpmToOptions;
154
163
  /**
155
164
  * 是否启用 PDF 嵌入语法
156
165
  *
@@ -295,4 +304,4 @@ declare function resolveImageSize(app: App, url: string, remote?: boolean): Prom
295
304
 
296
305
  declare function markdownPowerPlugin(options?: MarkdownPowerPluginOptions): Plugin;
297
306
 
298
- export { type BilibiliTokenMeta, type CanIUseMode, type CanIUseOptions, type CanIUseTokenMeta, type CodeSandboxTokenMeta, type CodeTabsOptions, type CodepenTokenMeta, type FileTreeIconMode, type FileTreeOptions, type IconsOptions, type JSFiddleTokenMeta, type MarkdownPowerPluginOptions, type PDFEmbedType, type PDFOptions, type PDFTokenMeta, type PlotOptions, type ReplEditorData, type ReplOptions, type ReplitTokenMeta, type SizeOptions, type ThemeOptions, type VideoOptions, type YoutubeTokenMeta, markdownPowerPlugin, resolveImageSize };
307
+ export { type BilibiliTokenMeta, type CanIUseMode, type CanIUseOptions, type CanIUseTokenMeta, type CodeSandboxTokenMeta, type CodeTabsOptions, type CodepenTokenMeta, type FileTreeIconMode, type FileTreeOptions, type IconsOptions, type JSFiddleTokenMeta, type MarkdownPowerPluginOptions, type NpmToOptions, type NpmToPackageManager, type PDFEmbedType, type PDFOptions, type PDFTokenMeta, type PlotOptions, type ReplEditorData, type ReplOptions, type ReplitTokenMeta, type SizeOptions, type ThemeOptions, type VideoOptions, type YoutubeTokenMeta, markdownPowerPlugin, resolveImageSize };
package/lib/node/index.js CHANGED
@@ -1369,6 +1369,346 @@ async function read(file) {
1369
1369
  return void 0;
1370
1370
  }
1371
1371
 
1372
+ // src/node/container/npmTo.ts
1373
+ import { isArray } from "@vuepress/helper";
1374
+ import container4 from "markdown-it-container";
1375
+ var ALLOW_LIST = ["npm", "pnpm", "yarn", "bun", "deno"];
1376
+ var BOOL_FLAGS = ["--no-save", "-B", "--save-bundle", "--save-dev", "-D", "--save-prod", "-P", "--save-peer", "-O", "--save-optional", "-E", "--save-exact", "-y", "--yes", "-g", "--global"];
1377
+ var DEFAULT_TABS = ["npm", "pnpm", "yarn"];
1378
+ var MANAGERS_CONFIG = {
1379
+ install: {
1380
+ pattern: /(?:^|\s)npm\s+(?:install|i)$/,
1381
+ pnpm: { cli: "pnpm install" },
1382
+ yarn: { cli: "yarn" },
1383
+ bun: { cli: "bun install" },
1384
+ deno: { cli: "deno install" }
1385
+ },
1386
+ add: {
1387
+ pattern: /(?:^|\s)npm\s+(?:install|i|add)(?:\s|$)/,
1388
+ pnpm: {
1389
+ cli: "pnpm add",
1390
+ flags: {
1391
+ "--no-save": "",
1392
+ // unsupported
1393
+ "-B": "",
1394
+ // unsupported
1395
+ "--save-bundle": ""
1396
+ // unsupported
1397
+ }
1398
+ },
1399
+ yarn: {
1400
+ cli: "yarn add",
1401
+ flags: {
1402
+ "--save-dev": "--dev",
1403
+ "--save-prod": "--prod",
1404
+ "-P": "",
1405
+ // in npm, `-P` same as `--save-prod`. but in yarn, `-P` same as `--peer`
1406
+ "--save-peer": "--peer",
1407
+ "--save-optional": "--optional",
1408
+ "--no-save": "",
1409
+ // unsupported
1410
+ "--save-exact": "--exact",
1411
+ "-B": "",
1412
+ // unsupported
1413
+ "--save-bundle": ""
1414
+ // unsupported
1415
+ }
1416
+ },
1417
+ bun: {
1418
+ cli: "bun add",
1419
+ flags: {
1420
+ "--save-dev": "--development",
1421
+ "-P": "",
1422
+ // it's default
1423
+ "--save-prod": "",
1424
+ // it's default
1425
+ "--save-peer": "",
1426
+ // unsupported
1427
+ "-O": "--optional",
1428
+ "--save-optional": "--optional",
1429
+ "--no-save": "",
1430
+ // unsupported
1431
+ "--save-exact": "--exact",
1432
+ "-B": "",
1433
+ // unsupported
1434
+ "--save-bundle": ""
1435
+ // unsupported
1436
+ }
1437
+ },
1438
+ deno: {
1439
+ cli: "deno add",
1440
+ flags: {
1441
+ "-g": "",
1442
+ // unsupported
1443
+ "--global": "",
1444
+ // unsupported
1445
+ "--save-dev": "--dev",
1446
+ "-P": "",
1447
+ // unsupported
1448
+ "--save-prod": "",
1449
+ // unsupported
1450
+ "--save-peer": "",
1451
+ // unsupported
1452
+ "-O": "",
1453
+ // unsupported
1454
+ "--save-optional": "",
1455
+ // unsupported
1456
+ "--no-save": "",
1457
+ // unsupported
1458
+ "-E": "",
1459
+ // unsupported
1460
+ "--save-exact": "",
1461
+ // unsupported
1462
+ "-B": "",
1463
+ // unsupported
1464
+ "--save-bundle": ""
1465
+ // unsupported
1466
+ }
1467
+ }
1468
+ },
1469
+ run: {
1470
+ pattern: /(?:^|\s)npm\s+(?:run|run-script|rum|urn)(?:\s|$)/,
1471
+ pnpm: {
1472
+ cli: "pnpm",
1473
+ flags: {
1474
+ "-w": "-F",
1475
+ // same as `--workspace`
1476
+ "--workspace": "--filter",
1477
+ // filter by workspaces
1478
+ "--": ""
1479
+ // scripts flags
1480
+ }
1481
+ },
1482
+ yarn: {
1483
+ cli: "yarn",
1484
+ flags: {
1485
+ "-w": "",
1486
+ // unsupported
1487
+ "--workspace": ""
1488
+ // unsupported
1489
+ }
1490
+ },
1491
+ bun: {
1492
+ cli: "bun run",
1493
+ flags: {
1494
+ "-w": "--filter",
1495
+ // same as `--workspace`
1496
+ "--workspace": "--filter"
1497
+ // filter by workspaces
1498
+ }
1499
+ },
1500
+ deno: {
1501
+ cli: "deno run",
1502
+ flags: {
1503
+ "-w": "",
1504
+ // unsupported
1505
+ "--workspace": ""
1506
+ // unsupported
1507
+ }
1508
+ }
1509
+ },
1510
+ create: {
1511
+ pattern: /(?:^|\s)npm\s+create\s/,
1512
+ pnpm: { cli: "pnpm create", flags: { "-y": "", "--yes": "" } },
1513
+ yarn: { cli: "yarn create", flags: { "-y": "", "--yes": "" } },
1514
+ bun: { cli: "bun create", flags: { "-y": "", "--yes": "" } },
1515
+ deno: { cli: "deno run -A ", flags: { "-y": "", "--yes": "" } }
1516
+ },
1517
+ init: {
1518
+ pattern: /(?:^|\s)npm\s+init/,
1519
+ pnpm: { cli: "pnpm init", flags: { "-y": "", "--yes": "" } },
1520
+ yarn: { cli: "yarn init", flags: { "-y": "", "--yes": "" } },
1521
+ bun: { cli: "bun init", flags: { "-y": "", "--yes": "" } },
1522
+ deno: { cli: "deno init", flags: { "-y": "", "--yes": "" } }
1523
+ },
1524
+ npx: {
1525
+ pattern: /(?:^|\s)npx\s+/,
1526
+ pnpm: { cli: "pnpm dlx" },
1527
+ yarn: { cli: "yarn dlx" },
1528
+ bun: { cli: "bunx" },
1529
+ deno: { cli: "deno run -A" }
1530
+ },
1531
+ remove: {
1532
+ pattern: /(?:^|\s)npm\s+(?:uninstall|r|rm|remove|unlink|un)(?:\s|$)/,
1533
+ pnpm: {
1534
+ cli: "pnpm remove",
1535
+ flags: { "--no-save": "", "--save": "", "-S": "" }
1536
+ },
1537
+ yarn: {
1538
+ cli: "yarn remove",
1539
+ flags: { "--save-dev": "--dev", "--save": "", "-S": "", "-g": "", "--global": "" }
1540
+ },
1541
+ bun: {
1542
+ cli: "bun remove",
1543
+ flags: { "--save-dev": "--development", "--save": "", "-S": "", "-g": "", "--global": "" }
1544
+ },
1545
+ deno: {
1546
+ cli: "deno uninstall",
1547
+ flags: { "--save-dev": "--dev", "--save": "", "-S": "" }
1548
+ }
1549
+ },
1550
+ ci: {
1551
+ pattern: /(?:^|\s)npm\s+ci$/,
1552
+ pnpm: { cli: "pnpm install --frozen-lockfile" },
1553
+ yarn: { cli: "yarn install --immutable" },
1554
+ bun: { cli: "bun install --frozen-lockfile" },
1555
+ deno: { cli: "deno install --frozen" }
1556
+ }
1557
+ };
1558
+ function npmToPlugins(md, options) {
1559
+ const type2 = "npm-to";
1560
+ const validate = (info) => info.trim().startsWith(type2);
1561
+ const opt = isArray(options) ? { tabs: options } : options;
1562
+ const defaultTabs = opt.tabs?.length ? opt.tabs : DEFAULT_TABS;
1563
+ const render = (tokens, idx) => {
1564
+ const { attrs: attrs2 } = resolveAttrs(tokens[idx].info.slice(type2.length - 1));
1565
+ const tabs2 = attrs2.tabs ? attrs2.tabs.split(/,\s*/) : defaultTabs;
1566
+ if (tokens[idx].nesting === 1) {
1567
+ const token = tokens[idx + 1];
1568
+ const info = token.info.trim();
1569
+ if (token.type === "fence" && (info.startsWith("sh") || info.startsWith("bash") || info.startsWith("shell"))) {
1570
+ const content = token.content;
1571
+ token.hidden = true;
1572
+ token.type = "text";
1573
+ token.content = "";
1574
+ const lines = content.split(/(\n|\s*&&\s*)/);
1575
+ return md.render(resolveNpmTo(lines, info, idx, tabs2), {});
1576
+ }
1577
+ }
1578
+ return "";
1579
+ };
1580
+ md.use(container4, type2, { validate, render });
1581
+ }
1582
+ function resolveNpmTo(lines, info, idx, tabs2) {
1583
+ tabs2 = validateTabs(tabs2);
1584
+ const res = [];
1585
+ const map = {};
1586
+ for (const tab3 of tabs2) {
1587
+ const newLines = [];
1588
+ for (const line of lines) {
1589
+ const config = findConfig(line);
1590
+ if (config && config[tab3]) {
1591
+ const parsed = map[line] ??= parseLine(line);
1592
+ const { cli, flags } = config[tab3];
1593
+ if (parsed) {
1594
+ let newLine = `${parsed.env ? `${parsed.env} ` : ""}${cli}`;
1595
+ if (parsed.args && flags) {
1596
+ let args = parsed.args;
1597
+ for (const [key, value] of Object.entries(flags)) {
1598
+ args = args.replaceAll(key, value);
1599
+ }
1600
+ newLine += ` ${args.replace(/\s+-/g, " -").trim()}`;
1601
+ }
1602
+ if (parsed.cmd)
1603
+ newLine += ` ${parsed.cmd}`;
1604
+ if (parsed.scriptArgs)
1605
+ newLine += ` ${parsed.scriptArgs}`;
1606
+ newLines.push(newLine.trim());
1607
+ }
1608
+ } else {
1609
+ newLines.push(line);
1610
+ }
1611
+ }
1612
+ res.push(`@tab ${tab3}
1613
+ \`\`\`${info}
1614
+ ${newLines.join("")}
1615
+ \`\`\``);
1616
+ }
1617
+ return `:::code-tabs#npm-to-${idx}
1618
+ ${res.join("\n")}
1619
+ :::`;
1620
+ }
1621
+ function findConfig(line) {
1622
+ for (const { pattern, ...config } of Object.values(MANAGERS_CONFIG)) {
1623
+ if (pattern.test(line)) {
1624
+ return config;
1625
+ }
1626
+ }
1627
+ return void 0;
1628
+ }
1629
+ function validateTabs(tabs2) {
1630
+ if (tabs2.length === 0) {
1631
+ return DEFAULT_TABS;
1632
+ }
1633
+ return tabs2.filter((tab3) => ALLOW_LIST.includes(tab3));
1634
+ }
1635
+ var LINE_REG = /(.*)(npm|npx)\s+(.*)/;
1636
+ function parseLine(line) {
1637
+ const match = line.match(LINE_REG);
1638
+ if (!match)
1639
+ return false;
1640
+ const [, env, cli, rest] = match;
1641
+ if (cli === "npx")
1642
+ return { env, cli, cmd: "", scriptArgs: rest?.trim() };
1643
+ const idx = rest.indexOf(" ");
1644
+ if (idx === -1)
1645
+ return { env, cli: `${cli} ${rest.trim()}`, cmd: "" };
1646
+ return { env, cli: `${cli} ${rest.slice(0, idx)}`, ...parseArgs(rest.slice(idx + 1)) };
1647
+ }
1648
+ function parseArgs(line) {
1649
+ line = line?.trim();
1650
+ if (!line)
1651
+ return { cmd: "" };
1652
+ const [npmArgs, scriptArgs] = line.split(/\s+--\s+/);
1653
+ let cmd = "";
1654
+ let args = "";
1655
+ if (npmArgs[0] !== "-") {
1656
+ if (npmArgs[0] === '"' || npmArgs[0] === "'") {
1657
+ const idx = npmArgs.slice(1).indexOf(npmArgs[0]);
1658
+ cmd = npmArgs.slice(0, idx);
1659
+ args = npmArgs.slice(idx + 1);
1660
+ } else {
1661
+ const idx = npmArgs.indexOf(" ");
1662
+ if (idx === -1) {
1663
+ cmd = npmArgs;
1664
+ } else {
1665
+ cmd = npmArgs.slice(0, idx);
1666
+ args = npmArgs.slice(idx + 1);
1667
+ }
1668
+ }
1669
+ } else {
1670
+ let newLine = "";
1671
+ let value = "";
1672
+ let isQuote = false;
1673
+ let isBool = false;
1674
+ let isNextValue = false;
1675
+ let quote = "";
1676
+ for (let i = 0; i < npmArgs.length; i++) {
1677
+ const v = npmArgs[i];
1678
+ if (!isQuote && (v === '"' || v === "'")) {
1679
+ quote = v;
1680
+ isQuote = true;
1681
+ value += v;
1682
+ } else if (isQuote && v === quote) {
1683
+ isQuote = false;
1684
+ value += v;
1685
+ } else if ((v === " " || v === "=" || i === npmArgs.length - 1) && !isQuote && value) {
1686
+ if (i === npmArgs.length - 1) {
1687
+ value += v;
1688
+ }
1689
+ const isKey = value[0] === "-";
1690
+ if (isKey) {
1691
+ isBool = BOOL_FLAGS.includes(value);
1692
+ isNextValue = !isBool;
1693
+ }
1694
+ if (!isKey && !isNextValue) {
1695
+ cmd += `${value} `;
1696
+ } else {
1697
+ newLine += `${value}${v || ""}`;
1698
+ if (!isKey && isNextValue) {
1699
+ isNextValue = false;
1700
+ }
1701
+ }
1702
+ value = "";
1703
+ } else {
1704
+ value += v;
1705
+ }
1706
+ }
1707
+ args = newLine;
1708
+ }
1709
+ return { cmd, args: args.trim(), scriptArgs };
1710
+ }
1711
+
1372
1712
  // src/node/container/tabs.ts
1373
1713
  import { tab as tab2 } from "@mdit/plugin-tab";
1374
1714
  var tabs = (md) => {
@@ -1397,6 +1737,9 @@ async function containerPlugin(app, md, options) {
1397
1737
  alignPlugin(md);
1398
1738
  tabs(md);
1399
1739
  codeTabs(md, options.codeTabs);
1740
+ if (options.npmTo) {
1741
+ npmToPlugins(md, typeof options.npmTo === "boolean" ? {} : options.npmTo);
1742
+ }
1400
1743
  if (options.repl)
1401
1744
  await langReplPlugin(app, md, options.repl);
1402
1745
  if (options.fileTree) {
@@ -1405,7 +1748,7 @@ async function containerPlugin(app, md, options) {
1405
1748
  }
1406
1749
 
1407
1750
  // src/node/embed/caniuse.ts
1408
- import container4 from "markdown-it-container";
1751
+ import container5 from "markdown-it-container";
1409
1752
  import { customAlphabet } from "nanoid";
1410
1753
 
1411
1754
  // src/node/embed/createEmbedRuleBlock.ts
@@ -1487,7 +1830,7 @@ function legacyCaniuse(md, { mode = "embed" } = {}) {
1487
1830
  return "";
1488
1831
  }
1489
1832
  };
1490
- md.use(container4, type2, { validate, render });
1833
+ md.use(container5, type2, { validate, render });
1491
1834
  }
1492
1835
  function resolveCanIUse({ feature, mode, versions }) {
1493
1836
  if (!feature)
@@ -79,6 +79,11 @@ interface JSFiddleTokenMeta extends SizeOptions {
79
79
  tab?: string;
80
80
  }
81
81
 
82
+ type NpmToPackageManager = 'npm' | 'pnpm' | 'yarn' | 'bun' | 'deno';
83
+ type NpmToOptions = NpmToPackageManager[] | {
84
+ tabs?: NpmToPackageManager[];
85
+ };
86
+
82
87
  type PDFEmbedType = 'iframe' | 'embed' | 'pdfjs';
83
88
  interface PDFTokenMeta extends SizeOptions {
84
89
  page?: number;
@@ -149,6 +154,10 @@ interface MarkdownPowerPluginOptions {
149
154
  * 配置代码块分组
150
155
  */
151
156
  codeTabs?: CodeTabsOptions;
157
+ /**
158
+ * 是否启用 npm-to 容器
159
+ */
160
+ npmTo?: boolean | NpmToOptions;
152
161
  /**
153
162
  * 是否启用 PDF 嵌入语法
154
163
  *
@@ -285,4 +294,4 @@ interface YoutubeTokenMeta extends SizeOptions {
285
294
  end?: string | number;
286
295
  }
287
296
 
288
- export type { BilibiliTokenMeta, CanIUseMode, CanIUseOptions, CanIUseTokenMeta, CodeSandboxTokenMeta, CodeTabsOptions, CodepenTokenMeta, FileTreeIconMode, FileTreeOptions, IconsOptions, JSFiddleTokenMeta, MarkdownPowerPluginOptions, PDFEmbedType, PDFOptions, PDFTokenMeta, PlotOptions, ReplEditorData, ReplOptions, ReplitTokenMeta, SizeOptions, ThemeOptions, VideoOptions, YoutubeTokenMeta };
297
+ export type { BilibiliTokenMeta, CanIUseMode, CanIUseOptions, CanIUseTokenMeta, CodeSandboxTokenMeta, CodeTabsOptions, CodepenTokenMeta, FileTreeIconMode, FileTreeOptions, IconsOptions, JSFiddleTokenMeta, MarkdownPowerPluginOptions, NpmToOptions, NpmToPackageManager, PDFEmbedType, PDFOptions, PDFTokenMeta, PlotOptions, ReplEditorData, ReplOptions, ReplitTokenMeta, SizeOptions, ThemeOptions, VideoOptions, YoutubeTokenMeta };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "vuepress-plugin-md-power",
3
3
  "type": "module",
4
- "version": "1.0.0-rc.105",
4
+ "version": "1.0.0-rc.107",
5
5
  "description": "The Plugin for VuePress 2 - markdown power",
6
6
  "author": "pengzhanbo <volodymyr@foxmail.com>",
7
7
  "license": "MIT",
@@ -31,8 +31,14 @@
31
31
  "lib"
32
32
  ],
33
33
  "peerDependencies": {
34
+ "markdown-it": "^14.0.0",
34
35
  "vuepress": "2.0.0-rc.17"
35
36
  },
37
+ "peerDependenciesMeta": {
38
+ "markdown-it": {
39
+ "optional": true
40
+ }
41
+ },
36
42
  "dependencies": {
37
43
  "@mdit/plugin-attrs": "^0.13.1",
38
44
  "@mdit/plugin-footnote": "^0.13.1",
@@ -46,10 +52,10 @@
46
52
  "image-size": "^1.1.1",
47
53
  "markdown-it-container": "^4.0.0",
48
54
  "nanoid": "^5.0.7",
49
- "shiki": "^1.21.0",
50
- "tm-grammars": "^1.17.28",
51
- "tm-themes": "^1.8.6",
52
- "vue": "^3.5.10"
55
+ "shiki": "^1.22.0",
56
+ "tm-grammars": "^1.18.0",
57
+ "tm-themes": "^1.8.7",
58
+ "vue": "^3.5.11"
53
59
  },
54
60
  "devDependencies": {
55
61
  "@types/markdown-it": "^14.1.2"