@anyblock/any-block-core 3.4.6 → 3.4.10

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/ABConvertEvent.ts CHANGED
@@ -7,6 +7,8 @@
7
7
  * 这些操作统一注册在此处
8
8
  */
9
9
 
10
+ import { ABCSetting } from "./ABSetting"
11
+
10
12
  /**
11
13
  * 一些AB块的后触发事件 - css加载完触发
12
14
  *
@@ -267,41 +269,8 @@ export function abConvertEvent(d: Element|Document, isCycle: boolean = false) {
267
269
  const scale_new = el_g.getBBox().height/el_div.offsetWidth;
268
270
  el_svg.setAttribute("style", `height:${el_g.getBBox().height*scale_new+40}px`); // 重调容器大小
269
271
  // el_g.setAttribute("transform", `translate(20.0,80.0) scale(${scale_new})`) // 重调位置和缩放
270
- markmap_event(d) // 好像调位置有问题,只能重渲染了……
272
+ ABCSetting.obsidian.markmap_event?.(d) // 好像调位置有问题,只能重渲染了……
271
273
  }
272
274
  }
273
275
  }
274
276
  }
275
-
276
- /**
277
- * 一些AB块的后触发事件 - dom加载完触发 - markmap
278
- */
279
- export function markmap_event(d: Element|Document) {
280
- // xxx2markmap,渲染事件
281
- if (d.querySelector('.ab-markmap-svg')) {
282
- console.log(" - markmap_event")
283
- let script_el: HTMLScriptElement|null = document.querySelector('script[script-id="ab-markmap-script"]');
284
- if (script_el) script_el.remove();
285
- const divEl = d as Element;
286
- let markmapId = '';
287
- if (divEl.tagName === 'DIV') {
288
- markmapId = divEl.querySelector('.ab-markmap-svg')?.id || '';
289
- }
290
- script_el = document.createElement('script'); document.head.appendChild(script_el);
291
- script_el.type = "module";
292
- script_el.setAttribute("script-id", "ab-markmap-script");
293
- script_el.textContent = `
294
- import { Markmap, } from 'https://jspm.dev/markmap-view';
295
- const markmapId = "${markmapId || ''}";
296
- let mindmaps;
297
- if (markmapId) {
298
- mindmaps = document.querySelectorAll('#' + markmapId);
299
- } else {
300
- mindmaps = document.querySelectorAll('.ab-markmap-svg'); // 注意一下这里的选择器
301
- }
302
- for(const mindmap of mindmaps) {
303
- mindmap.innerHTML = "";
304
- Markmap.create(mindmap,null,JSON.parse(mindmap.getAttribute('data-json')));
305
- }`;
306
- }
307
- }
@@ -25,7 +25,18 @@ import {
25
25
  } from './converter/ABConvert'
26
26
  import { autoABAlias } from "./ABAlias"
27
27
  import { ABCSetting } from "./ABSetting"
28
-
28
+
29
+ /** 上个处理器的结果
30
+ * 多个处理器串连时,每个处理器给下一个处理器的东西。
31
+ * 标注了上次的处理结果,以让下个处理器判断是否能继续处理,还是跳过。
32
+ */
33
+ type PREV = {
34
+ prev_result: ABConvert_IOType; // 上次转换后的结果,初始必为string
35
+ prev_type: string; // 上次转换后的结果的类型 (类型检测而来)
36
+ prev_type2: ABConvert_IOEnum; // 上次转换后的结果的类型 (接口声明而来)
37
+ prev_processor: string | undefined | any; // 上一次执行转换的处理器
38
+ }
39
+
29
40
  /**
30
41
  * AB转换器的管理器。注意:使用前必须先执行:`redefine_renderMarkdown`
31
42
  *
@@ -82,15 +93,15 @@ export class ABConvertManager {
82
93
  * @detail 这里需要能够被回调函数替换。从而用于接回软件自身的html渲染机制,来进行解耦
83
94
  * @param markdown 原md
84
95
  * @param el 要追加到的元素
85
- * @param ctx Obsidian在这里需要传入 MarkdownRenderChild 类型,但为了跨平台我这里修改成可选的any类型
96
+ * @param ctx Obsidian在这里需要传入 MarkdownRenderChild 类型,但为了跨平台我这里修改成可选的unknown类型
86
97
  */
