Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix : 新增通过12个CommonMark测试用例;撰写竞品文档;基于fireHookAction设计异步 #896

Open
wants to merge 10 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
371 changes: 371 additions & 0 deletions MiddleTask.md

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/cherry-markdown.css
Git LFS file not shown
4 changes: 2 additions & 2 deletions dist/cherry-markdown.js
Git LFS file not shown
4 changes: 2 additions & 2 deletions dist/cherry-markdown.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/fonts/ch-icon.eot
Git LFS file not shown
2 changes: 1 addition & 1 deletion dist/fonts/ch-icon.ttf
Git LFS file not shown
2 changes: 1 addition & 1 deletion dist/fonts/ch-icon.woff
Git LFS file not shown
2 changes: 1 addition & 1 deletion dist/fonts/ch-icon.woff2
Git LFS file not shown
21 changes: 21 additions & 0 deletions primaryTask.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

原来的测试结果:
Snapshot Summary
› 5 snapshots failed from 1 test suite. Inspect your code changes or run `yarn test -u` to update them.

Test Suites: 5 failed, 2 passed, 7 total
Tests: 442 failed, 213 passed, 655 total
Snapshots: 5 failed, 5 total
Time: 10.266 s, estimated 16 s
现在的测试结果

Snapshot Summary
› 5 snapshots failed from 1 test suite. Inspect your code changes or run `yarn test -u` to update them.

Test Suites: 5 failed, 2 passed, 7 total
Tests: 430 failed, 225 passed, 655 total
Snapshots: 5 failed, 5 total
Time: 11.297 s
Ran all test suites.

