Skip to content

Commit

Permalink
fix: 代码块逆解析成代码块语法时有多余的中括号产生 Tencent#414
Browse files Browse the repository at this point in the history
  • Loading branch information
sunsonliu committed Mar 23, 2023
1 parent bac8173 commit 76acd94
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions src/utils/htmlparser.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ const htmlParser = {
const temObj = arr[i];
if (temObj.type === 'tag') ret = this.$handleTagObject(temObj, ret);
else if (temObj.type === 'text' && temObj.content.length > 0) {
ret += temObj.content.replace(/ /g, ' ').replace(/[\n]+/g, '\n');
ret += temObj.content
.replace(/ /g, ' ')
.replace(/[\n]+/g, '\n')
.replace(/^[ \t\n]+$/, '\n');
}
}
return ret;
Expand All @@ -69,7 +72,7 @@ const htmlParser = {
*/
$handleTagObject(temObj, returnString) {
let ret = returnString;
if (temObj.attrs.class && temObj.attrs.class.indexOf('ch-icon') >= 0) {
if (temObj.attrs.class && /(ch-icon-square|ch-icon-check)/.test(temObj.attrs.class)) {
// 针对checklist
if (temObj.attrs.class.indexOf('ch-icon-check') >= 0) {
ret += '[x]';
Expand Down Expand Up @@ -100,7 +103,8 @@ const htmlParser = {
}
if (obj.name === 'code' || obj.name === 'pre') {
// 解析代码块 或 行内代码
return self.tagParser.codeParser(obj, self.$dealCodeTag(obj));
// pre时,强制转成代码块
return self.tagParser.codeParser(obj, self.$dealCodeTag(obj), obj.name === 'pre');
}
if (typeof self.tagParser[`${obj.name}Parser`] === 'function') {
// 解析对应的具体标签
Expand Down Expand Up @@ -325,10 +329,11 @@ const htmlParser = {
* 解析code标签
* @param {HTMLElement} obj
* @param {string} str 需要回填的字符串
* @param {boolean} isBlock 是否强制为代码块
* @returns {string} str
*/
codeParser(obj, str) {
return this.formatEngine.convertCode(str);
codeParser(obj, str, isBlock = false) {
return this.formatEngine.convertCode(str, isBlock);
},
/**
* 解析br标签
Expand Down Expand Up @@ -723,11 +728,11 @@ const htmlParser = {
convertBr(str, attr) {
return str + attr;
},
convertCode(str) {
if (/\n/.test(str)) {
convertCode(str, isBlock = false) {
if (/\n/.test(str) || isBlock) {
return `\`\`\`\n${str.replace(/\n+$/, '')}\n\`\`\``;
}
return ` \`${str.replace(/`/g, '\\`')}\` `;
return `\`${str.replace(/`/g, '\\`')}\``;
},
convertB(str) {
return /^\s*$/.test(str) ? '' : `**${str}**`;
Expand Down

0 comments on commit 76acd94

Please sign in to comment.