87
- public m_renderMarkdownFn:(markdown: string, el: HTMLElement, ctx?: any) => void = (markdown, el) => {
98
+ public m_renderMarkdownFn:(markdown: string, el: HTMLElement, ctx?: unknown) => void = (markdown, el) => {
88
99
  el.classList.add("markdown-rendered") // 并注意,应当在使用该函数前将el添加该css类,或者重定义时增加该条语句
89
100
  console.error("Please use renderMarkdownFn redefine render function")
90
101
  }
91
102
 
92
103
  /// 用回调函数替换重渲染器
93
- public redefine_renderMarkdown(callback: (markdown: string, el: HTMLElement, ctx?: any) => void) {
104
+ public redefine_renderMarkdown(callback: (markdown: string, el: HTMLElement, ctx?: unknown) => void) {
94
105
  this.m_renderMarkdownFn = callback
95
106
  }
96
107
 
@@ -110,25 +121,24 @@ export class ABConvertManager {
110
121
  * @param header 转换方式
111
122
  * @param content 要转换的初始文本 (无前缀版本,前缀在选择器环节已经删除了)
112
123
  * @param selectorName 选择器名,空表示未知
113
- * @return 等于el,无用,后面可以删了
114
124
  */
115
- public static autoABConvert(el:HTMLDivElement, header:string, content:string, selectorName:string = "", ctx?: any): void{
116
- let prev_result: ABConvert_IOType = content // 上次转换后的结果,初始必为string
117
- let prev_type: string = "string" // 上次转换后的结果的类型 (类型检测而来)
118
- let prev_type2: ABConvert_IOEnum = ABConvert_IOEnum.text // 上次转换后的结果的类型 (接口声明而来)
119
- let prev_processor; // 上一次转换的处理器
120
- let prev = { // 组合在一起是为了引用传参
121
- prev_result, prev_type, prev_type2, prev_processor
125
+ public static autoABConvert(el:HTMLDivElement, header:string, content:string, selectorName:string = "", ctx?: unknown): void{
126
+ // 初始情况,上一个处理器不存在
127
+ let prev: PREV = {
128
+ prev_result: content,
129
+ prev_type: "string",
130
+ prev_type2: ABConvert_IOEnum.text,
131
+ prev_processor: undefined,
122
132
  }
123
133
 
124
- if (false && ABCSetting.is_debug) ABConvertManager.startTime = performance.now();
134
+ if (false && ABCSetting.is_debug) ABConvertManager.startTime = performance.now(); // 性能检查1
125
135
  {
126
- header = autoABAlias(header, selectorName, prev_result as string);
136
+ header = autoABAlias(header, selectorName, prev.prev_result as string);
127
137
  let list_header = header.split("|")
128
- prev_result = this.autoABConvert_runConvert(el, list_header, prev, ctx)
138
+ this.autoABConvert_runConvert(el, list_header, prev, ctx)
129
139
  this.autoABConvert_last(el, header, selectorName, prev, ctx)
130
140
  }
131
- if (false && ABCSetting.is_debug) {
141
+ if (false && ABCSetting.is_debug) { // 性能检查2
132
142
  const endTime = performance.now();
133
143
  console.log(`Takes ${(endTime - ABConvertManager.startTime).toFixed(2)} ms when selector "${selectorName}" header "${header}"`);
134
144
  }
@@ -143,7 +153,11 @@ export class ABConvertManager {
143
153
  * @param prev_type2 上次转换后的结果的类型 (接口声明而来, IOEnum类型)
144
154
  * @returns 递归转换后的结果
145
155
  */
146
- private static autoABConvert_runConvert(el:HTMLDivElement, list_header:string[], prev:any, ctx?: any):any{
156
+ private static autoABConvert_runConvert(
157
+ el:HTMLDivElement, list_header:string[],
158
+ prev: PREV,
159
+ ctx?: unknown
160
+ ): PREV | void {
147
161
  // 循环header组,直到遍历完文本处理器或遇到渲染处理器
148
162
  for (let item_header of list_header){ // TODO 因为可能被插入新的“中间自动转换器”,要么for替换成递归,要么都在头部预处理时弄完
149
163
  for (let abReplaceProcessor of ABConvertManager.getInstance().list_abConvert){
@@ -179,7 +193,7 @@ export class ABConvertManager {
179
193
  prev.prev_type2==ABConvert_IOEnum.text
180
194
  ){ // 需要输入html,实际输入md,则插入一个md->html
181
195
  const subEl: HTMLDivElement = document.createElement("div"); el.appendChild(subEl);
182
- ABConvertManager.getInstance().m_renderMarkdownFn(prev.prev_result, subEl);
196
+ ABConvertManager.getInstance().m_renderMarkdownFn(prev.prev_result as string, subEl);
183
197
  prev.prev_result = el
184
198
  prev.prev_type = typeof(prev.prev_result)
185
199
  prev.prev_type2 = ABConvert_IOEnum.el
@@ -206,7 +220,14 @@ export class ABConvertManager {
206
220
  }
207
221
 
208
222
  // (2) 执行处理器
209
- prev.prev_result = abReplaceProcessor.process(el, item_header, prev.prev_result, ctx)
223
+ // 避免用户扩展或PR的一些新处理器不正常
224
+ // TODO cm 相关的依赖升级后,list2card 中包含 `<br>` 时,id `id:c2listdata2items-e` 存在错误
225
+ try {
226
+ prev.prev_result = abReplaceProcessor.process(el, item_header, prev.prev_result, ctx)
227
+ } catch (e) {
228
+ console.error(`处理器执行错误, id:${abReplaceProcessor.id}, header:${item_header}, error:`, e)
229
+ break
230
+ }
210
231
  prev.prev_type = typeof(prev.prev_result)
211
232
  prev.prev_type2 = abReplaceProcessor.process_return as ABConvert_IOEnum
212
233
  prev.prev_processor = abReplaceProcessor.process
@@ -232,19 +253,19 @@ export class ABConvertManager {
232
253
  /**
233
254
  * 子函数,后处理/尾处理,主要进行末尾追加指令
234
255
  */
235
- private static autoABConvert_last (el:HTMLDivElement, header:string, selectorName:string, prev:any, ctx?: any):any{
256
+ private static autoABConvert_last (el:HTMLDivElement, _header:string, _selectorName:string, prev:PREV, _ctx?: unknown):any{
236
257
  // text内容,则给一个md渲染器
237
258
  if (prev.prev_type == "string" && prev.prev_type2 == ABConvert_IOEnum.text) {
238
259
  const subEl = document.createElement("div"); el.appendChild(subEl);
239
260
  ABConvertManager.getInstance().m_renderMarkdownFn(prev.prev_result as string, subEl);
240
- prev.prev_result = el; prev.prev_type = "object"; prev.prev_type2 = ABConvert_IOEnum.el; prev.process = "md";
261
+ prev.prev_result = el; prev.prev_type = "object"; prev.prev_type2 = ABConvert_IOEnum.el; prev.prev_processor = "md";
241
262
  }
242
263
  // json内容/数组内容,则用代码块表示
243
264
  else if (prev.prev_type == "string" && prev.prev_type2 == ABConvert_IOEnum.json) {
244
265
  const code_str:string = "```json\n" + prev.prev_result + "\n```\n"
245
266
  const subEl = document.createElement("div"); el.appendChild(subEl);
246
267
  ABConvertManager.getInstance().m_renderMarkdownFn(code_str, subEl);
247
- prev.prev_result = el; prev.prev_type = "object"; prev.prev_type2 = ABConvert_IOEnum.el; prev.process = "show_json";
268
+ prev.prev_result = el; prev.prev_type = "object"; prev.prev_type2 = ABConvert_IOEnum.el; prev.prev_processor = "show_json";
248
269
  }
249
270
  // 数组流,用代码块表示
250
271
  else if (prev.prev_type == "object" &&
@@ -253,7 +274,7 @@ export class ABConvertManager {
253
274
  const code_str:string = "```json\n" + JSON.stringify(prev.prev_result, null, 2) + "\n```\n"
254
275
  const subEl = document.createElement("div"); el.appendChild(subEl);
255
276
  ABConvertManager.getInstance().m_renderMarkdownFn(code_str, subEl);
256
- prev.prev_result = el; prev.prev_type = "object"; prev.prev_type2 = ABConvert_IOEnum.el; prev.process = "show_listStream";
277
+ prev.prev_result = el; prev.prev_type = "object"; prev.prev_type2 = ABConvert_IOEnum.el; prev.prev_processor = "show_listStream";
257
278
  }
258
279
  else if (prev.prev_type == "object" && prev.prev_type2 == ABConvert_IOEnum.el) {
259
280
  return prev
package/ABSetting.ts CHANGED
@@ -16,9 +16,10 @@ export const ABCSetting: {
16
16
  env: "obsidian"|"obsidian-min"|"obsidian-pro"|"app"|"markdown-it"|"remark",
17
17
  // 某些环境的独占 api,其他环境用不上
18
18
  obsidian: {
19
- global_app: any,
20
- global_ctx: any, // MarkdownPostProcessorContext类型, obsidian专用
19
+ global_app: unknown|null,
20
+ global_ctx: unknown|null, // MarkdownPostProcessorContext类型, obsidian专用
21
21
  mermaid?: Promise<any>, // obsidian专用,obsidian 如何渲染mermaid
22
+ markmap_event?: (d: Element|Document) => Promise<void>, // 如何渲染 markmap
22
23
  },
23
24
  pro: {
24
25
  disable: boolean, // 禁用 pro 版的扩展功能,变为非 pro 版
@@ -45,6 +46,7 @@ export const ABCSetting: {
45
46
  global_app: null,
46
47
  global_ctx: null,
47
48
  mermaid: undefined,
49
+ markmap_event: undefined,
48
50
  },
49
51
  pro: {
50
52
  disable: false,
@@ -63,7 +65,7 @@ export const ABCSetting: {
63
65
  *
64
66
  * @attention 注意:修改正则要注意小括号的位置是否对应,不然还要去修改索引
65
67
  */
66
- export const ABReg = {
68
+ export const ABReg: Record<string, RegExp> = {
67
69
  /**
68
70
  * AB块头部
69
71
  *
package/README.md CHANGED
@@ -49,7 +49,7 @@ ABConvertManager.getInstance().redefine_renderMarkdown((markdown: string, el: HT
49
49
 
50
50
  const mdrc: MarkdownRenderChild = new MarkdownRenderChild(el);
51
51
  if (ctx) ctx.addChild(mdrc);
52
- else if (ABCSetting.global_ctx) ABCSetting.global_ctx.addChild(mdrc);
52
+ else if (ABCSetting.global_ctx) { (ABCSetting.global_ctx as MarkdownPostProcessorContext).addChild(mdrc) }
53
53
  /**
54
54
  * Renders markdown string to an HTML element.
55
55
  * @param app - A reference to the app object
@@ -18,10 +18,9 @@
18
18
  * - 在VuePress-Mdit环境,没有真正的document元素,而打开文件的钩子在mdit里面又没有,可能需要要vuepress插件解决
19
19
  */
20
20
 
21
- import DOMPurify from "dompurify"
21
+ import DOMPurify from "dompurify"
22
22
  import { ABConvert_IOEnum, ABConvert } from "./ABConvert"
23
23
  import { ABCSetting } from "../ABSetting"
24
- import { markmap_event } from "../ABConvertEvent";
25
24
 
26
25
  /**
27
26
  * 生成一个随机id
@@ -46,7 +45,7 @@ process_param: ABConvert_IOEnum.text,
46
45
  process_return: ABConvert_IOEnum.el,
47
46
  process: (el, header, content: string): HTMLElement=>{
48
47
  list2markmap(content, el)
49
- markmap_event(el)
48
+ ABCSetting.obsidian.markmap_event?.(el)
50
49
  // setTimeout(()=>{abConvertEvent(el)}, 500);
51
50
  return el
52
51
  }
@@ -117,3 +116,42 @@ function list2markmap(markdown: string, div: HTMLDivElement) {
117
116
 
118
117
  return div
119
118
  }
119
+
120
+ /**
121
+ * 一些AB块的后触发事件 - dom加载完触发 - markmap
122
+ *
123
+ * 注意这里的 script 标签执行动态类型会被 obsidian 审查认为是不安全的。
124
+ * 所以没必要打包使用时,不加载该函数
125
+ */
126
+ async function markmap_event(d: Element|Document) {
127
+ if (ABCSetting.env == "obsidian-min") return // min 不支持 markmap
128
+
129
+ // xxx2markmap,渲染事件
130
+ if (d.querySelector('.ab-markmap-svg')) {
131
+ console.log(" - markmap_event")
132
+ let script_el: HTMLScriptElement|null = document.querySelector('script[script-id="ab-markmap-script"]');
133
+ if (script_el) script_el.remove();
134
+ const divEl = d as Element;
135
+ let markmapId = '';
136
+ if (divEl.tagName === 'DIV') {
137
+ markmapId = divEl.querySelector('.ab-markmap-svg')?.id || '';
138
+ }
139
+ script_el = document.createElement('script'); document.head.appendChild(script_el);
140
+ script_el.type = "module";
141
+ script_el.setAttribute("script-id", "ab-markmap-script");
142
+ script_el.textContent = `
143
+ import { Markmap, } from 'https://jspm.dev/markmap-view';
144
+ const markmapId = "${markmapId || ''}";
145
+ let mindmaps;
146
+ if (markmapId) {
147
+ mindmaps = document.querySelectorAll('#' + markmapId);
148
+ } else {
149
+ mindmaps = document.querySelectorAll('.ab-markmap-svg'); // 注意一下这里的选择器
150
+ }
151
+ for(const mindmap of mindmaps) {
152
+ mindmap.innerHTML = "";
153
+ Markmap.create(mindmap,null,JSON.parse(mindmap.getAttribute('data-json')));
154
+ }`;
155
+ }
156
+ }
157
+ ABCSetting.obsidian.markmap_event = markmap_event
@@ -21,7 +21,7 @@ function getID(length=16){
21
21
  }
22
22
 
23
23
  // 纯组合,后续用别名模块替代
24
- const abc_title2mindmap = ABConvert.factory({
24
+ const _abc_title2mindmap = ABConvert.factory({
25
25
  id: "title2mindmap",
26
26
  name: "标题到脑图",
27
27
  process_param: ABConvert_IOEnum.text,
@@ -34,7 +34,7 @@ const abc_title2mindmap = ABConvert.factory({
34
34
  })
35
35
 
36
36
  // 纯组合,后续用别名模块替代
37
- const abc_list2mindmap = ABConvert.factory({
37
+ const _abc_list2mindmap = ABConvert.factory({
38
38
  id: "list2mindmap",
39
39
  name: "列表转mermaid思维导图",
40
40
  process_param: ABConvert_IOEnum.text,
@@ -46,7 +46,7 @@ const abc_list2mindmap = ABConvert.factory({
46
46
  }
47
47
  })
48
48
 
49
- const abc_list2mermaid = ABConvert.factory({
49
+ const _abc_list2mermaid = ABConvert.factory({
50
50
  id: "list2mermaid",
51
51
  name: "列表转mermaid流程图",
52
52
  match: /^list2mermaid(\((.*)\))?$/,
@@ -65,7 +65,7 @@ const abc_list2mermaid = ABConvert.factory({
65
65
  }
66
66
  })
67
67
 
68
- const abc_list2mermaidText = ABConvert.factory({
68
+ const _abc_list2mermaidText = ABConvert.factory({
69
69
  id: "list2mermaidText",
70
70
  name: "列表转mermaid文本",
71
71
  match: /^list2mermaidText(\((.*)\))?$/,
@@ -84,7 +84,7 @@ const abc_list2mermaidText = ABConvert.factory({
84
84
  }
85
85
  })
86
86
 
87
- const abc_list2mehrmaid = ABConvert.factory({
87
+ const _abc_list2mehrmaid = ABConvert.factory({
88
88
  id: "list2mehrmaidText",
89
89
  name: "列表转mehrmaid文本",
90
90
  match: /^list2mehrmaidText(\((.*)\))?$/,
@@ -103,7 +103,7 @@ const abc_list2mehrmaid = ABConvert.factory({
103
103
  }
104
104
  })
105
105
 
106
- const abc_mermaid = ABConvert.factory({
106
+ const _abc_mermaid = ABConvert.factory({
107
107
  id: "mermaid-with",
108
108
  name: "新mermaid",
109
109
  match: /^mermaid(\((.*)\))?$/,
package/package.json CHANGED
@@ -1,22 +1,22 @@
1
- {
2
- "name": "@anyblock/any-block-core",
3
- "version": "3.4.6",
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
- "main": "index.js",
6
- "scripts": {
7
- "test": "echo \"Error: no test specified\" && exit 1"
8
- },
9
- "author": "LincZero",
10
- "license": "GNU Affero General Public License v3.0",
11
- "dependencies": {
12
- "dompurify": "^3.4.3",
13
- "luxon": "^3.7.2",
14
- "markmap-lib": "^0.17.2",
15
- "plantuml-encoder": "^1.4.0"
16
- },
17
- "devDependencies": {
18
- "@types/luxon": "^3.7.1",
19
- "@types/plantuml-encoder": "^1.4.2",
20
- "tslib": "^2.8.1"
21
- }
22
- }
1
+ {
2
+ "name": "@anyblock/any-block-core",
3
+ "version": "3.4.10",
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
+ "main": "index.js",
6
+ "scripts": {
7
+ "test": "echo \"Error: no test specified\" && exit 1"
8
+ },
9
+ "author": "LincZero",
10
+ "license": "GNU Affero General Public License v3.0",
11
+ "dependencies": {
12
+ "dompurify": "^3.4.3",
13
+ "luxon": "^3.7.2",
14
+ "markmap-lib": "^0.17.2",
15
+ "plantuml-encoder": "^1.4.0"
16
+ },
17
+ "devDependencies": {
18
+ "@types/luxon": "^3.7.1",
19
+ "@types/plantuml-encoder": "^1.4.2",
20
+ "tslib": "^2.8.1"
21
+ }
22
+ }