@cherry-markdown/cherry-markdown-dev 0.8.58-dev

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.
Files changed (319) hide show
  1. package/package.json +149 -0
  2. package/src/Cherry.config.js +625 -0
  3. package/src/Cherry.js +1104 -0
  4. package/src/CherryStatic.js +70 -0
  5. package/src/Editor.js +748 -0
  6. package/src/Engine.js +381 -0
  7. package/src/Event.js +140 -0
  8. package/src/Factory.js +180 -0
  9. package/src/Logger.js +31 -0
  10. package/src/Previewer.js +1183 -0
  11. package/src/Sanitizer.js +4 -0
  12. package/src/Sanitizer.node.js +7 -0
  13. package/src/UrlCache.js +98 -0
  14. package/src/addons/advance/cherry-table-echarts-plugin.js +170 -0
  15. package/src/addons/cherry-code-block-mermaid-plugin.js +158 -0
  16. package/src/addons/cherry-code-block-plantuml-plugin.js +106 -0
  17. package/src/core/HookCenter.js +297 -0
  18. package/src/core/HooksConfig.js +100 -0
  19. package/src/core/ParagraphBase.js +332 -0
  20. package/src/core/SentenceBase.js +65 -0
  21. package/src/core/SyntaxBase.js +194 -0
  22. package/src/core/hooks/AutoLink.js +232 -0
  23. package/src/core/hooks/BackgroundColor.js +46 -0
  24. package/src/core/hooks/Blockquote.js +70 -0
  25. package/src/core/hooks/Br.js +85 -0
  26. package/src/core/hooks/CodeBlock.js +446 -0
  27. package/src/core/hooks/Color.js +46 -0
  28. package/src/core/hooks/CommentReference.js +96 -0
  29. package/src/core/hooks/Detail.js +108 -0
  30. package/src/core/hooks/Emoji.config.js +1825 -0
  31. package/src/core/hooks/Emoji.js +119 -0
  32. package/src/core/hooks/Emphasis.js +113 -0
  33. package/src/core/hooks/Footnote.js +125 -0
  34. package/src/core/hooks/FrontMatter.js +51 -0
  35. package/src/core/hooks/Header.js +234 -0
  36. package/src/core/hooks/HighLight.js +37 -0
  37. package/src/core/hooks/Hr.js +52 -0
  38. package/src/core/hooks/HtmlBlock.js +184 -0
  39. package/src/core/hooks/Image.js +174 -0
  40. package/src/core/hooks/InlineCode.js +48 -0
  41. package/src/core/hooks/InlineMath.js +107 -0
  42. package/src/core/hooks/Link.js +160 -0
  43. package/src/core/hooks/List.js +264 -0
  44. package/src/core/hooks/MathBlock.js +103 -0
  45. package/src/core/hooks/Panel.js +145 -0
  46. package/src/core/hooks/Paragraph.js +84 -0
  47. package/src/core/hooks/Ruby.js +34 -0
  48. package/src/core/hooks/Size.js +51 -0
  49. package/src/core/hooks/Strikethrough.js +54 -0
  50. package/src/core/hooks/Sub.js +47 -0
  51. package/src/core/hooks/SuggestList.js +333 -0
  52. package/src/core/hooks/Suggester.js +707 -0
  53. package/src/core/hooks/Sup.js +47 -0
  54. package/src/core/hooks/Table.js +275 -0
  55. package/src/core/hooks/Toc.js +292 -0
  56. package/src/core/hooks/Transfer.js +47 -0
  57. package/src/core/hooks/Underline.js +37 -0
  58. package/src/index.core.js +29 -0
  59. package/src/index.engine.core.js +68 -0
  60. package/src/index.engine.js +28 -0
  61. package/src/index.js +32 -0
  62. package/src/libs/mermaidAPI.8.4.8.js +1 -0
  63. package/src/libs/mermaidAPI.8.5.2.js +42 -0
  64. package/src/libs/rawdeflate.js +1663 -0
  65. package/src/locales/en_US.js +139 -0
  66. package/src/locales/index.js +25 -0
  67. package/src/locales/ru_RU.js +139 -0
  68. package/src/locales/zh_CN.js +142 -0
  69. package/src/sass/base.scss +26 -0
  70. package/src/sass/bubble_formula.scss +166 -0
  71. package/src/sass/ch-icon.scss +118 -0
  72. package/src/sass/cherry.scss +1116 -0
  73. package/src/sass/components/bubble.scss +173 -0
  74. package/src/sass/components/shortcut_key_config.scss +108 -0
  75. package/src/sass/formula_utils_bubble.scss +82 -0
  76. package/src/sass/icon_template.scss +24 -0
  77. package/src/sass/icons/uEA03-list.svg +19 -0
  78. package/src/sass/icons/uEA04-check.svg +14 -0
  79. package/src/sass/icons/uEA09-square.svg +10 -0
  80. package/src/sass/icons/uEA0A-bold.svg +20 -0
  81. package/src/sass/icons/uEA0B-code.svg +18 -0
  82. package/src/sass/icons/uEA0C-color.svg +13 -0
  83. package/src/sass/icons/uEA0D-header.svg +8 -0
  84. package/src/sass/icons/uEA0E-image.svg +15 -0
  85. package/src/sass/icons/uEA0F-italic.svg +8 -0
  86. package/src/sass/icons/uEA10-link.svg +16 -0
  87. package/src/sass/icons/uEA11-ol.svg +21 -0
  88. package/src/sass/icons/uEA12-size.svg +11 -0
  89. package/src/sass/icons/uEA13-strike.svg +16 -0
  90. package/src/sass/icons/uEA14-table.svg +12 -0
  91. package/src/sass/icons/uEA15-ul.svg +17 -0
  92. package/src/sass/icons/uEA16-underline.svg +13 -0
  93. package/src/sass/icons/uEA17-word.svg +16 -0
  94. package/src/sass/icons/uEA18-blockquote.svg +11 -0
  95. package/src/sass/icons/uEA19-font.svg +10 -0
  96. package/src/sass/icons/uEA1F-insertClass.svg +39 -0
  97. package/src/sass/icons/uEA20-insertFlow.svg +8 -0
  98. package/src/sass/icons/uEA21-insertFormula.svg +23 -0
  99. package/src/sass/icons/uEA22-insertGantt.svg +13 -0
  100. package/src/sass/icons/uEA23-insertGraph.svg +13 -0
  101. package/src/sass/icons/uEA24-insertPie.svg +19 -0
  102. package/src/sass/icons/uEA25-insertSeq.svg +20 -0
  103. package/src/sass/icons/uEA26-insertState.svg +35 -0
  104. package/src/sass/icons/uEA27-line.svg +11 -0
  105. package/src/sass/icons/uEA28-preview.svg +18 -0
  106. package/src/sass/icons/uEA29-previewClose.svg +24 -0
  107. package/src/sass/icons/uEA2A-toc.svg +24 -0
  108. package/src/sass/icons/uEA2D-sub.svg +15 -0
  109. package/src/sass/icons/uEA2E-sup.svg +15 -0
  110. package/src/sass/icons/uEA2F-h1.svg +16 -0
  111. package/src/sass/icons/uEA30-h2.svg +20 -0
  112. package/src/sass/icons/uEA31-h3.svg +23 -0
  113. package/src/sass/icons/uEA32-h4.svg +16 -0
  114. package/src/sass/icons/uEA33-h5.svg +20 -0
  115. package/src/sass/icons/uEA34-h6.svg +17 -0
  116. package/src/sass/icons/uEA35-video.svg +20 -0
  117. package/src/sass/icons/uEA36-insert.svg +25 -0
  118. package/src/sass/icons/uEA37-little_table.svg +30 -0
  119. package/src/sass/icons/uEA38-pdf.svg +27 -0
  120. package/src/sass/icons/uEA39-checklist.svg +22 -0
  121. package/src/sass/icons/uEA40-close.svg +12 -0
  122. package/src/sass/icons/uEA41-fullscreen.svg +81 -0
  123. package/src/sass/icons/uEA42-minscreen.svg +77 -0
  124. package/src/sass/icons/uEA43-insertChart.svg +23 -0
  125. package/src/sass/icons/uEA44-question.svg +25 -0
  126. package/src/sass/icons/uEA45-settings.svg +32 -0
  127. package/src/sass/icons/uEA46-ok.svg +7 -0
  128. package/src/sass/icons/uEA47-br.svg +22 -0
  129. package/src/sass/icons/uEA48-normal.svg +15 -0
  130. package/src/sass/icons/uEA49-undo.svg +19 -0
  131. package/src/sass/icons/uEA50-redo.svg +21 -0
  132. package/src/sass/icons/uEA51-copy.svg +6 -0
  133. package/src/sass/icons/uEA52-phone.svg +5 -0
  134. package/src/sass/icons/uEA53-cherry-table-delete.svg +17 -0
  135. package/src/sass/icons/uEA54-cherry-table-insert-bottom.svg +16 -0
  136. package/src/sass/icons/uEA55-cherry-table-insert-left.svg +15 -0
  137. package/src/sass/icons/uEA56-cherry-table-insert-right.svg +16 -0
  138. package/src/sass/icons/uEA57-cherry-table-insert-top.svg +16 -0
  139. package/src/sass/icons/uEA58-sort-s.svg +13 -0
  140. package/src/sass/icons/uEA59-pinyin.svg +1 -0
  141. package/src/sass/icons/uEA5A-create.svg +24 -0
  142. package/src/sass/icons/uEA5B-download.svg +34 -0
  143. package/src/sass/icons/uEA5C-edit.svg +3 -0
  144. package/src/sass/icons/uEA5D-export.svg +53 -0
  145. package/src/sass/icons/uEA5E-folder-open.svg +3 -0
  146. package/src/sass/icons/uEA5F-folder.svg +3 -0
  147. package/src/sass/icons/uEA60-help.svg +5 -0
  148. package/src/sass/icons/uEA61-pen-fill.svg +13 -0
  149. package/src/sass/icons/uEA62-pen.svg +3 -0
  150. package/src/sass/icons/uEA64-tips.svg +5 -0
  151. package/src/sass/icons/uEA65-warn.svg +5 -0
  152. package/src/sass/icons/uEA66-mistake.svg +4 -0
  153. package/src/sass/icons/uEA67-success.svg +4 -0
  154. package/src/sass/icons/uEA68-danger.svg +4 -0
  155. package/src/sass/icons/uEA69-info.svg +5 -0
  156. package/src/sass/icons/uEA6A-primary.svg +5 -0
  157. package/src/sass/icons/uEA6B-warning.svg +5 -0
  158. package/src/sass/icons/uEA6C-justify.svg +19 -0
  159. package/src/sass/icons/uEA6D-justifyCenter.svg +19 -0
  160. package/src/sass/icons/uEA6E-justifyLeft.svg +19 -0
  161. package/src/sass/icons/uEA6F-justifyRight.svg +19 -0
  162. package/src/sass/icons/uEA70-chevronsLeft.svg +1 -0
  163. package/src/sass/icons/uEA71-chevronsRight.svg +1 -0
  164. package/src/sass/icons/uEA72-trendingUp.svg +1 -0
  165. package/src/sass/icons/uEA74-codeBlock.svg +1 -0
  166. package/src/sass/icons/uEA75-expand.svg +3 -0
  167. package/src/sass/icons/uEA76-unExpand.svg +3 -0
  168. package/src/sass/icons/uEA77-swap-vert.svg +1 -0
  169. package/src/sass/icons/uEA78-swap.svg +1 -0
  170. package/src/sass/icons/uEA79-keyboard.svg +1 -0
  171. package/src/sass/icons/uEA7A-command.svg +1 -0
  172. package/src/sass/icons/uEA7B-search.svg +1 -0
  173. package/src/sass/index.scss +3 -0
  174. package/src/sass/markdown.scss +668 -0
  175. package/src/sass/markdown_pure.scss +9 -0
  176. package/src/sass/prettyprint/prettyprint.scss +118 -0
  177. package/src/sass/previewer.scss +179 -0
  178. package/src/sass/print.scss +13 -0
  179. package/src/sass/prism/coy.scss +220 -0
  180. package/src/sass/prism/dark.scss +132 -0
  181. package/src/sass/prism/default.scss +143 -0
  182. package/src/sass/prism/funky.scss +133 -0
  183. package/src/sass/prism/okaidia.scss +126 -0
  184. package/src/sass/prism/one-dark.scss +440 -0
  185. package/src/sass/prism/one-light.scss +428 -0
  186. package/src/sass/prism/solarized-light.scss +153 -0
  187. package/src/sass/prism/tomorrow-night.scss +125 -0
  188. package/src/sass/prism/twilight.scss +202 -0
  189. package/src/sass/prism/vs-dark.scss +275 -0
  190. package/src/sass/prism/vs-light.scss +168 -0
  191. package/src/sass/themes/blue.scss +411 -0
  192. package/src/sass/themes/dark.scss +517 -0
  193. package/src/sass/themes/default.scss +255 -0
  194. package/src/sass/themes/green.scss +395 -0
  195. package/src/sass/themes/light.scss +368 -0
  196. package/src/sass/themes/red.scss +397 -0
  197. package/src/sass/themes/violet.scss +410 -0
  198. package/src/sass/variable.scss +84 -0
  199. package/src/toolbars/Bubble.js +234 -0
  200. package/src/toolbars/BubbleFormula.js +298 -0
  201. package/src/toolbars/BubbleTable.js +147 -0
  202. package/src/toolbars/FloatMenu.js +131 -0
  203. package/src/toolbars/HiddenToolbar.js +36 -0
  204. package/src/toolbars/HookCenter.js +234 -0
  205. package/src/toolbars/MenuBase.js +569 -0
  206. package/src/toolbars/PreviewerBubble.js +608 -0
  207. package/src/toolbars/ShortcutKeyConfigPanel.js +345 -0
  208. package/src/toolbars/Sidebar.js +36 -0
  209. package/src/toolbars/Toc.js +242 -0
  210. package/src/toolbars/Toolbar.js +449 -0
  211. package/src/toolbars/ToolbarRight.js +37 -0
  212. package/src/toolbars/hooks/Audio.js +79 -0
  213. package/src/toolbars/hooks/BarTable.js +41 -0
  214. package/src/toolbars/hooks/Bold.js +73 -0
  215. package/src/toolbars/hooks/Br.js +34 -0
  216. package/src/toolbars/hooks/ChangeLocale.js +62 -0
  217. package/src/toolbars/hooks/ChatGpt.js +182 -0
  218. package/src/toolbars/hooks/CheckList.js +41 -0
  219. package/src/toolbars/hooks/Code.js +49 -0
  220. package/src/toolbars/hooks/CodeTheme.js +66 -0
  221. package/src/toolbars/hooks/Color.js +298 -0
  222. package/src/toolbars/hooks/Copy.js +141 -0
  223. package/src/toolbars/hooks/Detail.js +69 -0
  224. package/src/toolbars/hooks/DrawIo.js +57 -0
  225. package/src/toolbars/hooks/Export.js +49 -0
  226. package/src/toolbars/hooks/File.js +79 -0
  227. package/src/toolbars/hooks/Formula.js +69 -0
  228. package/src/toolbars/hooks/FullScreen.js +50 -0
  229. package/src/toolbars/hooks/Graph.js +263 -0
  230. package/src/toolbars/hooks/H1.js +71 -0
  231. package/src/toolbars/hooks/H2.js +71 -0
  232. package/src/toolbars/hooks/H3.js +71 -0
  233. package/src/toolbars/hooks/Header.js +118 -0
  234. package/src/toolbars/hooks/Hr.js +35 -0
  235. package/src/toolbars/hooks/Image.js +91 -0
  236. package/src/toolbars/hooks/InlineCode.js +53 -0
  237. package/src/toolbars/hooks/Insert.js +193 -0
  238. package/src/toolbars/hooks/Italic.js +72 -0
  239. package/src/toolbars/hooks/Justify.js +49 -0
  240. package/src/toolbars/hooks/LineTable.js +41 -0
  241. package/src/toolbars/hooks/Link.js +49 -0
  242. package/src/toolbars/hooks/List.js +55 -0
  243. package/src/toolbars/hooks/MobilePreview.js +44 -0
  244. package/src/toolbars/hooks/Ol.js +41 -0
  245. package/src/toolbars/hooks/Panel.js +140 -0
  246. package/src/toolbars/hooks/Pdf.js +78 -0
  247. package/src/toolbars/hooks/Publish.js +123 -0
  248. package/src/toolbars/hooks/QuickTable.js +43 -0
  249. package/src/toolbars/hooks/Quote.js +45 -0
  250. package/src/toolbars/hooks/Redo.js +33 -0
  251. package/src/toolbars/hooks/Ruby.js +59 -0
  252. package/src/toolbars/hooks/Search.js +53 -0
  253. package/src/toolbars/hooks/Settings.js +220 -0
  254. package/src/toolbars/hooks/ShortcutKey.js +62 -0
  255. package/src/toolbars/hooks/Size.js +118 -0
  256. package/src/toolbars/hooks/Split.js +37 -0
  257. package/src/toolbars/hooks/Strikethrough.js +71 -0
  258. package/src/toolbars/hooks/Sub.js +58 -0
  259. package/src/toolbars/hooks/Sup.js +58 -0
  260. package/src/toolbars/hooks/SwitchModel.js +56 -0
  261. package/src/toolbars/hooks/Table.js +56 -0
  262. package/src/toolbars/hooks/Theme.js +62 -0
  263. package/src/toolbars/hooks/Toc.js +35 -0
  264. package/src/toolbars/hooks/TogglePreview.js +91 -0
  265. package/src/toolbars/hooks/Ul.js +41 -0
  266. package/src/toolbars/hooks/Underline.js +68 -0
  267. package/src/toolbars/hooks/Undo.js +30 -0
  268. package/src/toolbars/hooks/Video.js +79 -0
  269. package/src/toolbars/hooks/Word.js +78 -0
  270. package/src/toolbars/hooks/WordCount.js +106 -0
  271. package/src/utils/autoindent.js +58 -0
  272. package/src/utils/cm-search-replace.js +794 -0
  273. package/src/utils/code-preview-language-setting.js +180 -0
  274. package/src/utils/codeBlockContentHandler.js +400 -0
  275. package/src/utils/config.js +174 -0
  276. package/src/utils/copy.js +55 -0
  277. package/src/utils/dialog.js +214 -0
  278. package/src/utils/dom.js +163 -0
  279. package/src/utils/downloadUtil.js +23 -0
  280. package/src/utils/env.js +22 -0
  281. package/src/utils/error.js +61 -0
  282. package/src/utils/event.js +38 -0
  283. package/src/utils/export.js +166 -0
  284. package/src/utils/file.js +164 -0
  285. package/src/utils/formulaUtilsHandler.js +232 -0
  286. package/src/utils/htmlparser.js +976 -0
  287. package/src/utils/image.js +99 -0
  288. package/src/utils/imgSizeHandler.js +279 -0
  289. package/src/utils/lazyLoadImg.js +327 -0
  290. package/src/utils/lineFeed.js +49 -0
  291. package/src/utils/listContentHandler.js +227 -0
  292. package/src/utils/lookbehind-replace.js +81 -0
  293. package/src/utils/mathjax.js +89 -0
  294. package/src/utils/myersDiff.js +211 -0
  295. package/src/utils/pasteHelper.js +253 -0
  296. package/src/utils/platformTransform.js +71 -0
  297. package/src/utils/recount-pos.js +59 -0
  298. package/src/utils/regexp.js +295 -0
  299. package/src/utils/sanitize.js +477 -0
  300. package/src/utils/selection.js +50 -0
  301. package/src/utils/shortcutKey.js +291 -0
  302. package/src/utils/svgUtils.js +96 -0
  303. package/src/utils/tableContentHandler.js +876 -0
  304. package/test/core/CommonMark.spec.ts +62 -0
  305. package/test/core/hooks/AutoLink.spec.ts +28 -0
  306. package/test/core/hooks/List.spec.ts +79 -0
  307. package/test/core/hooks/__snapshots__/List.spec.ts.snap +11 -0
  308. package/test/example.md +778 -0
  309. package/test/node.js +10 -0
  310. package/test/suites/commonmark.spec.json +5218 -0
  311. package/test/tsconfig.test.json +6 -0
  312. package/test/utils/regexp.spec.ts +28 -0
  313. package/types/cherry.d.ts +675 -0
  314. package/types/codemirror.d.ts +22 -0
  315. package/types/editor.d.ts +72 -0
  316. package/types/global.d.ts +16 -0
  317. package/types/menus.d.ts +24 -0
  318. package/types/previewer.d.ts +53 -0
  319. package/types/syntax.d.ts +52 -0
