@md-plugins/md-plugin-codeblocks 0.1.0-alpha.27 → 0.1.0-alpha.29

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.
Files changed (2) hide show
  1. package/dist/index.mjs +39 -16
  2. package/package.json +2 -2
package/dist/index.mjs CHANGED
@@ -1,6 +1,13 @@
1
1
  import prism from 'prismjs';
2
2
  import loadLanguages from 'prismjs/components/index.js';
3
3
 
4
+ function resolvePluginOptions(options, key, defaults) {
5
+ if (options && typeof options === "object" && key in options) {
6
+ return { ...defaults, ...options[key] };
7
+ }
8
+ return { ...defaults, ...options };
9
+ }
10
+
4
11
  const defaultLangList = [
5
12
  { name: "markup" },
6
13
  { name: "bash", customCopy: true },
@@ -16,23 +23,39 @@ const defaultLangList = [
16
23
  { name: "diff" }
17
24
  // special grammars
18
25
  ];
19
- const codeblocksPlugin = (md, {
20
- defaultLang = "markup",
21
- containerComponent = "MarkdownPrerender",
22
- copyButtonComponent = "MarkdownCopyButton",
23
- preClass = "markdown-code",
24
- codeClass = "",
25
- tabPanelTagName = "q-tab-panel",
26
- tabPanelTagClass = "q-pa-none",
27
- pageScripts = [
26
+ const DEFAULT_CODEBLOCK_PLUGIN_OPTIONS = {
27
+ defaultLang: "markup",
28
+ containerComponent: "MarkdownPrerender",
29
+ copyButtonComponent: "MarkdownCopyButton",
30
+ preClass: "markdown-code",
31
+ codeClass: "",
32
+ tabPanelTagName: "q-tab-panel",
33
+ tabPanelTagClass: "q-pa-none",
34
+ pageScripts: [
28
35
  "import MarkdownPrerender from 'src/.q-press/components/MarkdownPrerender'",
29
- // ts file
30
36
  "import MarkdownCopyButton from 'src/.q-press/components/MarkdownCopyButton.vue'"
31
37
  ],
32
- langList = defaultLangList
33
- } = {}) => {
34
- const customCopyLangList = langList.filter((l) => l.customCopy === true).map((l) => l.name);
38
+ langList: defaultLangList
39
+ };
40
+ const codeblocksPlugin = (md, options) => {
41
+ const resolvedOptions = resolvePluginOptions(
42
+ options,
43
+ "codeblocksPlugin",
44
+ DEFAULT_CODEBLOCK_PLUGIN_OPTIONS
45
+ );
46
+ const {
47
+ defaultLang = DEFAULT_CODEBLOCK_PLUGIN_OPTIONS.defaultLang,
48
+ containerComponent = DEFAULT_CODEBLOCK_PLUGIN_OPTIONS.containerComponent,
49
+ copyButtonComponent = DEFAULT_CODEBLOCK_PLUGIN_OPTIONS.copyButtonComponent,
50
+ preClass = DEFAULT_CODEBLOCK_PLUGIN_OPTIONS.preClass,
51
+ codeClass = DEFAULT_CODEBLOCK_PLUGIN_OPTIONS.codeClass,
52
+ tabPanelTagName = DEFAULT_CODEBLOCK_PLUGIN_OPTIONS.tabPanelTagName,
53
+ tabPanelTagClass = DEFAULT_CODEBLOCK_PLUGIN_OPTIONS.tabPanelTagClass,
54
+ pageScripts = DEFAULT_CODEBLOCK_PLUGIN_OPTIONS.pageScripts,
55
+ langList = defaultLangList
56
+ } = resolvedOptions;
35
57
  loadLanguages(langList.map((l) => l.name));
58
+ const customCopyLangList = langList.filter((l) => l.customCopy === true).map((l) => l.name);
36
59
  const langMatch = langList.map((l) => l.aliases || l.name).join("|");
37
60
  const definitionLineRE = new RegExp(
38
61
  `^(?<lang>(tabs|${langMatch}))(\\s+\\[(?<attrs>.*)\\])?(\\s+(?<title>.+))?$`
@@ -122,7 +145,7 @@ const codeblocksPlugin = (md, {
122
145
  const target = acc[lineIndex];
123
146
  if (target === undefined) return;
124
147
  target.prefix.push(
125
- target.classList.includes(`line-add`) === true ? "+" : target.classList.includes(`line-rem`) === true ? "-" : " "
148
+ target.classList.includes(`line-add`) ? "+" : target.classList.includes(`line-rem`) ? "-" : " "
126
149
  );
127
150
  });
128
151
  return acc;
@@ -188,9 +211,9 @@ const codeblocksPlugin = (md, {
188
211
  return "";
189
212
  }
190
213
  if (token.info === "") {
191
- token.info = defaultLang;
214
+ token.info = defaultLang ?? "markup";
192
215
  }
193
- if (pageScripts.length > 0) {
216
+ if (pageScripts && pageScripts.length > 0) {
194
217
  env.pageScripts = env.pageScripts || /* @__PURE__ */ new Set();
195
218
  for (const script of pageScripts) {
196
219
  env.pageScripts.add(script);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@md-plugins/md-plugin-codeblocks",
3
- "version": "0.1.0-alpha.27",
3
+ "version": "0.1.0-alpha.29",
4
4
  "description": "A markdown-it plugin for code blocks.",
5
5
  "keywords": [
6
6
  "markdown-it",
@@ -40,7 +40,7 @@
40
40
  "prismjs": "^1.29.0",
41
41
  "@types/markdown-it": "^14.1.2",
42
42
  "@types/prismjs": "^1.26.5",
43
- "@md-plugins/shared": "0.1.0-alpha.27"
43
+ "@md-plugins/shared": "0.1.0-alpha.29"
44
44
  },
45
45
  "peerDependencies": {
46
46
  "markdown-it": "^14.1.0",