从一开始的完全看不明白,到逐渐理解。终于坚持了下来,最终完成12个测试用例的通过,超额完成任务hhh
45 changes: 44 additions & 1 deletion src/Engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,41 @@ export default class Engine {
}
return $md;
}
/** 在Engine类中添加一个新的异步方法$fireAsyncHookAction。这个方法的主要目的是并行处理所有异步hook,从而提高性能
保持兼容性:新方法的设计保持了与原有$fireHookAction方法相似的接口,这样可以最小化对现有代码的修改,保持向后兼容性
异步处理:使用Promise.all来并行执行所有异步hook。这样可以显著提高处理大量数据或复杂操作时的性能
错误处理:使用try-catch块来捕获并处理可能出现的错误,确保异步操作的稳定性
*/
async $fireAsyncHookAction(md, type, action, actionArgs) {
let $md = md;
if (!this.hooks || !this.hooks[type]) {
return $md;
}

const hooks = this.hooks[type];
const asyncHooks = hooks.filter((hook) => hook[action] && hook[action].constructor.name === 'AsyncFunction');

try {
const results = await Promise.all(
asyncHooks.map(async (hook) => {
if (!hook.$engine) {
hook.$engine = this;
}
return await hook[action]($md, actionArgs, this.markdownParams);
}),
);

results.forEach((result) => {
if (result) {
$md = result;
}
});
} catch (e) {
throw new NestedError(e);
}

return $md;
}
md5(str) {
if (!this.md5StrMap[str]) {
this.md5StrMap[str] = md5(str);
Expand Down Expand Up @@ -261,7 +295,16 @@ export default class Engine {
$md = this.$deCacheBigData($md);
return $md;
}

/** 使得makehtml成为异步方法,只对makeHtml方法中的$dealParagraph调用进行修改,使用新的$fireAsyncHookAction方法。这样可以保持其他部分的代码不变,减少潜在的bug
* async makeHtml(md) {
let $md = this.$cacheBigData(md);
$md = this.$beforeMakeHtml($md);
$md = await this.$fireAsyncHookAction($md, 'paragraph', 'makeHtml', this.$dealSentenceByCache.bind(this));
$md = this.$afterMakeHtml($md);
$md = this.$deCacheBigData($md);
return $md;
}
*/
mounted() {
this.$fireHookAction('', 'sentence', 'mounted');
this.$fireHookAction('', 'paragraph', 'mounted');
Expand Down
21 changes: 20 additions & 1 deletion src/core/ParagraphBase.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,26 @@ export default class ParagraphBase extends SyntaxBase {
}

toHtml(str, sentenceMakeFunc) {
return str;
let result = str;

// 处理标题
result = result.replace(/^(#{1,6})\s+(.+)$/gm, (match, hashes, content) => {
const level = hashes.length;
return `<h${level}>${sentenceMakeFunc(content).html}</h${level}>`;
});
// 处理无序列表
result = result.replace(/^(\s*)-\s+(.+)$/gm, (match, indent, content) => {
return `<ul><li>${sentenceMakeFunc(content).html}</li></ul>`;
});
// 处理有序列表
result = result.replace(/^(\s*)\d+\.\s+(.+)$/gm, (match, indent, content) => {
return `<ol><li>${sentenceMakeFunc(content).html}</li></ol>`;
});
// 处理引用块
result = result.replace(/^>\s+(.+)$/gm, (match, content) => {
return `<blockquote>${sentenceMakeFunc(content).html}</blockquote>`;
});
return result;
}

makeHtml(str, sentenceMakeFunc) {
Expand Down
23 changes: 4 additions & 19 deletions src/core/hooks/AutoLink.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,27 +179,12 @@ export default class AutoLink extends SyntaxBase {
return match;
});
}

// 将正则表达式更改为不匹配没有任何换行字符的裸 URL
rule() {
// (?<protocol>\\w+:)\\/\\/
const ret = {
// ?<left>
begin: '(<?)',
content: [
// ?<protocol>
'((?:[a-z][a-z0-9+.-]{1,31}:)?)', // protocol is any seq of 2-32 chars beginning with letter
// '(?<slash>(?:\\/{2})?)',
// ?<address>
// '([^\\s\\x00-\\x1f"<>]+)',
`((?:${URL_INLINE.source})|(?:${EMAIL_INLINE.source}))`,
// [
// `(?<url>${ URL_INLINE.source })`,
// `(?<email>${ EMAIL_INLINE.source })`, // email
// ].join('|'),
// ')'
].join(''),
// ?<right>
end: '(>?)', // TODO: extend attrs e.g. {target=_blank}
begin: '(<)',
content: ['((?:[a-z][a-z0-9+.-]{1,31}:)?)', `((?:${URL_INLINE.source})|(?:${EMAIL_INLINE.source}))`].join(''),
end: '(>)',
};
ret.reg = compileRegExp(ret, 'ig');
return ret;
Expand Down
2 changes: 1 addition & 1 deletion src/core/hooks/CodeBlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export default class CodeBlock extends ParagraphBase {
this.selfClosing = config.selfClosing; // 自动闭合,为true时,当md中有奇数个```时,会自动在md末尾追加一个```
this.mermaid = config.mermaid; // mermaid的配置,目前仅支持格式设置,svg2img=true 展示成图片,false 展示成svg
this.indentedCodeBlock = typeof config.indentedCodeBlock === 'undefined' ? true : config.indentedCodeBlock; // 是否支持缩进代码块
this.INLINE_CODE_REGEX = /(`+)(.+?(?:\n.+?)*?)\1/g;
this.INLINE_CODE_REGEX = /(`+)[ ]?([ ]*.+?(`*)(?:[^`\n]+?)*?)[ ]?\1/g;
if (config && config.customRenderer) {
this.customLang = Object.keys(config.customRenderer).map((lang) => lang.toLowerCase());
this.customParser = { ...config.customRenderer };
Expand Down
10 changes: 5 additions & 5 deletions src/core/hooks/Header.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,13 +207,13 @@ export default class Header extends ParagraphBase {
// setext Header
// TODO: 支持多行标题
const setext = {
begin: '(?:^|\\n)(\\n*)', // (?<lines>\\n*)
begin: '(?:^|\\n)(\\n*)',
content: [
'(?:\\h*',
'(.+)', // (?<text>.+)
'(.+?)', // 使用非贪婪匹配
')\\n',
'(?:\\h*',
'([=]+|[-]+)', // (?<level>[=]+|[-]+)
'([=]+|[-]+)\\s*', // 允许结尾有空白字符
')',
].join(''),
end: '(?=$|\\n)',
Expand All @@ -222,8 +222,8 @@ export default class Header extends ParagraphBase {

// atx header
const atx = {
begin: '(?:^|\\n)(\\n*)(?:\\h*(#{1,6}))', // (?<lines>\\n*), (?<level>#{1,6})
content: '(.+?)', // '(?<text>.+?)'
begin: '(?:^|\\n)(\\n*)(?:\\h{0,3}(#{1,6})\\s+)', // 允许最多3个空格缩进
content: '(.+?)(?:\\s+#+\\s*)?', // 允许可选的结束#
end: '(?=$|\\n)',
};
this.strict && (atx.begin += '(?=\\h+)'); // (?=\\s+) for strict mode
Expand Down
6 changes: 5 additions & 1 deletion src/core/hooks/InlineCode.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ export default class InlineCode extends ParagraphBase {
}

rule() {
const ret = { begin: '(`+)[ ]*', end: '[ ]*\\1', content: '(.+?(?:\\n.+?)*?)' };
const ret = {
begin: '(?<![`\\\\]|^`)(`+)(?!`)',
content: '([^`]|(?<!^)`(?!\\2))*?',
end: '(?<!`|\\\\)\\2(?!`)',
};
ret.reg = compileRegExp(ret, 'g');
return ret;
}
Expand Down
Loading
Loading