md-editor-v3 4.14.1 → 4.15.0-0-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/README.md CHANGED
@@ -124,6 +124,8 @@ Inputing prompt and mark, emoji extensions
124
124
  | noImgZoomIn | `boolean` | false | Enable the function of enlarging images |
125
125
  | customIcon | `CustomIcon` | {} | Customized icons |
126
126
  | sanitizeMermaid | `(h: string) => Promise<string>` | (h: string) => Promise.resolve(h) | Convert the generated mermaid code |
127
+ | codeFoldable | `boolean` | true | Whether to enable code folding feature |
128
+ | autoFoldThreshold | `number` | 30 | Threshold for triggering automatic code folding by line count |
127
129
 
128
130
  ### 🔩 MdEditor Props
129
131
 
@@ -2966,7 +2966,9 @@ const Content = /* @__PURE__ */ vue.defineComponent({
2966
2966
  "formatCopiedText": props2.formatCopiedText,
2967
2967
  "noHighlight": props2.noHighlight,
2968
2968
  "noImgZoomIn": props2.noImgZoomIn,
2969
- "sanitizeMermaid": props2.sanitizeMermaid
2969
+ "sanitizeMermaid": props2.sanitizeMermaid,
2970
+ "codeFoldable": props2.codeFoldable,
2971
+ "autoFoldThreshold": props2.autoFoldThreshold
2970
2972
  }, null), props2.catalogVisible && vue.createVNode(MdCatalog, {
2971
2973
  "theme": props2.theme,
2972
2974
  "class": `${config.prefix}-catalog-editor`,
@@ -3242,7 +3244,9 @@ const Editor = /* @__PURE__ */ vue.defineComponent({
3242
3244
  ctx.emit("onInputBoxWitdhChange", width);
3243
3245
  },
3244
3246
  "sanitizeMermaid": props2.sanitizeMermaid,
3245
- "transformImgUrl": props2.transformImgUrl
3247
+ "transformImgUrl": props2.transformImgUrl,
3248
+ "codeFoldable": props2.codeFoldable,
3249
+ "autoFoldThreshold": props2.autoFoldThreshold
3246
3250
  }, null), props2.footers.length > 0 && vue.createVNode(Footer, {
3247
3251
  "modelValue": props2.modelValue,
3248
3252
  "footers": props2.footers,
@@ -15,71 +15,46 @@ const lruCache = require("lru-cache");
15
15
  const userZoom = (props, html) => {
16
16
  const editorId = vue.inject("editorId");
17
17
  const { noImgZoomIn } = props;
18
- let zoomHander = () => {
19
- };
20
- vue.watch(
21
- [html, vue.toRef(props.setting, "preview")],
22
- () => {
23
- if (!noImgZoomIn && props.setting.preview) {
24
- zoomHander();
25
- }
26
- },
27
- {
28
- immediate: true
18
+ const zoomHander = util.debounce(() => {
19
+ const imgs = document.querySelectorAll(`#${editorId}-preview img`);
20
+ if (imgs.length === 0) {
21
+ return;
29
22
  }
30
- );
31
- vue.onMounted(() => {
32
- zoomHander = util.debounce(() => {
33
- const imgs = document.querySelectorAll(`#${editorId}-preview img`);
34
- if (imgs.length === 0) {
35
- return;
36
- }
37
- mediumZoom(imgs, {
38
- background: "#00000073"
39
- });
23
+ mediumZoom(imgs, {
24
+ background: "#00000073"
40
25
  });
41
26
  });
42
- };
43
- const StrIcon = (name, customIcon) => {
44
- if (typeof customIcon[name] === "string") {
45
- return customIcon[name];
46
- }
47
- const defaultStr = `<i class="${config.prefix}-iconfont ${config.prefix}-icon-${name}"></i>`;
48
- switch (config.configOption.iconfontType) {
49
- case "svg": {
50
- return `<svg class="${config.prefix}-icon" aria-hidden="true"><use xlink:href="#${config.prefix}-icon-${name}"></use></svg>`;
51
- }
52
- default: {
53
- return defaultStr;
27
+ vue.onMounted(() => {
28
+ zoomHander();
29
+ });
30
+ vue.watch([html, vue.toRef(props.setting, "preview")], () => {
31
+ if (!noImgZoomIn && props.setting.preview) {
32
+ zoomHander();
54
33
  }
55
- }
34
+ });
56
35
  };
57
36
  const useCopyCode = (props, html, key) => {
58
37
  const editorId = vue.inject("editorId");
59
38
  const ult = vue.inject("usedLanguageText");
60
- const customIcon = vue.inject("customIcon");
61
39
  const initCopyEntry = () => {
62
- document.querySelectorAll(`#${editorId}-preview pre`).forEach((pre) => {
63
- var _a;
40
+ document.querySelectorAll(`#${editorId} .${config.prefix}-preview .${config.prefix}-code`).forEach((codeBlock) => {
64
41
  let clearTimer = -1;
65
- (_a = pre.querySelector(".copy-button")) == null ? void 0 : _a.remove();
66
- const copyBtnText = ult.value.copyCode.text;
67
- const copyButton = document.createElement("span");
68
- copyButton.setAttribute("class", "copy-button");
69
- copyButton.dataset.tips = copyBtnText;
70
- copyButton.innerHTML = StrIcon("copy", customIcon.value);
71
- copyButton.addEventListener("click", () => {
72
- clearTimeout(clearTimer);
73
- const codeText = pre.querySelector("code").innerText;
74
- const success = copy(props.formatCopiedText(codeText));
75
- const succssTip = ult.value.copyCode.successTips;
76
- const failTip = ult.value.copyCode.failTips;
77
- copyButton.dataset.tips = success ? succssTip : failTip;
78
- clearTimer = window.setTimeout(() => {
79
- copyButton.dataset.tips = copyBtnText;
80
- }, 1500);
81
- });
82
- pre.appendChild(copyButton);
42
+ const copyButton = codeBlock.querySelector(
43
+ `.${config.prefix}-copy-button`
44
+ );
45
+ if (copyButton)
46
+ copyButton.onclick = (e) => {
47
+ e.preventDefault();
48
+ clearTimeout(clearTimer);
49
+ const activeCode = codeBlock.querySelector("input:checked + pre code") || codeBlock.querySelector("pre code");
50
+ const codeText = activeCode.textContent;
51
+ const success = copy(props.formatCopiedText(codeText));
52
+ const { text, successTips, failTips } = ult.value.copyCode;
53
+ copyButton.innerHTML = success ? successTips : failTips;
54
+ clearTimer = window.setTimeout(() => {
55
+ copyButton.innerHTML = text;
56
+ }, 1500);
57
+ };
83
58
  });
84
59
  };
85
60
  const htmlChanged = () => {
@@ -597,14 +572,31 @@ const codetabs = (md, _opts) => {
597
572
  return info ? info.split(/(\s+)/g)[0] : "";
598
573
  }
599
574
  const fenceGroup = (tokens, idx, options, env, slf) => {
575
+ var _a, _b;
600
576
  if (tokens[idx].hidden) {
601
577
  return "";
602
578
  }
603
- const [GROUP, _] = getGroupAndTab(tokens[idx]);
579
+ const [GROUP] = getGroupAndTab(tokens[idx]);
604
580
  if (GROUP === null) {
605
- return defaultRender(tokens, idx, options, env, slf);
581
+ const open2 = tokens[idx].content.trim().split("\n").length < _opts.autoFoldThreshold;
582
+ const tagContainer2 = _opts.codeFoldable ? "details" : "div", tagHeader2 = _opts.codeFoldable ? "summary" : "div";
583
+ const codeRendered = defaultRender(tokens, idx, options, env, slf);
584
+ return `<${tagContainer2} class="${config.prefix}-code"${open2 ? " open" : ""}>
585
+ <${tagHeader2} class="${config.prefix}-code-head">
586
+ <div class="${config.prefix}-code-flag">
587
+ <span></span>
588
+ <span></span>
589
+ <span></span>
590
+ </div>
591
+ <div>
592
+ <span class="${config.prefix}-code-lang">${tokens[idx].info.trim()}</span>
593
+ <span class="${config.prefix}-copy-button">${(_a = _opts.usedLanguageTextRef.value) == null ? void 0 : _a.copyCode.text}</span>
594
+ </div>
595
+ </${tagHeader2}>${codeRendered}</${tagContainer2}>`;
606
596
  }
607
- let token, group, tab, checked, labels = "", pres = "";
597
+ let token, group, tab, checked, labels = "", pres = "", langs = "";
598
+ const open = tokens[idx].content.trim().split("\n").length < _opts.autoFoldThreshold;
599
+ const tagContainer = _opts.codeFoldable ? "details" : "div", tagHeader = _opts.codeFoldable ? "summary" : "div";
608
600
  for (let i = idx; i < tokens.length; i++) {
609
601
  token = tokens[i];
610
602
  [group, tab] = getGroupAndTab(token);
@@ -613,17 +605,31 @@ const codetabs = (md, _opts) => {
613
605
  }
614
606
  token.info = token.info.replace(re, "");
615
607
  token.hidden = true;
616
- checked = i - idx > 0 ? "" : " checked";
617
- labels += `<li><input type="radio" name="label-group-${_opts.editorId}-${idx}"${checked}><label for="group-${_opts.editorId}-${idx}-tab-${i - idx}" onclick="this.previousElementSibling.click()">${tab || getLangName(token)}</label></li>
618
- `;
619
- pres += `<input type="radio" id="group-${_opts.editorId}-${idx}-tab-${i - idx}" name="group-${_opts.editorId}-${idx}"${checked}>
620
- ` + defaultRender(tokens, i, options, env, slf);
608
+ const className = `${config.prefix}-codetab-${_opts.editorId}-${idx}-${i - idx}`;
609
+ checked = i - idx > 0 ? "" : "checked";
610
+ labels += `<li>
611
+ <input type="radio" name="${config.prefix}-codetab-label-${_opts.editorId}-${idx}" class="${className}" ${checked}>
612
+ <label onclick="document.querySelectorAll('.${className}').forEach(e => e.click())">${tab || getLangName(token)}</label>
613
+ </li>`;
614
+ pres += `<input type="radio" name="${config.prefix}-codetab-pre-${_opts.editorId}-${idx}" class="${className}" ${checked}>
615
+ ${defaultRender(tokens, i, options, env, slf)}`;
616
+ langs += `<input type="radio" name="${config.prefix}-codetab-lang-${_opts.editorId}-${idx}" class="${className}" ${checked}>
617
+ <span class=${config.prefix}-code-lang>${getLangName(token)}</span>`;
621
618
  }
622
- return '<div class="code-tabs">\n<ul>\n' + labels + "</ul>\n" + pres + "</div>";
619
+ return `<${tagContainer} class="${config.prefix}-code" ${open ? " open" : ""}>
620
+ <${tagHeader} class="${config.prefix}-code-head">
621
+ <div class="${config.prefix}-code-flag">
622
+ <ul class="${config.prefix}-codetab-label">${labels}</ul>
623
+ </div>
624
+ <div>
625
+ <span class="${config.prefix}-codetab-lang">${langs}</span>
626
+ <span class="${config.prefix}-copy-button">${(_b = _opts.usedLanguageTextRef.value) == null ? void 0 : _b.copyCode.text}</span>
627
+ </div>
628
+ </${tagHeader}>${pres}</${tagContainer}>`;
623
629
  };
624
630
  md.renderer.rules.fence = fenceGroup;
625
631
  };
626
- const CodeTabsPlugin = codetabs;
632
+ const CodePlugin = codetabs;
627
633
  const initLineNumber = (md) => {
628
634
  [
629
635
  "paragraph_open",
@@ -661,6 +667,9 @@ const initLineNumber = (md) => {
661
667
  const useMarkdownIt = (props, previewOnly) => {
662
668
  const { editorConfig, markdownItConfig, markdownItPlugins } = config.configOption;
663
669
  const editorId = vue.inject("editorId");
670
+ const usedLanguageTextRef = vue.inject(
671
+ "usedLanguageText"
672
+ );
664
673
  const showCodeRowNumber = vue.inject("showCodeRowNumber");
665
674
  const themeRef = vue.inject("theme");
666
675
  const headsRef = vue.ref([]);
@@ -696,9 +705,15 @@ const useMarkdownIt = (props, previewOnly) => {
696
705
  options: { mdHeadingId: props.mdHeadingId, headsRef }
697
706
  },
698
707
  {
699
- type: "codeTabs",
700
- plugin: CodeTabsPlugin,
701
- options: { editorId }
708
+ type: "code",
709
+ plugin: CodePlugin,
710
+ options: {
711
+ editorId,
712
+ usedLanguageTextRef,
713
+ // showCodeRowNumber,
714
+ codeFoldable: props.codeFoldable,
715
+ autoFoldThreshold: props.autoFoldThreshold
716
+ }
702
717
  },
703
718
  {
704
719
  type: "xss",
@@ -771,7 +786,7 @@ const useMarkdownIt = (props, previewOnly) => {
771
786
  } else {
772
787
  codeHtml = md.utils.escapeHtml(str);
773
788
  }
774
- const codeSpan = showCodeRowNumber ? index.generateCodeRowNumber(codeHtml.replace(/^\n+|\n+$/g, "")) : `<span class="code-block">${codeHtml.replace(/^\n+|\n+$/g, "")}</span>`;
789
+ const codeSpan = showCodeRowNumber ? index.generateCodeRowNumber(codeHtml.replace(/^\n+|\n+$/g, "")) : `<span class="${config.prefix}-code-block">${codeHtml.replace(/^\n+|\n+$/g, "")}</span>`;
775
790
  return `<pre><code class="language-${language}" language=${language}>${codeSpan}</code></pre>`;
776
791
  }
777
792
  });
@@ -879,6 +894,12 @@ const contentPreviewProps = {
879
894
  },
880
895
  sanitizeMermaid: {
881
896
  type: Function
897
+ },
898
+ codeFoldable: {
899
+ type: Boolean
900
+ },
901
+ autoFoldThreshold: {
902
+ type: Number
882
903
  }
883
904
  };
884
905
  const contentProps = {
@@ -1586,6 +1607,25 @@ const mdPreviewProps = {
1586
1607
  sanitizeMermaid: {
1587
1608
  type: Function,
1588
1609
  default: (h) => Promise.resolve(h)
1610
+ },
1611
+ /**
1612
+ * 是否开启折叠代码功能
1613
+ * 不开启会使用div标签替代details标签
1614
+ *
1615
+ * @default true
1616
+ */
1617
+ codeFoldable: {
1618
+ type: Boolean,
1619
+ default: true
1620
+ },
1621
+ /**
1622
+ * 触发自动折叠代码的行数阈值
1623
+ *
1624
+ * @default 30
1625
+ */
1626
+ autoFoldThreshold: {
1627
+ type: Number,
1628
+ default: 30
1589
1629
  }
1590
1630
  };
1591
1631
  const editorProps = {
@@ -1921,7 +1961,9 @@ const MdPreview = /* @__PURE__ */ vue.defineComponent({
1921
1961
  "noHighlight": noHighlight,
1922
1962
  "noImgZoomIn": props.noImgZoomIn,
1923
1963
  "previewOnly": true,
1924
- "sanitizeMermaid": props.sanitizeMermaid
1964
+ "sanitizeMermaid": props.sanitizeMermaid,
1965
+ "codeFoldable": props.codeFoldable,
1966
+ "autoFoldThreshold": props.autoFoldThreshold
1925
1967
  }, null)]);
1926
1968
  };
1927
1969
  }
@@ -1,4 +1,5 @@
1
1
  "use strict";
2
+ const config = require("./config.cjs");
2
3
  const base642File = (base64, fileName = "image.png") => {
3
4
  const arr = base64.split(",");
4
5
  const regResult = arr[0].match(/:(.*?);/);
@@ -24,7 +25,7 @@ const generateCodeRowNumber = (code) => {
24
25
  rowNumberList.push("<span></span>");
25
26
  });
26
27
  rowNumberList.push("</span>");
27
- return `<span class="code-block">${code}</span>${rowNumberList.join("")}`;
28
+ return `<span class="${config.prefix}-code-block">${code}</span>${rowNumberList.join("")}`;
28
29
  };
29
30
  const getRelativeTop = (element, container) => {
30
31
  if (!element || !container) {
@@ -39,8 +40,8 @@ const getRelativeTop = (element, container) => {
39
40
  };
40
41
  const getNextId = /* @__PURE__ */ (() => {
41
42
  let count = 0;
42
- return (prefix) => {
43
- return prefix + ++count;
43
+ return (prefix2) => {
44
+ return prefix2 + ++count;
44
45
  };
45
46
  })();
46
47
  const getZIndexIncrement = /* @__PURE__ */ (() => {
@@ -2965,7 +2965,9 @@ const Content = /* @__PURE__ */ defineComponent({
2965
2965
  "formatCopiedText": props2.formatCopiedText,
2966
2966
  "noHighlight": props2.noHighlight,
2967
2967
  "noImgZoomIn": props2.noImgZoomIn,
2968
- "sanitizeMermaid": props2.sanitizeMermaid
2968
+ "sanitizeMermaid": props2.sanitizeMermaid,
2969
+ "codeFoldable": props2.codeFoldable,
2970
+ "autoFoldThreshold": props2.autoFoldThreshold
2969
2971
  }, null), props2.catalogVisible && createVNode(MdCatalog, {
2970
2972
  "theme": props2.theme,
2971
2973
  "class": `${prefix}-catalog-editor`,
@@ -3241,7 +3243,9 @@ const Editor = /* @__PURE__ */ defineComponent({
3241
3243
  ctx.emit("onInputBoxWitdhChange", width);
3242
3244
  },
3243
3245
  "sanitizeMermaid": props2.sanitizeMermaid,
3244
- "transformImgUrl": props2.transformImgUrl
3246
+ "transformImgUrl": props2.transformImgUrl,
3247
+ "codeFoldable": props2.codeFoldable,
3248
+ "autoFoldThreshold": props2.autoFoldThreshold
3245
3249
  }, null), props2.footers.length > 0 && createVNode(Footer, {
3246
3250
  "modelValue": props2.modelValue,
3247
3251
  "footers": props2.footers,
@@ -1,5 +1,5 @@
1
- import { inject, watch, toRef, onMounted, nextTick, shallowRef, ref, computed, defineComponent, createVNode, Fragment, reactive, provide, onBeforeUnmount } from "vue";
2
- import { f as configOption, p as prefix, g as codeCss, s as staticTextDefault, b as allToolbar, d as allFooter } from "./config.mjs";
1
+ import { inject, onMounted, watch, toRef, nextTick, shallowRef, ref, computed, defineComponent, createVNode, Fragment, reactive, provide, onBeforeUnmount } from "vue";
2
+ import { p as prefix, f as configOption, g as codeCss, s as staticTextDefault, b as allToolbar, d as allFooter } from "./config.mjs";
3
3
  import { b as bus, C as CATALOG_CHANGED, P as PUSH_CATALOG, R as RERENDER, B as BUILD_FINISHED, O as ON_SAVE, E as ERROR_CATCHER, U as UPLOAD_IMAGE, a as CHANGE_CATALOG_VISIBLE, c as PAGE_FULL_SCREEN_CHANGED, F as FULL_SCREEN_CHANGED, d as PREVIEW_CHANGED, e as PREVIEW_ONLY_CHANGED, H as HTML_PREVIEW_CHANGED, f as CATALOG_VISIBLE_CHANGED, g as REPLACE, h as CHANGE_FULL_SCREEN, i as EVENT_LISTENER } from "./event-name.mjs";
4
4
  import { a as appendHandler, u as updateHandler } from "./dom.mjs";
5
5
  import { debounce, uuid, deepMerge, deepClone } from "@vavt/util";
@@ -14,71 +14,46 @@ import { LRUCache } from "lru-cache";
14
14
  const userZoom = (props, html) => {
15
15
  const editorId = inject("editorId");
16
16
  const { noImgZoomIn } = props;
17
- let zoomHander = () => {
18
- };
19
- watch(
20
- [html, toRef(props.setting, "preview")],
21
- () => {
22
- if (!noImgZoomIn && props.setting.preview) {
23
- zoomHander();
24
- }
25
- },
26
- {
27
- immediate: true
17
+ const zoomHander = debounce(() => {
18
+ const imgs = document.querySelectorAll(`#${editorId}-preview img`);
19
+ if (imgs.length === 0) {
20
+ return;
28
21
  }
29
- );
30
- onMounted(() => {
31
- zoomHander = debounce(() => {
32
- const imgs = document.querySelectorAll(`#${editorId}-preview img`);
33
- if (imgs.length === 0) {
34
- return;
35
- }
36
- mediumZoom(imgs, {
37
- background: "#00000073"
38
- });
22
+ mediumZoom(imgs, {
23
+ background: "#00000073"
39
24
  });
40
25
  });
41
- };
42
- const StrIcon = (name, customIcon) => {
43
- if (typeof customIcon[name] === "string") {
44
- return customIcon[name];
45
- }
46
- const defaultStr = `<i class="${prefix}-iconfont ${prefix}-icon-${name}"></i>`;
47
- switch (configOption.iconfontType) {
48
- case "svg": {
49
- return `<svg class="${prefix}-icon" aria-hidden="true"><use xlink:href="#${prefix}-icon-${name}"></use></svg>`;
50
- }
51
- default: {
52
- return defaultStr;
26
+ onMounted(() => {
27
+ zoomHander();
28
+ });
29
+ watch([html, toRef(props.setting, "preview")], () => {
30
+ if (!noImgZoomIn && props.setting.preview) {
31
+ zoomHander();
53
32
  }
54
- }
33
+ });
55
34
  };
56
35
  const useCopyCode = (props, html, key) => {
57
36
  const editorId = inject("editorId");
58
37
  const ult = inject("usedLanguageText");
59
- const customIcon = inject("customIcon");
60
38
  const initCopyEntry = () => {
61
- document.querySelectorAll(`#${editorId}-preview pre`).forEach((pre) => {
62
- var _a;
39
+ document.querySelectorAll(`#${editorId} .${prefix}-preview .${prefix}-code`).forEach((codeBlock) => {
63
40
  let clearTimer = -1;
64
- (_a = pre.querySelector(".copy-button")) == null ? void 0 : _a.remove();
65
- const copyBtnText = ult.value.copyCode.text;
66
- const copyButton = document.createElement("span");
67
- copyButton.setAttribute("class", "copy-button");
68
- copyButton.dataset.tips = copyBtnText;
69
- copyButton.innerHTML = StrIcon("copy", customIcon.value);
70
- copyButton.addEventListener("click", () => {
71
- clearTimeout(clearTimer);
72
- const codeText = pre.querySelector("code").innerText;
73
- const success = copy(props.formatCopiedText(codeText));
74
- const succssTip = ult.value.copyCode.successTips;
75
- const failTip = ult.value.copyCode.failTips;
76
- copyButton.dataset.tips = success ? succssTip : failTip;
77
- clearTimer = window.setTimeout(() => {
78
- copyButton.dataset.tips = copyBtnText;
79
- }, 1500);
80
- });
81
- pre.appendChild(copyButton);
41
+ const copyButton = codeBlock.querySelector(
42
+ `.${prefix}-copy-button`
43
+ );
44
+ if (copyButton)
45
+ copyButton.onclick = (e) => {
46
+ e.preventDefault();
47
+ clearTimeout(clearTimer);
48
+ const activeCode = codeBlock.querySelector("input:checked + pre code") || codeBlock.querySelector("pre code");
49
+ const codeText = activeCode.textContent;
50
+ const success = copy(props.formatCopiedText(codeText));
51
+ const { text, successTips, failTips } = ult.value.copyCode;
52
+ copyButton.innerHTML = success ? successTips : failTips;
53
+ clearTimer = window.setTimeout(() => {
54
+ copyButton.innerHTML = text;
55
+ }, 1500);
56
+ };
82
57
  });
83
58
  };
84
59
  const htmlChanged = () => {
@@ -596,14 +571,31 @@ const codetabs = (md, _opts) => {
596
571
  return info ? info.split(/(\s+)/g)[0] : "";
597
572
  }
598
573
  const fenceGroup = (tokens, idx, options, env, slf) => {
574
+ var _a, _b;
599
575
  if (tokens[idx].hidden) {
600
576
  return "";
601
577
  }
602
- const [GROUP, _] = getGroupAndTab(tokens[idx]);
578
+ const [GROUP] = getGroupAndTab(tokens[idx]);
603
579
  if (GROUP === null) {
604
- return defaultRender(tokens, idx, options, env, slf);
580
+ const open2 = tokens[idx].content.trim().split("\n").length < _opts.autoFoldThreshold;
581
+ const tagContainer2 = _opts.codeFoldable ? "details" : "div", tagHeader2 = _opts.codeFoldable ? "summary" : "div";
582
+ const codeRendered = defaultRender(tokens, idx, options, env, slf);
583
+ return `<${tagContainer2} class="${prefix}-code"${open2 ? " open" : ""}>
584
+ <${tagHeader2} class="${prefix}-code-head">
585
+ <div class="${prefix}-code-flag">
586
+ <span></span>
587
+ <span></span>
588
+ <span></span>
589
+ </div>
590
+ <div>
591
+ <span class="${prefix}-code-lang">${tokens[idx].info.trim()}</span>
592
+ <span class="${prefix}-copy-button">${(_a = _opts.usedLanguageTextRef.value) == null ? void 0 : _a.copyCode.text}</span>
593
+ </div>
594
+ </${tagHeader2}>${codeRendered}</${tagContainer2}>`;
605
595
  }
606
- let token, group, tab, checked, labels = "", pres = "";
596
+ let token, group, tab, checked, labels = "", pres = "", langs = "";
597
+ const open = tokens[idx].content.trim().split("\n").length < _opts.autoFoldThreshold;
598
+ const tagContainer = _opts.codeFoldable ? "details" : "div", tagHeader = _opts.codeFoldable ? "summary" : "div";
607
599
  for (let i = idx; i < tokens.length; i++) {
608
600
  token = tokens[i];
609
601
  [group, tab] = getGroupAndTab(token);
@@ -612,17 +604,31 @@ const codetabs = (md, _opts) => {
612
604
  }
613
605
  token.info = token.info.replace(re, "");
614
606
  token.hidden = true;
615
- checked = i - idx > 0 ? "" : " checked";
616
- labels += `<li><input type="radio" name="label-group-${_opts.editorId}-${idx}"${checked}><label for="group-${_opts.editorId}-${idx}-tab-${i - idx}" onclick="this.previousElementSibling.click()">${tab || getLangName(token)}</label></li>
617
- `;
618
- pres += `<input type="radio" id="group-${_opts.editorId}-${idx}-tab-${i - idx}" name="group-${_opts.editorId}-${idx}"${checked}>
619
- ` + defaultRender(tokens, i, options, env, slf);
607
+ const className = `${prefix}-codetab-${_opts.editorId}-${idx}-${i - idx}`;
608
+ checked = i - idx > 0 ? "" : "checked";
609
+ labels += `<li>
610
+ <input type="radio" name="${prefix}-codetab-label-${_opts.editorId}-${idx}" class="${className}" ${checked}>
611
+ <label onclick="document.querySelectorAll('.${className}').forEach(e => e.click())">${tab || getLangName(token)}</label>
612
+ </li>`;
613
+ pres += `<input type="radio" name="${prefix}-codetab-pre-${_opts.editorId}-${idx}" class="${className}" ${checked}>
614
+ ${defaultRender(tokens, i, options, env, slf)}`;
615
+ langs += `<input type="radio" name="${prefix}-codetab-lang-${_opts.editorId}-${idx}" class="${className}" ${checked}>
616
+ <span class=${prefix}-code-lang>${getLangName(token)}</span>`;
620
617
  }
621
- return '<div class="code-tabs">\n<ul>\n' + labels + "</ul>\n" + pres + "</div>";
618
+ return `<${tagContainer} class="${prefix}-code" ${open ? " open" : ""}>
619
+ <${tagHeader} class="${prefix}-code-head">
620
+ <div class="${prefix}-code-flag">
621
+ <ul class="${prefix}-codetab-label">${labels}</ul>
622
+ </div>
623
+ <div>
624
+ <span class="${prefix}-codetab-lang">${langs}</span>
625
+ <span class="${prefix}-copy-button">${(_b = _opts.usedLanguageTextRef.value) == null ? void 0 : _b.copyCode.text}</span>
626
+ </div>
627
+ </${tagHeader}>${pres}</${tagContainer}>`;
622
628
  };
623
629
  md.renderer.rules.fence = fenceGroup;
624
630
  };
625
- const CodeTabsPlugin = codetabs;
631
+ const CodePlugin = codetabs;
626
632
  const initLineNumber = (md) => {
627
633
  [
628
634
  "paragraph_open",
@@ -660,6 +666,9 @@ const initLineNumber = (md) => {
660
666
  const useMarkdownIt = (props, previewOnly) => {
661
667
  const { editorConfig, markdownItConfig, markdownItPlugins } = configOption;
662
668
  const editorId = inject("editorId");
669
+ const usedLanguageTextRef = inject(
670
+ "usedLanguageText"
671
+ );
663
672
  const showCodeRowNumber = inject("showCodeRowNumber");
664
673
  const themeRef = inject("theme");
665
674
  const headsRef = ref([]);
@@ -695,9 +704,15 @@ const useMarkdownIt = (props, previewOnly) => {
695
704
  options: { mdHeadingId: props.mdHeadingId, headsRef }
696
705
  },
697
706
  {
698
- type: "codeTabs",
699
- plugin: CodeTabsPlugin,
700
- options: { editorId }
707
+ type: "code",
708
+ plugin: CodePlugin,
709
+ options: {
710
+ editorId,
711
+ usedLanguageTextRef,
712
+ // showCodeRowNumber,
713
+ codeFoldable: props.codeFoldable,
714
+ autoFoldThreshold: props.autoFoldThreshold
715
+ }
701
716
  },
702
717
  {
703
718
  type: "xss",
@@ -770,7 +785,7 @@ const useMarkdownIt = (props, previewOnly) => {
770
785
  } else {
771
786
  codeHtml = md.utils.escapeHtml(str);
772
787
  }
773
- const codeSpan = showCodeRowNumber ? generateCodeRowNumber(codeHtml.replace(/^\n+|\n+$/g, "")) : `<span class="code-block">${codeHtml.replace(/^\n+|\n+$/g, "")}</span>`;
788
+ const codeSpan = showCodeRowNumber ? generateCodeRowNumber(codeHtml.replace(/^\n+|\n+$/g, "")) : `<span class="${prefix}-code-block">${codeHtml.replace(/^\n+|\n+$/g, "")}</span>`;
774
789
  return `<pre><code class="language-${language}" language=${language}>${codeSpan}</code></pre>`;
775
790
  }
776
791
  });
@@ -878,6 +893,12 @@ const contentPreviewProps = {
878
893
  },
879
894
  sanitizeMermaid: {
880
895
  type: Function
896
+ },
897
+ codeFoldable: {
898
+ type: Boolean
899
+ },
900
+ autoFoldThreshold: {
901
+ type: Number
881
902
  }
882
903
  };
883
904
  const contentProps = {
@@ -1585,6 +1606,25 @@ const mdPreviewProps = {
1585
1606
  sanitizeMermaid: {
1586
1607
  type: Function,
1587
1608
  default: (h) => Promise.resolve(h)
1609
+ },
1610
+ /**
1611
+ * 是否开启折叠代码功能
1612
+ * 不开启会使用div标签替代details标签
1613
+ *
1614
+ * @default true
1615
+ */
1616
+ codeFoldable: {
1617
+ type: Boolean,
1618
+ default: true
1619
+ },
1620
+ /**
1621
+ * 触发自动折叠代码的行数阈值
1622
+ *
1623
+ * @default 30
1624
+ */
1625
+ autoFoldThreshold: {
1626
+ type: Number,
1627
+ default: 30
1588
1628
  }
1589
1629
  };
1590
1630
  const editorProps = {
@@ -1920,7 +1960,9 @@ const MdPreview = /* @__PURE__ */ defineComponent({
1920
1960
  "noHighlight": noHighlight,
1921
1961
  "noImgZoomIn": props.noImgZoomIn,
1922
1962
  "previewOnly": true,
1923
- "sanitizeMermaid": props.sanitizeMermaid
1963
+ "sanitizeMermaid": props.sanitizeMermaid,
1964
+ "codeFoldable": props.codeFoldable,
1965
+ "autoFoldThreshold": props.autoFoldThreshold
1924
1966
  }, null)]);
1925
1967
  };
1926
1968
  }
@@ -1,3 +1,4 @@
1
+ import { p as prefix } from "./config.mjs";
1
2
  const base642File = (base64, fileName = "image.png") => {
2
3
  const arr = base64.split(",");
3
4
  const regResult = arr[0].match(/:(.*?);/);
@@ -23,7 +24,7 @@ const generateCodeRowNumber = (code) => {
23
24
  rowNumberList.push("<span></span>");
24
25
  });
25
26
  rowNumberList.push("</span>");
26
- return `<span class="code-block">${code}</span>${rowNumberList.join("")}`;
27
+ return `<span class="${prefix}-code-block">${code}</span>${rowNumberList.join("")}`;
27
28
  };
28
29
  const getRelativeTop = (element, container) => {
29
30
  if (!element || !container) {
@@ -38,8 +39,8 @@ const getRelativeTop = (element, container) => {
38
39
  };
39
40
  const getNextId = /* @__PURE__ */ (() => {
40
41
  let count = 0;
41
- return (prefix) => {
42
- return prefix + ++count;
42
+ return (prefix2) => {
43
+ return prefix2 + ++count;
43
44
  };
44
45
  })();
45
46
  const getZIndexIncrement = /* @__PURE__ */ (() => {