Browse Source

Perf: regex opts

pull/14/head
Vitaly Puzrin 10 years ago
parent
commit
bc7b8e2f89
  1. 2
      README.md
  2. 21
      lib/common/utils.js
  3. 13
      lib/parser_block.js

2
README.md

@ -88,7 +88,7 @@ var md = new Remarkable('full');
By default remarkable is configured to be similar to GFM, but with disabled HTML. By default remarkable is configured to be similar to GFM, but with disabled HTML.
### Highligh fenced blocks ### Highlight fenced blocks
To highlight content of fenced block, assing function to `highlight` option: To highlight content of fenced block, assing function to `highlight` option:

21
lib/common/utils.js

@ -30,18 +30,27 @@ function assign(obj /*from1, from2, from3, ...*/) {
} }
var ESCAPE_TEST_RE = /[&<>"]/; var HTML_ESCAPE_TEST_RE = /[&<>"]/;
var HTML_ESCAPE_REPLACE_RE = /[&<>"]/g;
var HTML_REPLACEMENTS = {
'&': '&amp;',
'<': '&lt;',
'>': '&gt;',
'"': '&quot;'
};
function replaceUnsafeChar(ch) {
return HTML_REPLACEMENTS[ch];
}
function escapeHtml(str) { function escapeHtml(str) {
if (ESCAPE_TEST_RE.test(str)) { if (HTML_ESCAPE_TEST_RE.test(str)) {
if (str.indexOf('&') >= 0) { str = str.replace(/&/g, '&amp;'); } return str.replace(HTML_ESCAPE_REPLACE_RE, replaceUnsafeChar);
if (str.indexOf('<') >= 0) { str = str.replace(/</g, '&lt;'); }
if (str.indexOf('>') >= 0) { str = str.replace(/>/g, '&gt;'); }
if (str.indexOf('"') >= 0) { str = str.replace(/"/g, '&quot;'); }
} }
return str; return str;
} }
var UNESCAPE_MD_RE = /\\([\\!"#$%&'()*+,.\/:;<=>?@[\]^_`{|}~-])/g; var UNESCAPE_MD_RE = /\\([\\!"#$%&'()*+,.\/:;<=>?@[\]^_`{|}~-])/g;
function unescapeMd(str) { function unescapeMd(str) {

13
lib/parser_block.js

@ -104,23 +104,24 @@ ParserBlock.prototype.tokenize = function (state, startLine, endLine) {
} }
}; };
var TABS_SCAN_RE = /[\n\t]/g;
var NEWLINES_RE = /\r[\n\u0085]|[\u2424\u2028\u0085]/g;
var SPACES_RE = /\u00a0/g;
ParserBlock.prototype.parse = function (src, options, env) { ParserBlock.prototype.parse = function (src, options, env) {
var state, lineStart = 0, lastTabPos = 0; var state, lineStart = 0, lastTabPos = 0;
if (!src) { return ''; } if (!src) { return []; }
// Normalize spaces // Normalize spaces
src = src.replace(/\u00a0/g, ' '); src = src.replace(SPACES_RE, ' ');
// Normalize newlines // Normalize newlines
src = src.replace(/\r\n/, '\n'); src = src.replace(NEWLINES_RE, '\n');
src = src.replace(/\r\u0085/, '\n');
src = src.replace(/[\u2424\u2028\u0085]/g, '\n');
// Replace tabs with proper number of spaces (1..4) // Replace tabs with proper number of spaces (1..4)
if (src.indexOf('\t') >= 0) { if (src.indexOf('\t') >= 0) {
src = src.replace(/[\n\t]/g, function (match, offset) { src = src.replace(TABS_SCAN_RE, function (match, offset) {
var result; var result;
if (src.charCodeAt(offset) === 0x0A) { if (src.charCodeAt(offset) === 0x0A) {
lineStart = offset + 1; lineStart = offset + 1;

Loading…
Cancel
Save