@anyblock/remark-any-block 1.0.0-beta9 → 1.0.1
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/anyblock.ts +68 -77
- package/dist/remark-any-block.cjs +3 -5
- package/dist/remark-any-block.js +3 -5
- package/index.ts +2 -2
- package/package.json +1 -1
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 (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,
|
|
@@ -245,7 +236,7 @@ export const transformer_anyblock: QuartzTransformerPlugin = (/*options: any*/)
|
|
|
245
236
|
name: "AnyBlock",
|
|
246
237
|
markdownPlugins(_ctx: BuildCtx) {
|
|
247
238
|
return [
|
|
248
|
-
// remark_anyblock_to_codeblock,
|
|
239
|
+
// remark_anyblock_to_codeblock, // 取消注释则用库的地方会报错找不到 document
|
|
249
240
|
remark_anyblock_render_codeblock, // last
|
|
250
241
|
]
|
|
251
242
|
},
|
|
@@ -49621,8 +49621,7 @@ const remark_anyblock_render_codeblock = () => {
|
|
|
49621
49621
|
if (typeof document == "undefined") return;
|
|
49622
49622
|
return (tree, _file) => {
|
|
49623
49623
|
visit$1(tree, "code", (node, index2, parent2) => {
|
|
49624
|
-
|
|
49625
|
-
if (node.lang != "anyblock") return;
|
|
49624
|
+
if (node.lang.toLowerCase() != "anyblock") return;
|
|
49626
49625
|
if (!parent2 || !index2) return;
|
|
49627
49626
|
const lines = node.value.split("\n");
|
|
49628
49627
|
const head = lines.shift();
|
|
@@ -49634,8 +49633,6 @@ const remark_anyblock_render_codeblock = () => {
|
|
|
49634
49633
|
const el = document.createElement("div");
|
|
49635
49634
|
el.classList.add("ab-note", "drop-shadow");
|
|
49636
49635
|
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
49636
|
const new_node = {
|
|
49640
49637
|
type: "html",
|
|
49641
49638
|
value: el.outerHTML
|
|
@@ -49659,7 +49656,7 @@ const transformer_anyblock = () => {
|
|
|
49659
49656
|
name: "AnyBlock",
|
|
49660
49657
|
markdownPlugins(_ctx) {
|
|
49661
49658
|
return [
|
|
49662
|
-
// remark_anyblock_to_codeblock,
|
|
49659
|
+
// remark_anyblock_to_codeblock, // 取消注释则用库的地方会报错找不到 document
|
|
49663
49660
|
remark_anyblock_render_codeblock
|
|
49664
49661
|
// last
|
|
49665
49662
|
];
|
|
@@ -49672,5 +49669,6 @@ const transformer_anyblock = () => {
|
|
|
49672
49669
|
exports.ABConvertManager = ABConvertManager;
|
|
49673
49670
|
exports.abConvertEvent = abConvertEvent;
|
|
49674
49671
|
exports.jsdom_init = jsdom_init;
|
|
49672
|
+
exports.remark_anyblock_render_codeblock = remark_anyblock_render_codeblock;
|
|
49675
49673
|
exports.transformer_anyblock = transformer_anyblock;
|
|
49676
49674
|
//# sourceMappingURL=remark-any-block.cjs.map
|
package/dist/remark-any-block.js
CHANGED
|
@@ -49597,8 +49597,7 @@ const remark_anyblock_render_codeblock = () => {
|
|
|
49597
49597
|
if (typeof document == "undefined") return;
|
|
49598
49598
|
return (tree, _file) => {
|
|
49599
49599
|
visit$1(tree, "code", (node, index2, parent2) => {
|
|
49600
|
-
|
|
49601
|
-
if (node.lang != "anyblock") return;
|
|
49600
|
+
if (node.lang.toLowerCase() != "anyblock") return;
|
|
49602
49601
|
if (!parent2 || !index2) return;
|
|
49603
49602
|
const lines = node.value.split("\n");
|
|
49604
49603
|
const head = lines.shift();
|
|
@@ -49610,8 +49609,6 @@ const remark_anyblock_render_codeblock = () => {
|
|
|
49610
49609
|
const el = document.createElement("div");
|
|
49611
49610
|
el.classList.add("ab-note", "drop-shadow");
|
|
49612
49611
|
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
49612
|
const new_node = {
|
|
49616
49613
|
type: "html",
|
|
49617
49614
|
value: el.outerHTML
|
|
@@ -49635,7 +49632,7 @@ const transformer_anyblock = () => {
|
|
|
49635
49632
|
name: "AnyBlock",
|
|
49636
49633
|
markdownPlugins(_ctx) {
|
|
49637
49634
|
return [
|
|
49638
|
-
// remark_anyblock_to_codeblock,
|
|
49635
|
+
// remark_anyblock_to_codeblock, // 取消注释则用库的地方会报错找不到 document
|
|
49639
49636
|
remark_anyblock_render_codeblock
|
|
49640
49637
|
// last
|
|
49641
49638
|
];
|
|
@@ -49649,6 +49646,7 @@ export {
|
|
|
49649
49646
|
ABConvertManager,
|
|
49650
49647
|
abConvertEvent,
|
|
49651
49648
|
jsdom_init,
|
|
49649
|
+
remark_anyblock_render_codeblock,
|
|
49652
49650
|
transformer_anyblock
|
|
49653
49651
|
};
|
|
49654
49652
|
//# sourceMappingURL=remark-any-block.js.map
|
package/index.ts
CHANGED
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
export { jsdom_init } from './jsdom_init'
|
|
3
3
|
export {
|
|
4
4
|
transformer_anyblock,
|
|
5
|
-
//
|
|
6
|
-
|
|
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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@anyblock/remark-any-block",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"description": "You can flexibility to create a 'Block' by many means. It also provides many useful features, like `list to table`.",
|
|
5
5
|
"types": "@types/index_remark.d.ts",
|
|
6
6
|
"type": "module",
|