Skip to content

Commit a275692

Browse files
lyngaiRSS1102
andauthored
fix: image syntax compatibility (#1555)
* fix(ref): fix use reference after empty alt text * fix(cache): fix broken data uri after bigdata restored * fix(checkbox): fix incorrect checkbox replacement from click * fix(url): fix empty url not restored * chore: update snapshot * chore: update yarn lock * chore: add changeset Addresses image syntax issues and resolves bug #1554. --------- Co-authored-by: 阿菜 Cai <[email protected]>
1 parent b261e16 commit a275692

File tree

8 files changed

+185
-536
lines changed

8 files changed

+185
-536
lines changed

.changeset/tiny-items-scream.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"cherry-markdown": patch
3+
---
4+
5+
fix: image syntax compatibility, Fixes #1554

packages/cherry-markdown/src/Engine.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import NestedError, { $expectTarget, $expectInherit, $expectInstance } from './u
1919
import CryptoJS from 'crypto-js';
2020
import SyntaxBase from './core/SyntaxBase';
2121
import ParagraphBase from './core/ParagraphBase';
22-
import { PUNCTUATION, longTextReg, imgBase64Reg, imgDrawioXmlReg } from './utils/regexp';
22+
import { PUNCTUATION, longTextReg, imgBase64Reg, imgDrawioXmlReg, base64Reg } from './utils/regexp';
2323
import { escapeHTMLSpecialChar } from './utils/sanitize';
2424
import Logger from './Logger';
2525
import { configureMathJax } from './utils/mathjax';
@@ -343,7 +343,12 @@ export default class Engine {
343343

344344
// 缓存大文本数据,用以提升渲染性能
345345
$cacheBigData(md) {
346-
let $md = md.replace(imgBase64Reg, (whole, m1, m2) => {
346+
let $md = md.replace(base64Reg, (dataUri) => {
347+
const cacheKey = `data:cherry/cache;sha256,${this.hash(dataUri)}`;
348+
this.cachedBigData[cacheKey] = dataUri;
349+
return cacheKey;
350+
});
351+
$md = $md.replace(imgBase64Reg, (whole, m1, m2) => {
347352
const cacheKey = `bigDataBegin${this.hash(m2)}bigDataEnd`;
348353
this.cachedBigData[cacheKey] = m2;
349354
return `${m1}${cacheKey})`;
@@ -361,10 +366,17 @@ export default class Engine {
361366
return $md;
362367
}
363368

369+
/**
370+
* @param {string} md
371+
*/
364372
$deCacheBigData(md) {
365-
return md.replace(/bigDataBegin[^\n]+?bigDataEnd/g, (whole) => {
366-
return this.cachedBigData[whole];
367-
});
373+
return md
374+
.replace(/data:cherry\/cache;sha256,[0-9a-f]+/g, (cacheUri) => {
375+
return this.cachedBigData[cacheUri];
376+
})
377+
.replace(/bigDataBegin[^\n]+?bigDataEnd/g, (whole) => {
378+
return this.cachedBigData[whole];
379+
});
368380
}
369381

370382
/**

packages/cherry-markdown/src/UrlCache.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ export default class UrlCache {
8484
const cherryInnerLinkRegex = /cherry-inner:\/\/([0-9a-f]+)/gi;
8585
const $html = html.replace(cherryInnerLinkRegex, (match) => {
8686
const originalUrl = UrlCache.get(match);
87-
return originalUrl || match;
87+
return typeof originalUrl === 'string' ? originalUrl : match;
8888
});
8989
return $html;
9090
}

packages/cherry-markdown/src/core/hooks/CommentReference.js

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,38 @@ export default class CommentReference extends ParagraphBase {
3838
this.commentCache = {};
3939
}
4040

41+
/**
42+
* 去除包裹 url 的尖括号
43+
* @param {string} wrappedUrl
44+
*/
45+
unwrapUrl(wrappedUrl) {
46+
const url = wrappedUrl.trim();
47+
// 原始的尖括号
48+
if (url.startsWith('<') && url.endsWith('>')) {
49+
return url.slice(1, -1);
50+
}
51+
// 被转义保护的尖括号
52+
if (url.startsWith('&#60;') && url.endsWith('&#62;')) {
53+
return url.slice(5, -5);
54+
}
55+
return url;
56+
}
57+
4158
pushCommentReferenceCache(key, cache) {
4259
const [url, ...args] = cache.split(/[ ]+/g);
43-
const innerUrl = UrlCache.set(url);
60+
const innerUrl = UrlCache.set(this.unwrapUrl(url));
4461
this.commentCache[`${key}`.toLowerCase()] = [innerUrl, ...args].join(' ');
4562
}
4663

4764
getCommentReferenceCache(key) {
4865
return this.commentCache[`${key}`.toLowerCase()] || null;
4966
}
5067

68+
/**
69+
*
70+
* @param {string} str
71+
* @returns
72+
*/
5173
beforeMakeHtml(str) {
5274
let $str = str;
5375
if (this.test($str)) {
@@ -57,7 +79,7 @@ export default class CommentReference extends ParagraphBase {
5779
return lineFeeds.join('');
5880
});
5981
// 替换实际引用
60-
const refRegex = /(\[[^\]\n]+?\])?(?:\[([^\]\n]+?)\])/g; // 匹配[xxx][ref]形式的内容,不严格大小写
82+
const refRegex = /(\[[^\]]*?\])?(?:\[([^\]\n]+?)\])/g; // 匹配[xxx][ref]形式的内容,不严格大小写
6183
$str = $str.replace(refRegex, (match, leadingContent, key) => {
6284
const cache = this.getCommentReferenceCache(key);
6385
if (cache) {

packages/cherry-markdown/src/toolbars/PreviewerBubble.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,11 +246,11 @@ export default class PreviewerBubble {
246246
let targetCh = -1;
247247
contents.forEach((lineContent, lineIdx) => {
248248
const tmp = lineContent.trim(); // 去掉句首的空格和制表符
249-
if (tmp.startsWith('- [ ]') || tmp.startsWith('- [x]')) {
249+
if (/^-\s+\[[ x]\]/i.test(tmp)) {
250250
// 如果是个checkbox
251251
if (editorCheckboxCount === this.checkboxIdx) {
252252
targetLine = lineIdx;
253-
targetCh = lineContent.indexOf('- [') + 3;
253+
targetCh = lineContent.indexOf('[') + 1;
254254
}
255255
editorCheckboxCount += 1;
256256
}

packages/cherry-markdown/src/utils/regexp.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ export function getDetailRule() {
263263
export const imgBase64Reg = /(\[[^\n]*?\]\(data:image\/[a-z]{1,10};base64,)([^)]+)\)/g;
264264

265265
// 匹配base64数据
266-
export const base64Reg = /(data:image\/[a-z]{1,10};base64,)([0-9a-zA-Z+/]+)/g;
266+
export const base64Reg = /(data:image\/[a-z]{1,10};base64,)([0-9a-zA-Z+/=]+)/g;
267267

268268
// 匹配内容非常多的单行文本,为了避免表格的场景,所以特意避免表格的识别
269269
export const longTextReg = /([^\n]{100})([^\n|`\s]{5900,})/g;

packages/cherry-markdown/test/core/__snapshots__/CommonMark.spec.ts.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1360,7 +1360,7 @@ exports[`engine > CommonMark-192 1`] = `
13601360
`;
13611361

13621362
exports[`engine > CommonMark-193 1`] = `
1363-
"<p data-sign="c748929db636b086c31f362ad2f999f6257bb59351aa714a748ec92ec7769b383" data-type="p" data-lines="3"> /url <br> 'the title' </p><p data-sign="3c65f714b4d72f614794b9c1dc97a9cb988e5bd7a6981a79e63700f04f901b8b2" data-type="p" data-lines="2">[foo](cherry-inner://e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 )</p>
1363+
"<p data-sign="c748929db636b086c31f362ad2f999f6257bb59351aa714a748ec92ec7769b383" data-type="p" data-lines="3"> /url <br> 'the title' </p><p data-sign="3c65f714b4d72f614794b9c1dc97a9cb988e5bd7a6981a79e63700f04f901b8b2" data-type="p" data-lines="2">[foo]( )</p>
13641364
"
13651365
`;
13661366

@@ -1395,7 +1395,7 @@ exports[`engine > CommonMark-199 1`] = `
13951395
`;
13961396

13971397
exports[`engine > CommonMark-200 1`] = `
1398-
"<p data-sign="901a805d3b4da566f815e5f755cc0781c4da142101080d8d5db1f7fb9ef0971b1" data-type="p" data-lines="1"><a href="&#60;&#62;">foo</a></p>
1398+
"<p data-sign="710f04ae6827e964349ae1ce3cc82a5088ee94ab7a10d615a82f3ac2c94631a21" data-type="p" data-lines="1"><a href="">foo</a></p>
13991399
"
14001400
`;
14011401

0 commit comments

Comments
 (0)