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

add pre 增加pre语法(四个空格变成代码块) #137

Merged
merged 3 commits into from
Feb 14, 2022
Merged
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
12 changes: 12 additions & 0 deletions src/Cherry.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,18 @@ const defaultConfig = {
customRenderer: {
// 自定义语法渲染器
},
/**
* indentedCodeBlock是缩进代码块是否启用的开关
*
* 在6.X之前的版本中默认不支持该语法。
* 因为cherry的开发团队认为该语法太丑了(容易误触)
* 开发团队希望用```代码块语法来彻底取代该语法
* 但在后续的沟通中,开发团队发现在某些场景下该语法有更好的显示效果
* 因此开发团队在6.X版本中才引入了该语法
* 已经引用6.x以下版本的业务如果想做到用户无感知升级,可以去掉该语法:
* indentedCodeBlock:false
*/
indentedCodeBlock: true,
},
emoji: {
useUnicode: true, // 是否使用unicode进行渲染
Expand Down
1 change: 1 addition & 0 deletions src/Editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export default class Editor {
lineNumbers: false, // 显示行数
cursorHeight: 0.85, // 光标高度,0.85好看一些
indentUnit: 4, // 缩进单位为4
tabSize: 4, // 一个tab转换成的空格数量
// styleActiveLine: false, // 当前行背景高亮
// matchBrackets: true, // 括号匹配
mode: 'markdown',
Expand Down
62 changes: 62 additions & 0 deletions src/core/hooks/CodeBlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export default class CodeBlock extends ParagraphBase {
this.customParser = {};
this.wrap = config.wrap; // 超出是否换行
this.lineNumber = config.lineNumber; // 是否显示行号
this.indentedCodeBlock = typeof config.indentedCodeBlock === 'undefined' ? true : config.indentedCodeBlock; // 是否支持缩进代码块
if (config && config.customRenderer) {
this.customLang = Object.keys(config.customRenderer).map((lang) => lang.toLowerCase());
this.customParser = { ...config.customRenderer };
Expand Down Expand Up @@ -196,9 +197,66 @@ export default class CodeBlock extends ParagraphBase {
return cacheCode;
}

/**
* 获取缩进代码块语法的正则
*/
$getIndentedCodeReg() {
const ret = {
begin: '(?:^|\\n\\s*\\n)(?: {4}|\\t)',
end: '(?=$|\\n( {0,3}[^ \\t\\n]|\\n[^ \\t\\n]))',
content: '([\\s\\S]+?)',
};
return new RegExp(ret.begin + ret.content + ret.end, 'g');
}

/**
* 生成缩进代码块(没有行号、没有代码高亮)
*/
$getIndentCodeBlock(str) {
if (!this.indentedCodeBlock) {
return str;
}
return this.$recoverCodeInIndent(str).replace(this.$getIndentedCodeReg(), (match, code) => {
const lineCount = (match.match(/\n/g) || []).length - 1;
const sign = this.$engine.md5(match);
const html = `<pre data-sign="${sign}" data-lines="${lineCount}">${escapeHTMLSpecialChar(
code.replace(/\n( {4}|\t)/g, '\n'),
)}</pre>`;
// return this.getCacheWithSpace(this.pushCache(html), match, true);
return this.pushCache(html, sign, lineCount);
});
}

/**
* 预处理缩进代码块,将缩进代码块里的高亮代码块和行内代码进行占位处理
*/
$replaceCodeInIndent(str) {
if (!this.indentedCodeBlock) {
return str;
}
return str.replace(this.$getIndentedCodeReg(), (match) => {
return match.replace(/`/g, '~~~IndentCode');
});
}

/**
* 恢复预处理的内容
*/
$recoverCodeInIndent(str) {
if (!this.indentedCodeBlock) {
return str;
}
return str.replace(this.$getIndentedCodeReg(), (match) => {
return match.replace(/~~~IndentCode/g, '`');
});
}

beforeMakeHtml(str, sentenceMakeFunc, markdownParams) {
let $str = str;

// 预处理缩进代码块
$str = this.$replaceCodeInIndent($str);

$str = $str.replace(this.RULE.reg, (match, leadingContent, lang, code) => {
let $code = code;
const { sign, lines } = this.computeLines(match, leadingContent, code);
Expand Down Expand Up @@ -262,6 +320,10 @@ export default class CodeBlock extends ParagraphBase {
});
$str = $str.replace(/~~not~inlineCode/g, '\\`');
}

// 处理缩进代码块
$str = this.$getIndentCodeBlock($str);

return $str;
}

Expand Down
2 changes: 1 addition & 1 deletion src/sass/cherry.scss
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@
font-size: 0.9em;
}

.cm-s-default .cm-whitespace {
.cm-s-default .cm-whitespace, .cm-tab {
font-family: $monospaceFont;
font-size: 0.9em;
}
Expand Down
9 changes: 9 additions & 0 deletions src/sass/markdown.scss
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,15 @@
margin: 0 0 $elementGap;
}

pre {
padding: 16px;
overflow: auto;
font-size: 85%;
line-height: 1.45;
background-color: #f6f8fa;
border-radius: 6px;
}

// 适配历史文章样式
@import 'prettyprint/prettyprint.scss';

Expand Down