@@ -0,0 +1,4 @@
1
+ import createDOMPurify from 'dompurify';
2
+
3
+ // for browser
4
+ export const sanitizer = createDOMPurify(window);
@@ -0,0 +1,7 @@
1
+ import createDOMPurify from 'dompurify';
2
+ import { JSDOM } from 'jsdom';
3
+
4
+ const { window } = new JSDOM('');
5
+
6
+ // @ts-expect-error
7
+ export const sanitizer = createDOMPurify(window);
@@ -0,0 +1,98 @@
1
+ /**
2
+ * Copyright (C) 2021 THL A29 Limited, a Tencent company.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ import CryptoJS from 'crypto-js';
17
+
18
+ let urlCache = {};
19
+ const cherryInnerLinkRegex = /^cherry-inner:\/\/([0-9a-f]+)$/i;
20
+
21
+ export function urlProcessorProxy(urlProcessor) {
22
+ return function (url, srcType, callback) {
23
+ if (UrlCache.isInnerLink(url)) {
24
+ const newUrl = urlProcessor(UrlCache.get(url), srcType, callback);
25
+ return UrlCache.replace(url, newUrl);
26
+ }
27
+ return urlProcessor(url, srcType, callback);
28
+ };
29
+ }
30
+
31
+ export default class UrlCache {
32
+ /**
33
+ * 判断url是否Cherry的内部链接
34
+ * @param {string} url 要检测的URL
35
+ * @returns
36
+ */
37
+ static isInnerLink(url) {
38
+ return cherryInnerLinkRegex.test(url);
39
+ }
40
+
41
+ /**
42
+ * 缓存url为内部链接,主要用于缩短超长链接,避免正则超时
43
+ * @param {string} url 要转换为内部链接的URL
44
+ * @returns
45
+ */
46
+ static set(url) {
47
+ const urlSign = CryptoJS.SHA256(url).toString();
48
+ urlCache[urlSign] = url;
49
+ return `cherry-inner://${urlSign}`;
50
+ }
51
+
52
+ /**
53
+ * 获取原始链接
54
+ * @param {string} innerUrl 内部链接
55
+ * @returns
56
+ */
57
+ static get(innerUrl) {
58
+ const [, urlSign] = innerUrl.match(cherryInnerLinkRegex) ?? [];
59
+ if (!urlSign) {
60
+ return;
61
+ }
62
+ return urlCache[urlSign];
63
+ }
64
+
65
+ /**
66
+ * 替换指定内部链接的真实地址
67
+ * @param {string} innerUrl 原始内部链接
68
+ * @param {string} newUrl 需要替换的链接
69
+ */
70
+ static replace(innerUrl, newUrl) {
71
+ const [, urlSign] = innerUrl.match(cherryInnerLinkRegex) ?? [];
72
+ if (!urlSign) {
73
+ return;
74
+ }
75
+ urlCache[urlSign] = newUrl;
76
+ return innerUrl;
77
+ }
78
+
79
+ /**
80
+ * 替换所有内部链接为原始的真实地址
81
+ * @param {string} html 包含 cherry-inner 协议地址的 html 文本
82
+ */
83
+ static restoreAll(html) {
84
+ const cherryInnerLinkRegex = /cherry-inner:\/\/([0-9a-f]+)/gi;
85
+ const $html = html.replace(cherryInnerLinkRegex, (match) => {
86
+ const originalUrl = UrlCache.get(match);
87
+ return originalUrl || match;
88
+ });
89
+ return $html;
90
+ }
91
+
92
+ /**
93
+ * 清空缓存
94
+ */
95
+ static clear() {
96
+ urlCache = {};
97
+ }
98
+ }
@@ -0,0 +1,170 @@
1
+ /**
2
+ * Tencent is pleased to support the open source community by making CherryMarkdown available.
3
+ *
4
+ * Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved.
5
+ * The below software in this distribution may have been modified by THL A29 Limited ("Tencent Modifications").
6
+ *
7
+ * All Tencent Modifications are Copyright (C) THL A29 Limited.
8
+ *
9
+ * CherryMarkdown is licensed under the Apache License, Version 2.0 (the "License");
10
+ * you may not use this file except in compliance with the License.
11
+ * You may obtain a copy of the License at
12
+ *
13
+ * http://www.apache.org/licenses/LICENSE-2.0
14
+ *
15
+ * Unless required by applicable law or agreed to in writing, software
16
+ * distributed under the License is distributed on an "AS IS" BASIS,
17
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
+ * See the License for the specific language governing permissions and
19
+ * limitations under the License.
20
+ */
21
+ import mergeWith from 'lodash/mergeWith';
22
+
23
+ const DEFAULT_OPTIONS = {
24
+ renderer: 'svg',
25
+ width: 500,
26
+ height: 300,
27
+ };
28
+
29
+ export default class EChartsTableEngine {
30
+ static install(cherryOptions, ...args) {
31
+ if (typeof window === 'undefined') {
32
+ console.warn('echarts-table-engine only works in browser.');
33
+ mergeWith(cherryOptions, {
34
+ engine: {
35
+ syntax: {
36
+ table: {
37
+ enableChart: false,
38
+ },
39
+ },
40
+ },
41
+ });
42
+ return;
43
+ }
44
+ mergeWith(cherryOptions, {
45
+ engine: {
46
+ syntax: {
47
+ table: {
48
+ enableChart: true,
49
+ chartRenderEngine: EChartsTableEngine,
50
+ externals: ['echarts'],
51
+ },
52
+ },
53
+ },
54
+ });
55
+ }
56
+
57
+ constructor(echartsOptions = {}) {
58
+ const { echarts, ...options } = echartsOptions;
59
+ if (!echarts && !window.echarts) {
60
+ throw new Error('table-echarts-plugin[init]: Package echarts not found.');
61
+ }
62
+ this.options = { ...DEFAULT_OPTIONS, ...(options || {}) };
63
+ this.echartsRef = echarts || window.echarts; // echarts引用
64
+ this.dom = null;
65
+ }
66
+
67
+ getInstance() {
68
+ if (!this.dom) {
69
+ this.dom = document.createElement('div');
70
+ this.echartsRef.init(this.dom, null, this.options);
71
+ }
72
+ return this.echartsRef.getInstanceByDom(this.dom);
73
+ }
74
+
75
+ render(type, options, tableObject) {
76
+ // console.log(type, options, tableObject);
77
+ let chartOption = {};
78
+ switch (type) {
79
+ case 'bar':
80
+ chartOption = this.renderBarChart(tableObject, options);
81
+ break;
82
+ case 'line':
83
+ chartOption = this.renderLineChart(tableObject, options);
84
+ break;
85
+ default:
86
+ return '';
87
+ }
88
+ const eChartInstance = this.getInstance();
89
+ eChartInstance.clear();
90
+ eChartInstance.setOption(chartOption);
91
+ return eChartInstance.getDom().innerHTML;
92
+ }
93
+
94
+ renderBarChart(tableObject, options) {
95
+ return this.$renderChartCommon(tableObject, options, 'bar');
96
+ }
97
+
98
+ renderLineChart(tableObject, options) {
99
+ return this.$renderChartCommon(tableObject, options, 'line');
100
+ }
101
+
102
+ $renderChartCommon(tableObject, options, type) {
103
+ // TODO: 通过options反转xy轴
104
+ const baseSeries = {
105
+ bar: {
106
+ type: 'bar',
107
+ barWidth: 20,
108
+ animation: false,
109
+ name: '',
110
+ data: [],
111
+ },
112
+ line: {
113
+ type: 'line',
114
+ animation: false,
115
+ name: '',
116
+ data: [],
117
+ },
118
+ };
119
+ if (!baseSeries[type]) {
120
+ return;
121
+ }
122
+ const dataSet = tableObject.rows.reduce(
123
+ (result, row) => {
124
+ // legend
125
+ result.legend.data.push(row[0]);
126
+ // series
127
+ result.series.push({
128
+ ...baseSeries[type],
129
+ name: row[0],
130
+ data: row.slice(1).map((data) => {
131
+ const num = Number.parseFloat(data.replace(/,/g, ''));
132
+ return num;
133
+ }),
134
+ });
135
+ return result;
136
+ },
137
+ {
138
+ legend: { data: [] },
139
+ series: [],
140
+ },
141
+ );
142
+ const chartOptions = {
143
+ ...dataSet,
144
+ xAxis: {
145
+ data: tableObject.header.slice(1),
146
+ type: 'category',
147
+ },
148
+ yAxis: {
149
+ type: 'value',
150
+ axisLabel: {
151
+ width: '100%',
152
+ },
153
+ },
154
+ grid: {
155
+ containLabel: true,
156
+ left: '1%',
157
+ right: '1%',
158
+ bottom: '10%',
159
+ },
160
+ };
161
+ return chartOptions;
162
+ }
163
+
164
+ onDestroy() {
165
+ if (!this.dom) {
166
+ return;
167
+ }
168
+ this.echartsRef.dispose(this.dom);
169
+ }
170
+ }
@@ -0,0 +1,158 @@
1
+ /**
2
+ * Copyright (C) 2021 THL A29 Limited, a Tencent company.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ import mergeWith from 'lodash/mergeWith';
17
+
18
+ const DEFAULT_OPTIONS = {
19
+ // TODO: themes
20
+ theme: 'default',
21
+ altFontFamily: 'sans-serif',
22
+ fontFamily: 'sans-serif',
23
+ themeCSS: '.label foreignObject { font-size: 90%; overflow: visible; } .label { font-family: sans-serif; }',
24
+ flowchart: {
25
+ useMaxWidth: false,
26
+ },
27
+ sequence: {
28
+ useMaxWidth: false,
29
+ },
30
+ startOnLoad: false,
31
+ logLevel: 5,
32
+ // fontFamily: 'Arial, monospace'
33
+ };
34
+
35
+ export default class MermaidCodeEngine {
36
+ static TYPE = 'figure';
37
+
38
+ static install(cherryOptions, ...args) {
39
+ mergeWith(cherryOptions, {
40
+ engine: {
41
+ syntax: {
42
+ codeBlock: {
43
+ customRenderer: {
44
+ mermaid: new MermaidCodeEngine(...args),
45
+ },
46
+ },
47
+ },
48
+ },
49
+ });
50
+ }
51
+
52
+ mermaidAPIRefs = null;
53
+ options = DEFAULT_OPTIONS;
54
+ dom = null;
55
+ mermaidCanvas = null;
56
+
57
+ constructor(mermaidOptions = {}) {
58
+ const { mermaid, mermaidAPI } = mermaidOptions;
59
+ if (
60
+ !mermaidAPI &&
61
+ !window.mermaidAPI &&
62
+ (!mermaid || !mermaid.mermaidAPI) &&
63
+ (!window.mermaid || !window.mermaid.mermaidAPI)
64
+ ) {
65
+ throw new Error('code-block-mermaid-plugin[init]: Package mermaid or mermaidAPI not found.');
66
+ }
67
+ this.options = { ...DEFAULT_OPTIONS, ...(mermaidOptions || {}) };
68
+ this.mermaidAPIRefs = mermaidAPI || window.mermaidAPI || mermaid.mermaidAPI || window.mermaid.mermaidAPI;
69
+ delete this.options.mermaid;
70
+ delete this.options.mermaidAPI;
71
+ this.mermaidAPIRefs.initialize(this.options);
72
+ }
73
+
74
+ mountMermaidCanvas($engine) {
75
+ if (this.mermaidCanvas && document.body.contains(this.mermaidCanvas)) {
76
+ return;
77
+ }
78
+ this.mermaidCanvas = document.createElement('div');
79
+ this.mermaidCanvas.style = 'width:1024px;opacity:0;position:fixed;top:100%;';
80
+ const container = $engine.$cherry.wrapperDom || document.body;
81
+ container.appendChild(this.mermaidCanvas);
82
+ }
83
+
84
+ /**
85
+ * 转换svg为img,如果出错则直出svg
86
+ * @param {string} svgCode
87
+ * @param {string} graphId
88
+ * @returns {string}
89
+ */
90
+ convertMermaidSvgToImg(svgCode, graphId) {
91
+ const domParser = new DOMParser();
92
+ let svgHtml;
93
+ const injectSvgFallback = (svg) =>
94
+ svg.replace('<svg ', '<svg style="max-width:100%;height:auto;font-family:sans-serif;" ');
95
+ try {
96
+ const svgDoc = /** @type {XMLDocument} */ (domParser.parseFromString(svgCode, 'image/svg+xml'));
97
+ const svgDom = /** @type {SVGSVGElement} */ (/** @type {any} */ (svgDoc.documentElement));
98
+ // tagName不是svg时,说明存在parse error
99
+ if (svgDom.tagName.toLowerCase() === 'svg') {
100
+ svgDom.style.maxWidth = '100%';
101
+ svgDom.style.height = 'auto';
102
+ svgDom.style.fontFamily = 'sans-serif';
103
+ const shadowSvg = /** @type {SVGSVGElement} */ (/** @type {any} */ (document.getElementById(graphId)));
104
+ let svgBox = shadowSvg.getBBox();
105
+ if (!svgDom.hasAttribute('viewBox')) {
106
+ svgDom.setAttribute('viewBox', `0 0 ${svgBox.width} ${svgBox.height}`);
107
+ } else {
108
+ svgBox = svgDom.viewBox.baseVal;
109
+ }
110
+ svgDom.getAttribute('width') === '100%' && svgDom.setAttribute('width', `${svgBox.width}`);
111
+ svgDom.getAttribute('height') === '100%' && svgDom.setAttribute('height', `${svgBox.height}`);
112
+ // fix end
113
+ svgHtml = svgDoc.documentElement.outerHTML;
114
+ // 屏蔽转img标签功能,如需要转换为img解除屏蔽即可
115
+ if (this.svg2img) {
116
+ const dataUrl = `data:image/svg+xml,${encodeURIComponent(svgDoc.documentElement.outerHTML)}`;
117
+ svgHtml = `<img class="svg-img" src="${dataUrl}" alt="${graphId}" />`;
118
+ }
119
+ } else {
120
+ svgHtml = injectSvgFallback(svgCode);
121
+ }
122
+ } catch (e) {
123
+ svgHtml = injectSvgFallback(svgCode);
124
+ }
125
+ return svgHtml;
126
+ }
127
+
128
+ render(src, sign, $engine, config = {}) {
129
+ let $sign = sign;
130
+ if (!$sign) {
131
+ $sign = Math.round(Math.random() * 100000000);
132
+ }
133
+ this.mountMermaidCanvas($engine);
134
+ let html;
135
+ // 多实例的情况下相同的内容ID相同会导致mermaid渲染异常
136
+ // 需要通过添加时间戳使得多次渲染相同内容的图像ID唯一
137
+ // 图像渲染节流在CodeBlock Hook内部控制
138
+ const graphId = `mermaid-${$sign}-${new Date().getTime()}`;
139
+ this.svg2img = config?.svg2img ?? false;
140
+ try {
141
+ this.mermaidAPIRefs.render(
142
+ graphId,
143
+ src,
144
+ (svgCode) => {
145
+ const fixedSvg = svgCode
146
+ .replace(/\s*markerUnits="0"/g, '')
147
+ .replace(/\s*x="NaN"/g, '')
148
+ .replace(/<br>/g, '<br/>');
149
+ html = this.convertMermaidSvgToImg(fixedSvg, graphId);
150
+ },
151
+ this.mermaidCanvas,
152
+ );
153
+ } catch (e) {
154
+ return e?.str;
155
+ }
156
+ return html;
157
+ }
158
+ }
@@ -0,0 +1,106 @@
1
+ // @ts-nocheck
2
+ /**
3
+ * Copyright (C) 2021 THL A29 Limited, a Tencent company.
4
+ *
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ */
17
+ import mergeWith from 'lodash/mergeWith';
18
+ import deflate from '@/libs/rawdeflate';
19
+
20
+ function encode64(data) {
21
+ let r = '';
22
+ for (let i = 0; i < data.length; i += 3) {
23
+ if (i + 2 === data.length) {
24
+ r += append3bytes(data.charCodeAt(i), data.charCodeAt(i + 1), 0);
25
+ } else if (i + 1 === data.length) {
26
+ r += append3bytes(data.charCodeAt(i), 0, 0);
27
+ } else {
28
+ r += append3bytes(data.charCodeAt(i), data.charCodeAt(i + 1), data.charCodeAt(i + 2));
29
+ }
30
+ }
31
+ return r;
32
+ }
33
+
34
+ function append3bytes(b1, b2, b3) {
35
+ const c1 = b1 >> 2;
36
+ const c2 = ((b1 & 0x3) << 4) | (b2 >> 4);
37
+ const c3 = ((b2 & 0xf) << 2) | (b3 >> 6);
38
+ const c4 = b3 & 0x3f;
39
+ let r = '';
40
+ r += encode6bit(c1 & 0x3f);
41
+ r += encode6bit(c2 & 0x3f);
42
+ r += encode6bit(c3 & 0x3f);
43
+ r += encode6bit(c4 & 0x3f);
44
+ return r;
45
+ }
46
+
47
+ function encode6bit(b1) {
48
+ let b = b1;
49
+ if (b < 10) {
50
+ return String.fromCharCode(48 + b);
51
+ }
52
+ b -= 10;
53
+ if (b < 26) {
54
+ return String.fromCharCode(65 + b);
55
+ }
56
+ b -= 26;
57
+ if (b < 26) {
58
+ return String.fromCharCode(97 + b);
59
+ }
60
+ b -= 26;
61
+ if (b === 0) {
62
+ return '-';
63
+ }
64
+ if (b === 1) {
65
+ return '_';
66
+ }
67
+ return '?';
68
+ }
69
+
70
+ function compress(s1, url) {
71
+ const s = unescape(encodeURIComponent(s1));
72
+ return `${url}/svg/${encode64(deflate(s, 9))}`;
73
+ }
74
+
75
+ export default class PlantUMLCodeEngine {
76
+ static install(cherryOptions, args) {
77
+ mergeWith(cherryOptions, {
78
+ engine: {
79
+ syntax: {
80
+ codeBlock: {
81
+ customRenderer: {
82
+ plantuml: new PlantUMLCodeEngine({
83
+ ...args,
84
+ ...(cherryOptions.engine.syntax.plantuml ?? {}),
85
+ }),
86
+ },
87
+ },
88
+ },
89
+ },
90
+ });
91
+ }
92
+
93
+ constructor(plantUMLOptions = {}) {
94
+ const defaultUrl = 'http://www.plantuml.com/plantuml';
95
+ this.baseUrl = plantUMLOptions.baseUrl ?? defaultUrl;
96
+ }
97
+
98
+ render(src, sign) {
99
+ let $sign = sign;
100
+ if (!$sign) {
101
+ $sign = Math.round(Math.random() * 100000000);
102
+ }
103
+ const graphId = `plantuml-${$sign}-${new Date().getTime()}`;
104
+ return `<img id="${graphId}" src="${compress(src, this.baseUrl)}" />`;
105
+ }
106
+ }