cherry-muse 1.0.4 → 1.0.5

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "cherry-muse",
3
3
  "license": "Apache-2.0",
4
- "version": "1.0.4",
4
+ "version": "1.0.5",
5
5
  "description": "Lightweight Cherry Markdown fork with 83% smaller bundle size (800KB vs 4.8MB)",
6
6
  "repository": {
7
7
  "type": "git",
@@ -35,6 +35,40 @@ export default class Blockquote extends ParagraphBase {
35
35
  // TODO: String.prototype.repeat polyfill
36
36
  }
37
37
 
38
+ getAlertInfo(line) {
39
+ const match = line.match(/^\s*\[!(NOTE|TIP|IMPORTANT|WARNING|CAUTION)\]/i);
40
+ if (match) {
41
+ return {
42
+ type: match[1].toUpperCase(),
43
+ remaining: line.replace(/^\s*\[!(NOTE|TIP|IMPORTANT|WARNING|CAUTION)\]/i, ''),
44
+ };
45
+ }
46
+ return null;
47
+ }
48
+
49
+ getPanelClass(type) {
50
+ const map = {
51
+ NOTE: 'note',
52
+ TIP: 'tip',
53
+ IMPORTANT: 'important',
54
+ WARNING: 'warning',
55
+ CAUTION: 'danger',
56
+ };
57
+ const t = map[type] || 'note';
58
+ return `cherry-panel cherry-panel__${t}`;
59
+ }
60
+
61
+ getPanelTitle(type) {
62
+ const map = {
63
+ NOTE: 'Note',
64
+ TIP: 'Tip',
65
+ IMPORTANT: 'Important',
66
+ WARNING: 'Warning',
67
+ CAUTION: 'Caution',
68
+ };
69
+ return map[type] || type;
70
+ }
71
+
38
72
  handleMatch(str, sentenceMakeFunc) {
39
73
  return str.replace(this.RULE.reg, (match, lines, content) => {
40
74
  const { sign: contentSign, html: parsedHtml } = sentenceMakeFunc(content);
@@ -47,9 +81,13 @@ export default class Blockquote extends ParagraphBase {
47
81
  const contentLines = parsedHtml.split('\n');
48
82
  const replaceReg = /^[>\s]+/;
49
83
  const countReg = />/g;
50
- let lastLevel = 1;
51
- let level = 0;
52
- let handledHtml = `<blockquote data-sign="${sign}_${lineCount}" data-lines="${lineCount}">`;
84
+ let lastLevel = 0;
85
+ let handledHtml = '';
86
+ const tagStack = [];
87
+ const rootAttrs = ` data-sign="${sign}_${lineCount}" data-lines="${lineCount}"`;
88
+ let isRoot = true;
89
+ let skipNextBr = false;
90
+
53
91
  for (let i = 0; contentLines[i]; i++) {
54
92
  if (i !== 0) {
55
93
  const leadIndent = computeLeadingSpaces(contentLines[i]);
@@ -59,31 +97,63 @@ export default class Blockquote extends ParagraphBase {
59
97
  lastIndent = leadIndent;
60
98
  }
61
99
  /* eslint-disable no-loop-func */
62
- const $line = contentLines[i].replace(replaceReg, (leadSymbol) => {
100
+ let currentLevel = 0;
101
+ let $line = contentLines[i].replace(replaceReg, (leadSymbol) => {
63
102
  const leadSymbols = leadSymbol.match(countReg);
64
103
  // 本行引用嵌套层级比上层要多
65
104
  if (leadSymbols && leadSymbols.length > lastLevel) {
66
- level = leadSymbols.length;
105
+ currentLevel = leadSymbols.length;
67
106
  } else {
68
107
  // 否则保持当前缩进层级
69
- level = lastLevel;
108
+ currentLevel = lastLevel;
70
109
  }
71
110
  return '';
72
111
  });
112
+
73
113
  // 同层级,且不为首行时补充一个换行
74
- if (lastLevel === level && i !== 0) {
75
- handledHtml += '<br>';
114
+ if (lastLevel === currentLevel && i !== 0) {
115
+ if (!skipNextBr) {
116
+ handledHtml += '<br>';
117
+ }
118
+ skipNextBr = false;
76
119
  }
120
+
77
121
  // 补充缩进
78
- if (lastLevel < level) {
79
- handledHtml += '<blockquote>'.repeat(level - lastLevel);
80
- lastLevel = level;
122
+ if (lastLevel < currentLevel) {
123
+ for (let l = lastLevel + 1; l <= currentLevel; l++) {
124
+ let tagOpen = `<blockquote${isRoot ? rootAttrs : ''}>`;
125
+ let tagClose = '</blockquote>';
126
+
127
+ // Check for Alert
128
+ if (l === currentLevel) {
129
+ const alertInfo = this.getAlertInfo($line);
130
+ if (alertInfo) {
131
+ const className = this.getPanelClass(alertInfo.type);
132
+ const title = this.getPanelTitle(alertInfo.type);
133
+ tagOpen = `<div class="${className}"${
134
+ isRoot ? rootAttrs : ''
135
+ }><div class="cherry-panel--title cherry-panel--title__not-empty">${title}</div><div class="cherry-panel--body"><p>`;
136
+ tagClose = '</p></div></div>';
137
+ $line = alertInfo.remaining;
138
+ if ($line.trim() === '') {
139
+ skipNextBr = true;
140
+ }
141
+ }
142
+ }
143
+
144
+ handledHtml += tagOpen;
145
+ tagStack.push(tagClose);
146
+ isRoot = false;
147
+ }
148
+ lastLevel = currentLevel;
81
149
  }
82
150
  // 插入当前行内容
83
151
  handledHtml += $line;
84
152
  }
85
153
  // 标签闭合
86
- handledHtml += '</blockquote>'.repeat(lastLevel);
154
+ while (tagStack.length) {
155
+ handledHtml += tagStack.pop();
156
+ }
87
157
  return this.getCacheWithSpace(this.pushCache(handledHtml, sign, lineCount), match);
88
158
  });
89
159
  }
@@ -584,6 +584,9 @@ export default class CodeBlock extends ParagraphBase {
584
584
  if (Prism.languages[lang]) {
585
585
  cacheCode = Prism.highlight(cacheCode, Prism.languages[lang], lang);
586
586
  cacheCode = this.renderLineNumber(cacheCode);
587
+ } else if (lang === 'text') {
588
+ cacheCode = escapeHTMLSpecialChar(cacheCode);
589
+ cacheCode = this.renderLineNumber(cacheCode);
587
590
  } else {
588
591
  cacheCode = escapeHTMLSpecialChar(cacheCode);
589
592
  Prism.plugins.autoloader.loadLanguages(
@@ -626,7 +629,12 @@ export default class CodeBlock extends ParagraphBase {
626
629
  $highlightCodeBlock(code, lang, clazz) {
627
630
  const language = lang;
628
631
  // 直接使用原始代码,不做 unescapeHTMLSpecialChar,避免破坏代码完整性
629
- let cacheCode = Prism.highlight(code, Prism.languages[language], language);
632
+ let cacheCode;
633
+ if (Prism.languages[language]) {
634
+ cacheCode = Prism.highlight(code, Prism.languages[language], language);
635
+ } else {
636
+ cacheCode = escapeHTMLSpecialChar(code);
637
+ }
630
638
  cacheCode = this.renderLineNumber(cacheCode);
631
639
  cacheCode = this.wrapCode(cacheCode, language);
632
640
  const cherry = this.$engine.$cherry;
@@ -801,9 +809,20 @@ export default class CodeBlock extends ParagraphBase {
801
809
  }
802
810
  // $code = this.$replaceSpecialChar($code);
803
811
  $code = $code.replace(/~X/g, '\\`');
812
+
813
+ // 判断是否需要异步渲染,如果需要异步渲染则不缓存
814
+ let effectiveLang = $lang;
815
+ if (!supportLanguages.includes(effectiveLang)) {
816
+ effectiveLang = 'text';
817
+ }
818
+ const isAsync =
819
+ !this.customHighlighter && !Prism.languages[effectiveLang] && effectiveLang !== 'text';
820
+
804
821
  cacheCode = this.renderCodeBlock($code, $lang, sign, lines);
805
822
  cacheCode = cacheCode.replace(/\\/g, '\\\\');
806
- cacheCode = this.$codeCache(sign, cacheCode);
823
+ if (!isAsync) {
824
+ cacheCode = this.$codeCache(sign, cacheCode);
825
+ }
807
826
  const result = this.getCacheWithSpace(this.pushCache(cacheCode, sign, lines), match);
808
827
  return addBlockQuoteSignToResult(result);
809
828
  });
@@ -252,7 +252,7 @@ export default class List extends ParagraphBase {
252
252
  const ret = {
253
253
  begin: '(?:^|\n)(\n*)(([ ]{0,3}([*+-]|\\d+[.]|[a-z]\\.|[I一二三四五六七八九十]+\\.)[ \\t]+)',
254
254
  content: '([^\\r]+?)',
255
- end: '(~0|\\n{2,}(?=\\S)(?![ \\t]*(?:[*+-]|\\d+[.]|[a-z]\\.|[I一二三四五六七八九十]+\\.)[ \\t]+)))',
255
+ end: '(~0|(?=\\n[ \\t]*(?::::|\\+\\+))|\\n{2,}(?=\\S)(?![ \\t]*(?:[*+-]|\\d+[.]|[a-z]\\.|[I一二三四五六七八九十]+\\.)[ \\t]+)))',
256
256
  };
257
257
  ret.reg = new RegExp(ret.begin + ret.content + ret.end, 'gm');
258
258
  return ret;
@@ -1,53 +0,0 @@
1
- https://github.com/liu11hao11/pinyin_js
2
-
3
- # pinyin_js
4
- 中文转拼音
5
- ##安装
6
- ```
7
- npm install
8
- ```
9
-
10
-
11
- ##汉字转化成带音节的拼音
12
- ```javascript
13
- var pinyin=require("pinyin_js");
14
- console.log(pinyin.pinyin("你好"," "));
15
- //输出结果是nǐ hǎo
16
- ```
17
-
18
- ##汉字转化成不带音节的拼音
19
- ```javascript
20
- var pinyin=require("pinyin_js");
21
- console.log(pinyin.pinyinWithOutYin("你好"," "));
22
- //输出结果是ni hao
23
- ```
24
-
25
- ##判断是否是汉字
26
- ```javascript
27
- var pinyin=require("pinyin_js");
28
- console.log(pinyin.isChineseWord("你好"));//true
29
- console.log(pinyin.isChineseWord("!你好",false));//true
30
- console.log(pinyin.isChineseWord("!你好",true));//第二个参数:true是严格模式,默认为严格模式
31
- //false
32
- ```
33
-
34
- ##首字母排序
35
-
36
- ```javascript
37
- var pinyin=require("pinyin_js");
38
- var users = [
39
- { 'user': '张三丰', 'age': 40 },
40
- { 'user': '123', 'age': 48 },
41
- { 'user': '张三', 'age': 48 },
42
- { 'user': '李四', 'age': 36 },
43
- { 'user': '张三炮', 'age': 34 }
44
- ];
45
- var sortResult = pinyin.sort(users, "user");
46
- console.log(sortResult)
47
- /*[ { user: '123', age: 48 },
48
- { user: '李四', age: 36 },
49
- { user: '张三', age: 48 },
50
- { user: '张三丰', age: 40 },
51
- { user: '张三炮', age: 34 } ]*/
52
-
53
- ```
package/tools/README.md DELETED
@@ -1,3 +0,0 @@
1
- # Emoji处理
2
-
3
- 数据来源:[gemoji](https://github.com/github/gemoji/tree/master/db)