@anyblock/remark-any-block 1.0.0-beta8 → 1.0.1-beta2
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 +14 -2
- package/anyblock.ts +73 -79
- package/dist/remark-any-block.cjs +39 -37
- package/dist/remark-any-block.js +39 -37
- package/index.ts +3 -3
- package/jsdom_init.ts +16 -2
- package/package.json +45 -44
package/README.md
CHANGED
|
@@ -2,9 +2,21 @@
|
|
|
2
2
|
|
|
3
3
|
## remark-any-block 使用
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
当前使用 Remark 解析/渲染引擎的主流 SSG (静态网站生成器) 有:
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
- Astro
|
|
8
|
+
- Quartz4: 包含 OFM (Obsidian风格Markdown) 相关的插件
|
|
9
|
+
- Docusaurus: 包含 MDX 相关插件
|
|
10
|
+
|
|
11
|
+
> [!WARNING]
|
|
12
|
+
> 暂时不属于完全通用的 Remark 插件 (doing, 或者你自己改改), 当前该插件测试于 Quartz V4 版本
|
|
13
|
+
> 详见 [any-block/QuartzDemo](https://github.com/any-block/QuartzDemo)
|
|
14
|
+
>
|
|
15
|
+
> Quartz V4 在 remark 的基础上又封装了一层
|
|
16
|
+
|
|
17
|
+
## 使用案例/示例
|
|
18
|
+
|
|
19
|
+
见 [any-block/QuartzDemo](https://github.com/any-block/QuartzDemo)
|
|
8
20
|
|
|
9
21
|
## 构建
|
|
10
22
|
|
package/anyblock.ts
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
import { Plugin } from "unified"
|
|
2
2
|
import { Root, RootContent, Paragraph, Text, Code, Html } from "mdast"
|
|
3
3
|
import type { VFile } from "vfile"
|
|
4
|
-
import { toMarkdown } from "mdast-util-to-markdown"
|
|
5
4
|
import { visit } from "unist-util-visit"
|
|
5
|
+
import { toMarkdown } from "mdast-util-to-markdown" // TODO 这里好像会有 document 依赖
|
|
6
|
+
// 而且不一定能反序列化成功 (有私有节点类型,甚至table类型都不能识别)
|
|
7
|
+
// 后期需要去除此 "修改树" 的 `transformer` / `mdast-util` 插件
|
|
8
|
+
// 修改成 `micromarkExtensions` 形式的插件
|
|
6
9
|
|
|
7
10
|
// 这里不想去依赖 Quartz 项目,所以用any。但是你可以去看具体的类型定义
|
|
8
11
|
// import { type QuartzTransformerPlugin } from "../types"
|
|
@@ -54,15 +57,18 @@ export interface AnyBlockOptions {
|
|
|
54
57
|
*/
|
|
55
58
|
function matchAbHeader(node: RootContent): string | null {
|
|
56
59
|
if (node.type !== "paragraph") return null;
|
|
60
|
+
|
|
57
61
|
const text = (node.children as RootContent[])
|
|
58
62
|
.map((c) => (c.type === "text" ? (c as Text).value : ""))
|
|
59
63
|
.join("");
|
|
60
|
-
const
|
|
61
|
-
|
|
64
|
+
const match = text.match(ABReg.reg_header_noprefix);
|
|
65
|
+
if (!match || !match.length) return null
|
|
66
|
+
|
|
67
|
+
return match[5]
|
|
62
68
|
}
|
|
63
69
|
|
|
64
70
|
/**
|
|
65
|
-
* 检测 `:::container`
|
|
71
|
+
* 检测 `:::container` 首段落
|
|
66
72
|
* 匹配时返回 `{flag, type}`
|
|
67
73
|
*/
|
|
68
74
|
function matchContainerStart(node: RootContent):
|
|
@@ -79,9 +85,9 @@ function matchContainerStart(node: RootContent):
|
|
|
79
85
|
}
|
|
80
86
|
|
|
81
87
|
/**
|
|
82
|
-
*
|
|
88
|
+
* 检测 `:::container` 尾段落
|
|
83
89
|
*/
|
|
84
|
-
function
|
|
90
|
+
function matchContainerEnd(node: RootContent, flag: string): boolean {
|
|
85
91
|
if (node.type !== "paragraph") return false;
|
|
86
92
|
const text = (node.children as RootContent[])
|
|
87
93
|
.map((c) => (c.type === "text" ? (c as Text).value : ""))
|
|
@@ -91,7 +97,7 @@ function isContainerEnd(node: RootContent, flag: string): boolean {
|
|
|
91
97
|
}
|
|
92
98
|
|
|
93
99
|
/**
|
|
94
|
-
* 将一组 mdast
|
|
100
|
+
* 将一组 mdast 节点反序列化为 markdown 格式
|
|
95
101
|
*/
|
|
96
102
|
function nodesToMarkdown(nodes: RootContent[]): string {
|
|
97
103
|
return toMarkdown(
|
|
@@ -107,86 +113,73 @@ function nodesToMarkdown(nodes: RootContent[]): string {
|
|
|
107
113
|
* - `:::type ... :::`
|
|
108
114
|
*/
|
|
109
115
|
export const remark_anyblock_to_codeblock: Plugin<[Partial<AnyBlockOptions>?], Root> =
|
|
110
|
-
(_options = {}) =>
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
116
|
+
(_options = {}) => (tree) =>
|
|
117
|
+
{
|
|
118
|
+
const children = [...tree.children] as RootContent[];
|
|
119
|
+
|
|
120
|
+
const out: RootContent[] = [];
|
|
121
|
+
for (let i = 0; i < children.length; i++) {
|
|
122
|
+
const node = children[i];
|
|
123
|
+
|
|
124
|
+
// step1. 检测 `[]` 语法
|
|
125
|
+
const header = matchAbHeader(node);
|
|
126
|
+
if (header) {
|
|
127
|
+
const node_next = children[i+1];
|
|
128
|
+
if (
|
|
129
|
+
node_next.type === "list" ||
|
|
130
|
+
node_next.type === "heading" ||
|
|
131
|
+
node_next.type === "code" ||
|
|
132
|
+
node_next.type === "blockquote"
|
|
133
|
+
// node_next.type === "table"
|
|
134
|
+
) {
|
|
135
|
+
const codeValue = `[${header}]\n${nodesToMarkdown([node_next])}`;
|
|
136
|
+
out.push({
|
|
137
|
+
type: "code",
|
|
138
|
+
lang: "anyblock",
|
|
139
|
+
value: codeValue,
|
|
140
|
+
data: { markup: "[]" },
|
|
141
|
+
} as Code);
|
|
142
|
+
i++; continue;
|
|
143
|
+
} else {}
|
|
144
|
+
}
|
|
117
145
|
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
n.type === "list" ||
|
|
127
|
-
n.type === "heading" ||
|
|
128
|
-
n.type === "code" ||
|
|
129
|
-
n.type === "blockquote" ||
|
|
130
|
-
n.type === "table"
|
|
131
|
-
) {
|
|
132
|
-
body.push(n);
|
|
133
|
-
continue;
|
|
134
|
-
}
|
|
135
|
-
// stop when first non-matching block is hit
|
|
146
|
+
// step2. 检测 `:::` 语法
|
|
147
|
+
const container = matchContainerStart(node);
|
|
148
|
+
if (container) {
|
|
149
|
+
const body: RootContent[] = [];
|
|
150
|
+
let j = i + 1;
|
|
151
|
+
for (; j < children.length; j++) {
|
|
152
|
+
const n = children[j];
|
|
153
|
+
if (matchContainerEnd(n, container.flag)) {
|
|
136
154
|
break;
|
|
137
155
|
}
|
|
138
|
-
|
|
139
|
-
const codeValue = `[${header}]\n${nodesToMarkdown(body)}`;
|
|
140
|
-
out.push({
|
|
141
|
-
type: "code",
|
|
142
|
-
lang: "AnyBlock",
|
|
143
|
-
value: codeValue,
|
|
144
|
-
data: { markup: "[]" },
|
|
145
|
-
} as Code);
|
|
146
|
-
i = j - 1;
|
|
147
|
-
continue;
|
|
148
|
-
}
|
|
156
|
+
body.push(n);
|
|
149
157
|
}
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
break;
|
|
160
|
-
}
|
|
161
|
-
body.push(n);
|
|
162
|
-
}
|
|
163
|
-
if (j < children.length) {
|
|
164
|
-
const codeValue = `[${container.type}]\n${nodesToMarkdown(body)}`;
|
|
165
|
-
out.push({
|
|
166
|
-
type: "code",
|
|
167
|
-
lang: "AnyBlock",
|
|
168
|
-
value: codeValue,
|
|
169
|
-
data: { markup: container.flag },
|
|
170
|
-
} as Code);
|
|
171
|
-
i = j; // skip closing line
|
|
172
|
-
continue;
|
|
173
|
-
}
|
|
158
|
+
if (j < children.length) {
|
|
159
|
+
const codeValue = `[${container.type}]\n${nodesToMarkdown(body)}`;
|
|
160
|
+
out.push({
|
|
161
|
+
type: "code",
|
|
162
|
+
lang: "anyblock",
|
|
163
|
+
value: codeValue,
|
|
164
|
+
data: { markup: container.flag },
|
|
165
|
+
} as Code);
|
|
166
|
+
i = j; continue;
|
|
174
167
|
}
|
|
175
|
-
|
|
176
|
-
// default passthrough
|
|
177
|
-
out.push(node);
|
|
178
168
|
}
|
|
179
169
|
|
|
180
|
-
|
|
170
|
+
// step3. 不处理的节点,保持不变
|
|
171
|
+
out.push(node)
|
|
181
172
|
}
|
|
182
173
|
|
|
174
|
+
(tree as Root).children = out;
|
|
175
|
+
}
|
|
176
|
+
|
|
183
177
|
// 渲染 anyblock 代码块
|
|
184
|
-
const remark_anyblock_render_codeblock = () => {
|
|
178
|
+
export const remark_anyblock_render_codeblock = () => {
|
|
185
179
|
if (typeof document == "undefined") return
|
|
186
180
|
return (tree: Root, _file: VFile) => {
|
|
187
181
|
visit(tree, "code", (node: Code, index: number|undefined, parent: any|undefined) => { // 遍历所有的 code 类型节点
|
|
188
|
-
|
|
189
|
-
if (node.lang != "anyblock") return
|
|
182
|
+
if (typeof node.lang != "string" || node.lang.toLowerCase() != "anyblock") return
|
|
190
183
|
if (!parent || !index) return
|
|
191
184
|
|
|
192
185
|
const lines = node.value.split("\n")
|
|
@@ -203,8 +196,6 @@ const remark_anyblock_render_codeblock = () => {
|
|
|
203
196
|
ABConvertManager.autoABConvert(el, header, content, markup.startsWith(":::") ? "mdit" : "");
|
|
204
197
|
|
|
205
198
|
// new node
|
|
206
|
-
console.log("\nanyblock codeblock transformer visit2:", header, 'c==', content)
|
|
207
|
-
console.log("\nanyblock codeblock transformer visit3:", el.outerHTML, el)
|
|
208
199
|
const new_node: Html = {
|
|
209
200
|
type: 'html',
|
|
210
201
|
value: el.outerHTML,
|
|
@@ -239,13 +230,16 @@ const remark_anyblock_render_codeblock = () => {
|
|
|
239
230
|
})
|
|
240
231
|
}
|
|
241
232
|
|
|
242
|
-
|
|
243
|
-
|
|
233
|
+
/** 这是 Quartz 的 Transformer 插件定义
|
|
234
|
+
*
|
|
235
|
+
* 仅 Quartz 项目可用,其他 Remark 项目 (如 Astro、Docusaurus) 不需要用到这个
|
|
236
|
+
*/
|
|
237
|
+
export const quartz_transformer_anyblock: QuartzTransformerPlugin = (/*options: any*/) => {
|
|
244
238
|
return {
|
|
245
239
|
name: "AnyBlock",
|
|
246
240
|
markdownPlugins(_ctx: BuildCtx) {
|
|
247
241
|
return [
|
|
248
|
-
// remark_anyblock_to_codeblock,
|
|
242
|
+
// remark_anyblock_to_codeblock, // 取消注释则用库的地方会报错找不到 document
|
|
249
243
|
remark_anyblock_render_codeblock, // last
|
|
250
244
|
]
|
|
251
245
|
},
|
|
@@ -23,13 +23,17 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
23
23
|
));
|
|
24
24
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
25
25
|
const MarkdownIt = require("markdown-it");
|
|
26
|
+
let dom = null;
|
|
26
27
|
async function jsdom_init() {
|
|
27
28
|
const { default: jsdom } = await import("jsdom");
|
|
28
29
|
const { JSDOM } = jsdom;
|
|
29
|
-
|
|
30
|
+
dom = new JSDOM(`<!DOCTYPE html><html><body></body></html>`, {
|
|
30
31
|
url: "http://localhost/"
|
|
31
32
|
// @warn 若缺少该行,则在mdit+build环境下,编译报错
|
|
32
33
|
});
|
|
34
|
+
jsdom_able();
|
|
35
|
+
}
|
|
36
|
+
async function jsdom_able() {
|
|
33
37
|
global.Storage = dom.window.Storage;
|
|
34
38
|
global.window = dom.window;
|
|
35
39
|
global.history = dom.window.history;
|
|
@@ -17503,16 +17507,16 @@ const DomUtils = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProp
|
|
|
17503
17507
|
textContent,
|
|
17504
17508
|
uniqueSort
|
|
17505
17509
|
}, Symbol.toStringTag, { value: "Module" }));
|
|
17506
|
-
function render(that,
|
|
17510
|
+
function render(that, dom2, options) {
|
|
17507
17511
|
if (!that)
|
|
17508
17512
|
return "";
|
|
17509
|
-
return that(
|
|
17513
|
+
return that(dom2 !== null && dom2 !== void 0 ? dom2 : that._root.children, null, void 0, options).toString();
|
|
17510
17514
|
}
|
|
17511
|
-
function isOptions(
|
|
17512
|
-
return typeof
|
|
17515
|
+
function isOptions(dom2, options) {
|
|
17516
|
+
return typeof dom2 === "object" && dom2 != null && !("length" in dom2) && !("type" in dom2);
|
|
17513
17517
|
}
|
|
17514
|
-
function html$1(
|
|
17515
|
-
const toRender = isOptions(
|
|
17518
|
+
function html$1(dom2, options) {
|
|
17519
|
+
const toRender = isOptions(dom2) ? (options = dom2, void 0) : dom2;
|
|
17516
17520
|
const opts = {
|
|
17517
17521
|
...defaultOpts$2,
|
|
17518
17522
|
...this === null || this === void 0 ? void 0 : this._options,
|
|
@@ -17520,9 +17524,9 @@ function html$1(dom, options) {
|
|
|
17520
17524
|
};
|
|
17521
17525
|
return render(this, toRender, opts);
|
|
17522
17526
|
}
|
|
17523
|
-
function xml(
|
|
17527
|
+
function xml(dom2) {
|
|
17524
17528
|
const options = { ...this._options, xmlMode: true };
|
|
17525
|
-
return render(this,
|
|
17529
|
+
return render(this, dom2, options);
|
|
17526
17530
|
}
|
|
17527
17531
|
function text$1(elements) {
|
|
17528
17532
|
const elems = elements ? elements : this ? this.root() : [];
|
|
@@ -17612,8 +17616,8 @@ function domEach(array, fn) {
|
|
|
17612
17616
|
fn(array[i], i);
|
|
17613
17617
|
return array;
|
|
17614
17618
|
}
|
|
17615
|
-
function cloneDom(
|
|
17616
|
-
const clone2 = "length" in
|
|
17619
|
+
function cloneDom(dom2) {
|
|
17620
|
+
const clone2 = "length" in dom2 ? Array.prototype.map.call(dom2, (el) => cloneNode(el, true)) : [cloneNode(dom2, true)];
|
|
17617
17621
|
const root2 = new Document$1(clone2);
|
|
17618
17622
|
clone2.forEach((node) => {
|
|
17619
17623
|
node.parent = root2;
|
|
@@ -19844,8 +19848,8 @@ function _insert(concatenator) {
|
|
|
19844
19848
|
if (!hasChildren(el))
|
|
19845
19849
|
return;
|
|
19846
19850
|
const domSrc = typeof elems[0] === "function" ? elems[0].call(el, i, this._render(el.children)) : elems;
|
|
19847
|
-
const
|
|
19848
|
-
concatenator(
|
|
19851
|
+
const dom2 = this._makeDomArray(domSrc, i < lastIdx);
|
|
19852
|
+
concatenator(dom2, el.children, el);
|
|
19849
19853
|
});
|
|
19850
19854
|
};
|
|
19851
19855
|
}
|
|
@@ -19899,11 +19903,11 @@ function prependTo(target) {
|
|
|
19899
19903
|
prependTarget.prepend(this);
|
|
19900
19904
|
return this;
|
|
19901
19905
|
}
|
|
19902
|
-
const append = _insert((
|
|
19903
|
-
uniqueSplice(children2, children2.length, 0,
|
|
19906
|
+
const append = _insert((dom2, children2, parent2) => {
|
|
19907
|
+
uniqueSplice(children2, children2.length, 0, dom2, parent2);
|
|
19904
19908
|
});
|
|
19905
|
-
const prepend = _insert((
|
|
19906
|
-
uniqueSplice(children2, 0, 0,
|
|
19909
|
+
const prepend = _insert((dom2, children2, parent2) => {
|
|
19910
|
+
uniqueSplice(children2, 0, 0, dom2, parent2);
|
|
19907
19911
|
});
|
|
19908
19912
|
function _wrap(insert) {
|
|
19909
19913
|
return function(wrapper) {
|
|
@@ -19988,8 +19992,8 @@ function after(...elems) {
|
|
|
19988
19992
|
if (index2 < 0)
|
|
19989
19993
|
return;
|
|
19990
19994
|
const domSrc = typeof elems[0] === "function" ? elems[0].call(el, i, this._render(el.children)) : elems;
|
|
19991
|
-
const
|
|
19992
|
-
uniqueSplice(siblings2, index2 + 1, 0,
|
|
19995
|
+
const dom2 = this._makeDomArray(domSrc, i < lastIdx);
|
|
19996
|
+
uniqueSplice(siblings2, index2 + 1, 0, dom2, parent2);
|
|
19993
19997
|
});
|
|
19994
19998
|
}
|
|
19995
19999
|
function insertAfter(target) {
|
|
@@ -20025,8 +20029,8 @@ function before(...elems) {
|
|
|
20025
20029
|
if (index2 < 0)
|
|
20026
20030
|
return;
|
|
20027
20031
|
const domSrc = typeof elems[0] === "function" ? elems[0].call(el, i, this._render(el.children)) : elems;
|
|
20028
|
-
const
|
|
20029
|
-
uniqueSplice(siblings2, index2, 0,
|
|
20032
|
+
const dom2 = this._makeDomArray(domSrc, i < lastIdx);
|
|
20033
|
+
uniqueSplice(siblings2, index2, 0, dom2, parent2);
|
|
20030
20034
|
});
|
|
20031
20035
|
}
|
|
20032
20036
|
function insertBefore(target) {
|
|
@@ -20064,11 +20068,11 @@ function replaceWith(content) {
|
|
|
20064
20068
|
}
|
|
20065
20069
|
const siblings2 = parent2.children;
|
|
20066
20070
|
const cont = typeof content === "function" ? content.call(el, i, el) : content;
|
|
20067
|
-
const
|
|
20068
|
-
update(
|
|
20071
|
+
const dom2 = this._makeDomArray(cont);
|
|
20072
|
+
update(dom2, null);
|
|
20069
20073
|
const index2 = siblings2.indexOf(el);
|
|
20070
|
-
uniqueSplice(siblings2, index2, 1,
|
|
20071
|
-
if (!
|
|
20074
|
+
uniqueSplice(siblings2, index2, 1, dom2, parent2);
|
|
20075
|
+
if (!dom2.includes(el)) {
|
|
20072
20076
|
el.parent = el.prev = el.next = null;
|
|
20073
20077
|
}
|
|
20074
20078
|
});
|
|
@@ -20304,8 +20308,8 @@ function getLoad(parse2, render2) {
|
|
|
20304
20308
|
_parse(content2, options2, isDocument3, context) {
|
|
20305
20309
|
return parse2(content2, options2, isDocument3, context);
|
|
20306
20310
|
}
|
|
20307
|
-
_render(
|
|
20308
|
-
return render2(
|
|
20311
|
+
_render(dom2) {
|
|
20312
|
+
return render2(dom2, this.options);
|
|
20309
20313
|
}
|
|
20310
20314
|
}
|
|
20311
20315
|
function initialize(selector, context, root2 = initialRoot, opts) {
|
|
@@ -28492,8 +28496,8 @@ function parseWithParse5(content, options, isDocument2, context) {
|
|
|
28492
28496
|
return isDocument2 ? parse$2(content, opts) : parseFragment(context, content, opts);
|
|
28493
28497
|
}
|
|
28494
28498
|
const renderOpts = { treeAdapter: adapter };
|
|
28495
|
-
function renderWithParse5(
|
|
28496
|
-
const nodes = "length" in
|
|
28499
|
+
function renderWithParse5(dom2) {
|
|
28500
|
+
const nodes = "length" in dom2 ? dom2 : [dom2];
|
|
28497
28501
|
for (let index2 = 0; index2 < nodes.length; index2 += 1) {
|
|
28498
28502
|
const node = nodes[index2];
|
|
28499
28503
|
if (isDocument$1(node)) {
|
|
@@ -29745,7 +29749,7 @@ function parseDocument$1(data2, options) {
|
|
|
29745
29749
|
return handler.root;
|
|
29746
29750
|
}
|
|
29747
29751
|
const parse$1 = getParse((content, options, isDocument2, context) => options.xmlMode || options._useHtmlParser2 ? parseDocument$1(content, options) : parseWithParse5(content, options, isDocument2, context));
|
|
29748
|
-
const load = getLoad(parse$1, (
|
|
29752
|
+
const load = getLoad(parse$1, (dom2, options) => options.xmlMode || options._useHtmlParser2 ? render$1(dom2, options) : renderWithParse5(dom2));
|
|
29749
29753
|
load([]);
|
|
29750
29754
|
const defaultSelectorRules = {
|
|
29751
29755
|
"div,p": ({ $node }) => ({
|
|
@@ -49621,8 +49625,7 @@ const remark_anyblock_render_codeblock = () => {
|
|
|
49621
49625
|
if (typeof document == "undefined") return;
|
|
49622
49626
|
return (tree, _file) => {
|
|
49623
49627
|
visit$1(tree, "code", (node, index2, parent2) => {
|
|
49624
|
-
|
|
49625
|
-
if (node.lang != "anyblock") return;
|
|
49628
|
+
if (typeof node.lang != "string" || node.lang.toLowerCase() != "anyblock") return;
|
|
49626
49629
|
if (!parent2 || !index2) return;
|
|
49627
49630
|
const lines = node.value.split("\n");
|
|
49628
49631
|
const head = lines.shift();
|
|
@@ -49634,8 +49637,6 @@ const remark_anyblock_render_codeblock = () => {
|
|
|
49634
49637
|
const el = document.createElement("div");
|
|
49635
49638
|
el.classList.add("ab-note", "drop-shadow");
|
|
49636
49639
|
ABConvertManager.autoABConvert(el, header, content, markup.startsWith(":::") ? "mdit" : "");
|
|
49637
|
-
console.log("\nanyblock codeblock transformer visit2:", header, "c==", content);
|
|
49638
|
-
console.log("\nanyblock codeblock transformer visit3:", el.outerHTML, el);
|
|
49639
49640
|
const new_node = {
|
|
49640
49641
|
type: "html",
|
|
49641
49642
|
value: el.outerHTML
|
|
@@ -49654,12 +49655,12 @@ const remark_anyblock_render_codeblock = () => {
|
|
|
49654
49655
|
el_child.innerHTML = result;
|
|
49655
49656
|
});
|
|
49656
49657
|
}
|
|
49657
|
-
const
|
|
49658
|
+
const quartz_transformer_anyblock = () => {
|
|
49658
49659
|
return {
|
|
49659
49660
|
name: "AnyBlock",
|
|
49660
49661
|
markdownPlugins(_ctx) {
|
|
49661
49662
|
return [
|
|
49662
|
-
// remark_anyblock_to_codeblock,
|
|
49663
|
+
// remark_anyblock_to_codeblock, // 取消注释则用库的地方会报错找不到 document
|
|
49663
49664
|
remark_anyblock_render_codeblock
|
|
49664
49665
|
// last
|
|
49665
49666
|
];
|
|
@@ -49672,5 +49673,6 @@ const transformer_anyblock = () => {
|
|
|
49672
49673
|
exports.ABConvertManager = ABConvertManager;
|
|
49673
49674
|
exports.abConvertEvent = abConvertEvent;
|
|
49674
49675
|
exports.jsdom_init = jsdom_init;
|
|
49675
|
-
exports.
|
|
49676
|
+
exports.remark_anyblock_render_codeblock = remark_anyblock_render_codeblock;
|
|
49677
|
+
exports.transformer_anyblock = quartz_transformer_anyblock;
|
|
49676
49678
|
//# sourceMappingURL=remark-any-block.cjs.map
|
package/dist/remark-any-block.js
CHANGED
|
@@ -1,11 +1,15 @@
|
|
|
1
1
|
import MarkdownIt from "markdown-it";
|
|
2
|
+
let dom = null;
|
|
2
3
|
async function jsdom_init() {
|
|
3
4
|
const { default: jsdom } = await import("jsdom");
|
|
4
5
|
const { JSDOM } = jsdom;
|
|
5
|
-
|
|
6
|
+
dom = new JSDOM(`<!DOCTYPE html><html><body></body></html>`, {
|
|
6
7
|
url: "http://localhost/"
|
|
7
8
|
// @warn 若缺少该行,则在mdit+build环境下,编译报错
|
|
8
9
|
});
|
|
10
|
+
jsdom_able();
|
|
11
|
+
}
|
|
12
|
+
async function jsdom_able() {
|
|
9
13
|
global.Storage = dom.window.Storage;
|
|
10
14
|
global.window = dom.window;
|
|
11
15
|
global.history = dom.window.history;
|
|
@@ -17479,16 +17483,16 @@ const DomUtils = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProp
|
|
|
17479
17483
|
textContent,
|
|
17480
17484
|
uniqueSort
|
|
17481
17485
|
}, Symbol.toStringTag, { value: "Module" }));
|
|
17482
|
-
function render(that,
|
|
17486
|
+
function render(that, dom2, options) {
|
|
17483
17487
|
if (!that)
|
|
17484
17488
|
return "";
|
|
17485
|
-
return that(
|
|
17489
|
+
return that(dom2 !== null && dom2 !== void 0 ? dom2 : that._root.children, null, void 0, options).toString();
|
|
17486
17490
|
}
|
|
17487
|
-
function isOptions(
|
|
17488
|
-
return typeof
|
|
17491
|
+
function isOptions(dom2, options) {
|
|
17492
|
+
return typeof dom2 === "object" && dom2 != null && !("length" in dom2) && !("type" in dom2);
|
|
17489
17493
|
}
|
|
17490
|
-
function html$1(
|
|
17491
|
-
const toRender = isOptions(
|
|
17494
|
+
function html$1(dom2, options) {
|
|
17495
|
+
const toRender = isOptions(dom2) ? (options = dom2, void 0) : dom2;
|
|
17492
17496
|
const opts = {
|
|
17493
17497
|
...defaultOpts$2,
|
|
17494
17498
|
...this === null || this === void 0 ? void 0 : this._options,
|
|
@@ -17496,9 +17500,9 @@ function html$1(dom, options) {
|
|
|
17496
17500
|
};
|
|
17497
17501
|
return render(this, toRender, opts);
|
|
17498
17502
|
}
|
|
17499
|
-
function xml(
|
|
17503
|
+
function xml(dom2) {
|
|
17500
17504
|
const options = { ...this._options, xmlMode: true };
|
|
17501
|
-
return render(this,
|
|
17505
|
+
return render(this, dom2, options);
|
|
17502
17506
|
}
|
|
17503
17507
|
function text$1(elements) {
|
|
17504
17508
|
const elems = elements ? elements : this ? this.root() : [];
|
|
@@ -17588,8 +17592,8 @@ function domEach(array, fn) {
|
|
|
17588
17592
|
fn(array[i], i);
|
|
17589
17593
|
return array;
|
|
17590
17594
|
}
|
|
17591
|
-
function cloneDom(
|
|
17592
|
-
const clone2 = "length" in
|
|
17595
|
+
function cloneDom(dom2) {
|
|
17596
|
+
const clone2 = "length" in dom2 ? Array.prototype.map.call(dom2, (el) => cloneNode(el, true)) : [cloneNode(dom2, true)];
|
|
17593
17597
|
const root2 = new Document$1(clone2);
|
|
17594
17598
|
clone2.forEach((node) => {
|
|
17595
17599
|
node.parent = root2;
|
|
@@ -19820,8 +19824,8 @@ function _insert(concatenator) {
|
|
|
19820
19824
|
if (!hasChildren(el))
|
|
19821
19825
|
return;
|
|
19822
19826
|
const domSrc = typeof elems[0] === "function" ? elems[0].call(el, i, this._render(el.children)) : elems;
|
|
19823
|
-
const
|
|
19824
|
-
concatenator(
|
|
19827
|
+
const dom2 = this._makeDomArray(domSrc, i < lastIdx);
|
|
19828
|
+
concatenator(dom2, el.children, el);
|
|
19825
19829
|
});
|
|
19826
19830
|
};
|
|
19827
19831
|
}
|
|
@@ -19875,11 +19879,11 @@ function prependTo(target) {
|
|
|
19875
19879
|
prependTarget.prepend(this);
|
|
19876
19880
|
return this;
|
|
19877
19881
|
}
|
|
19878
|
-
const append = _insert((
|
|
19879
|
-
uniqueSplice(children2, children2.length, 0,
|
|
19882
|
+
const append = _insert((dom2, children2, parent2) => {
|
|
19883
|
+
uniqueSplice(children2, children2.length, 0, dom2, parent2);
|
|
19880
19884
|
});
|
|
19881
|
-
const prepend = _insert((
|
|
19882
|
-
uniqueSplice(children2, 0, 0,
|
|
19885
|
+
const prepend = _insert((dom2, children2, parent2) => {
|
|
19886
|
+
uniqueSplice(children2, 0, 0, dom2, parent2);
|
|
19883
19887
|
});
|
|
19884
19888
|
function _wrap(insert) {
|
|
19885
19889
|
return function(wrapper) {
|
|
@@ -19964,8 +19968,8 @@ function after(...elems) {
|
|
|
19964
19968
|
if (index2 < 0)
|
|
19965
19969
|
return;
|
|
19966
19970
|
const domSrc = typeof elems[0] === "function" ? elems[0].call(el, i, this._render(el.children)) : elems;
|
|
19967
|
-
const
|
|
19968
|
-
uniqueSplice(siblings2, index2 + 1, 0,
|
|
19971
|
+
const dom2 = this._makeDomArray(domSrc, i < lastIdx);
|
|
19972
|
+
uniqueSplice(siblings2, index2 + 1, 0, dom2, parent2);
|
|
19969
19973
|
});
|
|
19970
19974
|
}
|
|
19971
19975
|
function insertAfter(target) {
|
|
@@ -20001,8 +20005,8 @@ function before(...elems) {
|
|
|
20001
20005
|
if (index2 < 0)
|
|
20002
20006
|
return;
|
|
20003
20007
|
const domSrc = typeof elems[0] === "function" ? elems[0].call(el, i, this._render(el.children)) : elems;
|
|
20004
|
-
const
|
|
20005
|
-
uniqueSplice(siblings2, index2, 0,
|
|
20008
|
+
const dom2 = this._makeDomArray(domSrc, i < lastIdx);
|
|
20009
|
+
uniqueSplice(siblings2, index2, 0, dom2, parent2);
|
|
20006
20010
|
});
|
|
20007
20011
|
}
|
|
20008
20012
|
function insertBefore(target) {
|
|
@@ -20040,11 +20044,11 @@ function replaceWith(content) {
|
|
|
20040
20044
|
}
|
|
20041
20045
|
const siblings2 = parent2.children;
|
|
20042
20046
|
const cont = typeof content === "function" ? content.call(el, i, el) : content;
|
|
20043
|
-
const
|
|
20044
|
-
update(
|
|
20047
|
+
const dom2 = this._makeDomArray(cont);
|
|
20048
|
+
update(dom2, null);
|
|
20045
20049
|
const index2 = siblings2.indexOf(el);
|
|
20046
|
-
uniqueSplice(siblings2, index2, 1,
|
|
20047
|
-
if (!
|
|
20050
|
+
uniqueSplice(siblings2, index2, 1, dom2, parent2);
|
|
20051
|
+
if (!dom2.includes(el)) {
|
|
20048
20052
|
el.parent = el.prev = el.next = null;
|
|
20049
20053
|
}
|
|
20050
20054
|
});
|
|
@@ -20280,8 +20284,8 @@ function getLoad(parse2, render2) {
|
|
|
20280
20284
|
_parse(content2, options2, isDocument3, context) {
|
|
20281
20285
|
return parse2(content2, options2, isDocument3, context);
|
|
20282
20286
|
}
|
|
20283
|
-
_render(
|
|
20284
|
-
return render2(
|
|
20287
|
+
_render(dom2) {
|
|
20288
|
+
return render2(dom2, this.options);
|
|
20285
20289
|
}
|
|
20286
20290
|
}
|
|
20287
20291
|
function initialize(selector, context, root2 = initialRoot, opts) {
|
|
@@ -28468,8 +28472,8 @@ function parseWithParse5(content, options, isDocument2, context) {
|
|
|
28468
28472
|
return isDocument2 ? parse$2(content, opts) : parseFragment(context, content, opts);
|
|
28469
28473
|
}
|
|
28470
28474
|
const renderOpts = { treeAdapter: adapter };
|
|
28471
|
-
function renderWithParse5(
|
|
28472
|
-
const nodes = "length" in
|
|
28475
|
+
function renderWithParse5(dom2) {
|
|
28476
|
+
const nodes = "length" in dom2 ? dom2 : [dom2];
|
|
28473
28477
|
for (let index2 = 0; index2 < nodes.length; index2 += 1) {
|
|
28474
28478
|
const node = nodes[index2];
|
|
28475
28479
|
if (isDocument$1(node)) {
|
|
@@ -29721,7 +29725,7 @@ function parseDocument$1(data2, options) {
|
|
|
29721
29725
|
return handler.root;
|
|
29722
29726
|
}
|
|
29723
29727
|
const parse$1 = getParse((content, options, isDocument2, context) => options.xmlMode || options._useHtmlParser2 ? parseDocument$1(content, options) : parseWithParse5(content, options, isDocument2, context));
|
|
29724
|
-
const load = getLoad(parse$1, (
|
|
29728
|
+
const load = getLoad(parse$1, (dom2, options) => options.xmlMode || options._useHtmlParser2 ? render$1(dom2, options) : renderWithParse5(dom2));
|
|
29725
29729
|
load([]);
|
|
29726
29730
|
const defaultSelectorRules = {
|
|
29727
29731
|
"div,p": ({ $node }) => ({
|
|
@@ -49597,8 +49601,7 @@ const remark_anyblock_render_codeblock = () => {
|
|
|
49597
49601
|
if (typeof document == "undefined") return;
|
|
49598
49602
|
return (tree, _file) => {
|
|
49599
49603
|
visit$1(tree, "code", (node, index2, parent2) => {
|
|
49600
|
-
|
|
49601
|
-
if (node.lang != "anyblock") return;
|
|
49604
|
+
if (typeof node.lang != "string" || node.lang.toLowerCase() != "anyblock") return;
|
|
49602
49605
|
if (!parent2 || !index2) return;
|
|
49603
49606
|
const lines = node.value.split("\n");
|
|
49604
49607
|
const head = lines.shift();
|
|
@@ -49610,8 +49613,6 @@ const remark_anyblock_render_codeblock = () => {
|
|
|
49610
49613
|
const el = document.createElement("div");
|
|
49611
49614
|
el.classList.add("ab-note", "drop-shadow");
|
|
49612
49615
|
ABConvertManager.autoABConvert(el, header, content, markup.startsWith(":::") ? "mdit" : "");
|
|
49613
|
-
console.log("\nanyblock codeblock transformer visit2:", header, "c==", content);
|
|
49614
|
-
console.log("\nanyblock codeblock transformer visit3:", el.outerHTML, el);
|
|
49615
49616
|
const new_node = {
|
|
49616
49617
|
type: "html",
|
|
49617
49618
|
value: el.outerHTML
|
|
@@ -49630,12 +49631,12 @@ const remark_anyblock_render_codeblock = () => {
|
|
|
49630
49631
|
el_child.innerHTML = result;
|
|
49631
49632
|
});
|
|
49632
49633
|
}
|
|
49633
|
-
const
|
|
49634
|
+
const quartz_transformer_anyblock = () => {
|
|
49634
49635
|
return {
|
|
49635
49636
|
name: "AnyBlock",
|
|
49636
49637
|
markdownPlugins(_ctx) {
|
|
49637
49638
|
return [
|
|
49638
|
-
// remark_anyblock_to_codeblock,
|
|
49639
|
+
// remark_anyblock_to_codeblock, // 取消注释则用库的地方会报错找不到 document
|
|
49639
49640
|
remark_anyblock_render_codeblock
|
|
49640
49641
|
// last
|
|
49641
49642
|
];
|
|
@@ -49649,6 +49650,7 @@ export {
|
|
|
49649
49650
|
ABConvertManager,
|
|
49650
49651
|
abConvertEvent,
|
|
49651
49652
|
jsdom_init,
|
|
49652
|
-
|
|
49653
|
+
remark_anyblock_render_codeblock,
|
|
49654
|
+
quartz_transformer_anyblock as transformer_anyblock
|
|
49653
49655
|
};
|
|
49654
49656
|
//# sourceMappingURL=remark-any-block.js.map
|
package/index.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
// JsDom。仅用于提供document对象支持 (如果Ob环境中则不需要,用ob自带document对象的)
|
|
2
2
|
export { jsdom_init } from './jsdom_init'
|
|
3
3
|
export {
|
|
4
|
-
transformer_anyblock,
|
|
5
|
-
//
|
|
6
|
-
|
|
4
|
+
quartz_transformer_anyblock as transformer_anyblock,
|
|
5
|
+
// remark_anyblock_to_codeblock, // 取消注释则用库的地方会报错找不到 document
|
|
6
|
+
remark_anyblock_render_codeblock,
|
|
7
7
|
} from './anyblock'
|
|
8
8
|
export { abConvertEvent } from '../ABConverter/ABConvertEvent'
|
|
9
9
|
export { ABConvertManager } from '../ABConverter/ABConvertManager' // for client
|
package/jsdom_init.ts
CHANGED
|
@@ -6,12 +6,19 @@
|
|
|
6
6
|
|
|
7
7
|
// import jsdom from "jsdom"
|
|
8
8
|
|
|
9
|
+
let dom: any = null;
|
|
10
|
+
|
|
9
11
|
export async function jsdom_init() {
|
|
10
12
|
const { default: jsdom } = await import('jsdom') // 废弃,要同步,避免docuemnt初始化不及时
|
|
11
13
|
const { JSDOM } = jsdom
|
|
12
|
-
|
|
14
|
+
dom = new JSDOM(`<!DOCTYPE html><html><body></body></html>`, {
|
|
13
15
|
url: 'http://localhost/', // @warn 若缺少该行,则在mdit+build环境下,编译报错
|
|
14
|
-
})
|
|
16
|
+
})
|
|
17
|
+
jsdom_able()
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/// 启用 jsdom 环境
|
|
21
|
+
export async function jsdom_able() {
|
|
15
22
|
global.Storage = dom.window.Storage;
|
|
16
23
|
global.window = dom.window as any
|
|
17
24
|
global.history = dom.window.history // @warn 若缺少该行,则在mdit+build环境下,编译报错:ReferenceError: history is not defined
|
|
@@ -28,3 +35,10 @@ export async function jsdom_init() {
|
|
|
28
35
|
dom.window.scrollTo = ()=>{} // @warn 若缺少该行,编译警告:Error: Not implemented: window.scrollTo
|
|
29
36
|
global.MutationObserver = dom.window.MutationObserver
|
|
30
37
|
}
|
|
38
|
+
|
|
39
|
+
/// 禁用 jsdom 环境
|
|
40
|
+
export async function jsdom_disable() {
|
|
41
|
+
global.window = undefined
|
|
42
|
+
global.history = undefined
|
|
43
|
+
global.document = undefined
|
|
44
|
+
}
|
package/package.json
CHANGED
|
@@ -1,44 +1,45 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@anyblock/remark-any-block",
|
|
3
|
-
"version": "1.0.
|
|
4
|
-
"description": "You can flexibility to create a 'Block' by many means. It also provides many useful features, like `list to table`.",
|
|
5
|
-
"types": "@types/index_remark.d.ts",
|
|
6
|
-
"type": "module",
|
|
7
|
-
"main": "dist/remark-any-block.cjs",
|
|
8
|
-
"exports": {
|
|
9
|
-
".": {
|
|
10
|
-
"import": "./dist/remark-any-block.js",
|
|
11
|
-
"require": "./dist/remark-any-block.cjs"
|
|
12
|
-
}
|
|
13
|
-
},
|
|
14
|
-
"publishConfig": {
|
|
15
|
-
"access": "public"
|
|
16
|
-
},
|
|
17
|
-
"scripts": {
|
|
18
|
-
"test": "echo \"Error: no test specified\" && exit 1",
|
|
19
|
-
"build": "pnpm build_vite",
|
|
20
|
-
"build_vite": "pnpm copy_abc_style && vite build",
|
|
21
|
-
"prepublishOnly": "pnpm build",
|
|
22
|
-
"copy_abc_style": "copyfiles --flat ../../src/ABConverter/style/styles.css ./"
|
|
23
|
-
},
|
|
24
|
-
"keywords": [],
|
|
25
|
-
"author": "",
|
|
26
|
-
"license": "GNU Affero General Public License v3.0",
|
|
27
|
-
"packageManager": "pnpm@10.10.0",
|
|
28
|
-
"dependencies": {
|
|
29
|
-
"
|
|
30
|
-
"
|
|
31
|
-
"mdast
|
|
32
|
-
"
|
|
33
|
-
"
|
|
34
|
-
"
|
|
35
|
-
"
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
"@types/
|
|
40
|
-
"
|
|
41
|
-
"
|
|
42
|
-
"
|
|
43
|
-
|
|
44
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "@anyblock/remark-any-block",
|
|
3
|
+
"version": "1.0.1-beta2",
|
|
4
|
+
"description": "You can flexibility to create a 'Block' by many means. It also provides many useful features, like `list to table`.",
|
|
5
|
+
"types": "@types/index_remark.d.ts",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"main": "dist/remark-any-block.cjs",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"import": "./dist/remark-any-block.js",
|
|
11
|
+
"require": "./dist/remark-any-block.cjs"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"publishConfig": {
|
|
15
|
+
"access": "public"
|
|
16
|
+
},
|
|
17
|
+
"scripts": {
|
|
18
|
+
"test": "echo \"Error: no test specified\" && exit 1",
|
|
19
|
+
"build": "pnpm build_vite",
|
|
20
|
+
"build_vite": "pnpm copy_abc_style && vite build",
|
|
21
|
+
"prepublishOnly": "pnpm build",
|
|
22
|
+
"copy_abc_style": "copyfiles --flat ../../src/ABConverter/style/styles.css ./"
|
|
23
|
+
},
|
|
24
|
+
"keywords": [],
|
|
25
|
+
"author": "",
|
|
26
|
+
"license": "GNU Affero General Public License v3.0",
|
|
27
|
+
"packageManager": "pnpm@10.10.0",
|
|
28
|
+
"dependencies": {
|
|
29
|
+
"jsdom": "^26.1.0",
|
|
30
|
+
"markdown-it": "^14.1.0",
|
|
31
|
+
"mdast": "^3.0.0",
|
|
32
|
+
"mdast-util-to-markdown": "^2.1.2",
|
|
33
|
+
"tslib": "^2.8.1",
|
|
34
|
+
"unified": "^11.0.5",
|
|
35
|
+
"unist-util-visit": "^5.0.0",
|
|
36
|
+
"vfile": "^6.0.3"
|
|
37
|
+
},
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"@types/jsdom": "^21.1.7",
|
|
40
|
+
"@types/markdown-it": "^14.1.2",
|
|
41
|
+
"copyfiles": "^2.4.1",
|
|
42
|
+
"typescript": "^5.9.3",
|
|
43
|
+
"vite": "^7.2.7"
|
|
44
|
+
}
|
|
45
|
+
}
|