Skip to content

Commit

Permalink
fix(list): fix magic number
Browse files Browse the repository at this point in the history
  • Loading branch information
jiawei686 authored and humyfred committed Dec 30, 2021
1 parent f4d6a2a commit 34224a8
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/Cherry.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ const defaultConfig = {
// }
list: {
listNested: false, // 同级列表类型转换后变为子级
intentSpace: 2, // 默认2个空格缩进
indentSpace: 2, // 默认2个空格缩进
},
table: {
enableChart: false,
Expand Down
11 changes: 7 additions & 4 deletions src/core/hooks/List.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
*/
import ParagraphBase from '@/core/ParagraphBase';

const INDENT_SPACE_NUM = 4; // commonmark default use 1~4 spaces for indent
const TAB_SPACE_NUM = 4; // 1 tab === 4 space

function attrsToAttributeString(object) {
if (typeof object !== 'object' && Object.keys(object).length < 1) {
return '';
Expand All @@ -40,7 +43,7 @@ function handleIndent(str, node) {
const indentRegex = /^(\t|[ ])/;
let $str = str;
while (indentRegex.test($str)) {
node.space += $str[0] === '\t' ? 4 : 1;
node.space += $str[0] === '\t' ? TAB_SPACE_NUM : 1;
$str = $str.replace(indentRegex, '');
}
return $str;
Expand Down Expand Up @@ -101,9 +104,9 @@ export default class List extends ParagraphBase {
constructor({ config }) {
super({ needCache: true });
this.config = config || {};
this.intentSpace = this.config.intentSpace > 0 ? this.config.intentSpace : 2;
this.tree = [];
this.emptyLines = 0;
this.indentSpace = Math.max(this.config.indentSpace, 2);
}

addNode(node, current, parent, last) {
Expand Down Expand Up @@ -143,14 +146,14 @@ export default class List extends ParagraphBase {
} else {
const { space } = node;
const lastSpace = this.tree[last].space;
if (space < lastSpace + this.intentSpace) {
if (space < lastSpace + this.indentSpace) {
// 成为同级节点
if (this.config.listNested && this.tree[last].type !== node.type) {
this.addNode(node, i, last);
} else {
this.addNode(node, i, this.tree[last].parent);
}
} else if (space < lastSpace + this.intentSpace + 4) {
} else if (space < lastSpace + this.indentSpace + INDENT_SPACE_NUM) {
// 成为子节点
this.addNode(node, i, last);
} else {
Expand Down

0 comments on commit 34224a8

Please sign in to comment.