markdown-it-any-block 3.3.6 → 3.3.7-beta1

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.
@@ -25,6 +25,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
25
25
  const MarkdownIt = require("markdown-it");
26
26
  let dom = null;
27
27
  async function jsdom_init(enable = true) {
28
+ if (document) return;
28
29
  const { default: jsdom } = await import("jsdom");
29
30
  const { JSDOM } = jsdom;
30
31
  dom = new JSDOM(`<!DOCTYPE html><html><body></body></html>`, {
@@ -34,6 +35,7 @@ async function jsdom_init(enable = true) {
34
35
  if (enable) jsdom_enable();
35
36
  }
36
37
  function jsdom_enable() {
38
+ if (!dom) return;
37
39
  global.Storage = dom.window.Storage;
38
40
  global.window = dom.window;
39
41
  global.history = dom.window.history;
@@ -52,6 +54,7 @@ function jsdom_enable() {
52
54
  global.MutationObserver = dom.window.MutationObserver;
53
55
  }
54
56
  function jsdom_disable() {
57
+ if (!dom) return;
55
58
  global.window = void 0;
56
59
  global.history = void 0;
57
60
  global.document = void 0;
@@ -49548,9 +49551,11 @@ function abRender_fence(md, options) {
49548
49551
  ab_header = match2[1];
49549
49552
  let ab_content = lines.join("\n");
49550
49553
  ab_content = ab_content.trimStart();
49554
+ jsdom_enable();
49551
49555
  const el = document.createElement("div");
49552
49556
  el.classList.add("ab-note", "drop-shadow");
49553
49557
  ABConvertManager.autoABConvert(el, ab_header, ab_content, token.markup.startsWith(":::") ? "mdit" : "");
49558
+ jsdom_disable();
49554
49559
  if (el.classList.contains("ab-raw")) {
49555
49560
  const subEl = el.querySelector(".ab-raw-data");
49556
49561
  if (subEl) {
@@ -1,6 +1,7 @@
1
1
  import MarkdownIt from "markdown-it";
2
2
  let dom = null;
3
3
  async function jsdom_init(enable = true) {
4
+ if (document) return;
4
5
  const { default: jsdom } = await import("jsdom");
5
6
  const { JSDOM } = jsdom;
6
7
  dom = new JSDOM(`<!DOCTYPE html><html><body></body></html>`, {
@@ -10,6 +11,7 @@ async function jsdom_init(enable = true) {
10
11
  if (enable) jsdom_enable();
11
12
  }
12
13
  function jsdom_enable() {
14
+ if (!dom) return;
13
15
  global.Storage = dom.window.Storage;
14
16
  global.window = dom.window;
15
17
  global.history = dom.window.history;
@@ -28,6 +30,7 @@ function jsdom_enable() {
28
30
  global.MutationObserver = dom.window.MutationObserver;
29
31
  }
30
32
  function jsdom_disable() {
33
+ if (!dom) return;
31
34
  global.window = void 0;
32
35
  global.history = void 0;
33
36
  global.document = void 0;
@@ -49524,9 +49527,11 @@ function abRender_fence(md, options) {
49524
49527
  ab_header = match2[1];
49525
49528
  let ab_content = lines.join("\n");
49526
49529
  ab_content = ab_content.trimStart();
49530
+ jsdom_enable();
49527
49531
  const el = document.createElement("div");
49528
49532
  el.classList.add("ab-note", "drop-shadow");
49529
49533
  ABConvertManager.autoABConvert(el, ab_header, ab_content, token.markup.startsWith(":::") ? "mdit" : "");
49534
+ jsdom_disable();
49530
49535
  if (el.classList.contains("ab-raw")) {
49531
49536
  const subEl = el.querySelector(".ab-raw-data");
49532
49537
  if (subEl) {
package/index_mdit.ts CHANGED
@@ -44,7 +44,7 @@
44
44
  import MarkdownIt from "markdown-it"
45
45
 
46
46
  // 2. markdown-it-container
47
- import MarkdownItConstructor from "markdown-it-container";
47
+ import MarkdownItConstructor from "markdown-it-container"
48
48
 
49
49
  // 3. markdown-it-anyblock 插件
50
50
  // import { ABConvertManager } from "./index"
@@ -65,6 +65,7 @@ import "../ABConverter/converter/abc_echarts"
65
65
  import "../ABConverter/converter/abc_plantuml" // 可选建议:
66
66
  import "../ABConverter/converter/abc_mermaid" // 可选建议:新版无额外依赖,旧版非 min 环境下 7.1MB
67
67
  import "../ABConverter/converter/abc_markmap" // 可选建议:1.3MB
68
+ import { jsdom_disable, jsdom_enable } from "./jsdom_init"
68
69
 
69
70
  interface Options {
70
71
  multiline: boolean;
@@ -372,10 +373,12 @@ function abRender_fence(md: MarkdownIt, options?: Partial<Options>): void {
372
373
  //`<!--afterbegin-->${rawCode}<!--beforeend--></div><!--afterend-->`
373
374
 
374
375
  // anyBlock专属渲染
376
+ jsdom_enable()
375
377
  const el: HTMLDivElement = document.createElement("div"); el.classList.add("ab-note", "drop-shadow");
376
378
  // 临时el,未appendClild到dom中,脱离作用域会自动销毁
377
379
  // 用临时el是因为 mdit render 是 md_str 转 html_str 的,而Ob和原插件那边是使用HTML类的,要兼容
378
380
  ABConvertManager.autoABConvert(el, ab_header, ab_content, token.markup.startsWith(':::')?'mdit':'')
381
+ jsdom_disable()
379
382
 
380
383
  // anyBlock特殊情况 - 需要再渲染 (ob不需要,主要是vuepress有些插件可以复用一下,并且处理mdit无客户端环境可能存在的问题)
381
384
  if (el.classList.contains("ab-raw")) {
package/jsdom_init.ts CHANGED
@@ -9,6 +9,7 @@
9
9
  let dom: any = null;
10
10
 
11
11
  export async function jsdom_init(enable: boolean = true) {
12
+ if (document) return // 客户端环境,无需 jsdom 环境,同时也避免误卸载 document 环境
12
13
  const { default: jsdom } = await import('jsdom') // 废弃,要同步,避免docuemnt初始化不及时
13
14
  const { JSDOM } = jsdom
14
15
  dom = new JSDOM(`<!DOCTYPE html><html><body></body></html>`, {
@@ -19,6 +20,7 @@ export async function jsdom_init(enable: boolean = true) {
19
20
 
20
21
  /// 启用 jsdom 环境
21
22
  export function jsdom_enable() {
23
+ if (!dom) return // console.error("jsdom_enable failed: please run jsdom_init first.") // 允许客户端中运行,不使用jsdom
22
24
  global.Storage = dom.window.Storage;
23
25
  global.window = dom.window as any
24
26
  global.history = dom.window.history // @warn 若缺少该行,则在mdit+build环境下,编译报错:ReferenceError: history is not defined
@@ -38,6 +40,7 @@ export function jsdom_enable() {
38
40
 
39
41
  /// 禁用 jsdom 环境
40
42
  export function jsdom_disable() {
43
+ if (!dom) return // console.error("jsdom_disable failed: please run jsdom_init first.") // 允许客户端中运行,不使用jsdom
41
44
  global.window = undefined
42
45
  global.history = undefined
43
46
  global.document = undefined
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "markdown-it-any-block",
3
- "version": "3.3.6",
3
+ "version": "3.3.7-beta1",
4
4
  "description": "You can flexibility to create a 'Block' by many means. It also provides many useful features, like `list to table`. (obsidian/markdown-it/vuepress plugin/app)",
5
5
  "types": "@types/index_mdit.d.ts",
6
6
  "type": "module",