我决定爱上 Stack Overflow 上的 Markdown 编辑器。它是来自 John Fraser 的 showdown.js
的一个分支。我想在我的一个项目中使用这个解析器,但是在分析源代码后,我发现它有点乱,不合我的口味。
所以我着手修改JavaScript代码以满足我的需要,即:
- 摆脱所有的全局变量,
- 将变量声明组合到每个范围的单个
var
, - 将串联更改为数组连接,
- 旨在使缩小的源代码更小的各种其他调整。
除一个小问题外,我的一切都运行良好:command.doList
函数中的自动完成代码在 Chrome 中运行不正常。我已经测试了在 Internet Explorer、FireFox 和 Safari 中正常工作的所有内容。我已将问题归结为以下几行:
// Get the item prefix - e.g. " 1. " for a numbered list, " - " for a bulleted
// list.
getItemPrefix = function () {
var prefix;
if (isNumberedList) {
// the `s` variable is just a string space.
prefix = [s, num, '. '].join('');
num++;
} else {
prefix = [s, bullet, s].join('');
}
return prefix;
};
// Fixes the prefixes of the other list items.
getPrefixedItem = function (itemText) {
// The numbering flag is unset when called by autoindent.
if (isNumberedList === undefined) {
isNumberedList = /^\s*\d/.test(itemText);
}
// Renumber/bullet the list element.
// THE FOLLOWING LINES COMMENTED OUT TO FIX A BUG.
//itemText = itemText.replace(/^[ ]{0,3}([\*\+\-]|\d+[.])\s/gm,
// function () { return getItemPrefix(); });
return itemText;
};
靠近末尾的两条注释掉的行阻止 Chrome 表现不稳定,但编号列表不再自动递增。一旦我取消注释这些行,所有主要浏览器都会开始正常工作,Chrome 除外。 (Chrome 变得疯狂并使用额外的编号和项目符号自动完成列表,在尝试使用无序列表时也会发生这种情况。)
在 Stack Overflow 上这里显然不存在这个问题,所以我希望这只是我的一个愚蠢的疏忽。 (要么是那个,要么是 Stack Overflow 已经更新并修复了这个错误。)
最佳答案
你可以看看fix用于另一个 WMD 分支(第 32 行)。
关于javascript - 这不会经常发生 : Why is this JavaScript code only broken in Chrome?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4321325/