From a75f59392241b55e94c9d1a61579341033b3895a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EC=86=8C=ED=9D=AC?= <42666492+sohee-lee7@users.noreply.github.com> Date: Fri, 21 Jun 2019 15:35:10 +0900 Subject: [PATCH] fix: data-tomark-pass appear when bracket is used for text (fix: #551) (#555) * fix: data-tomark-pass appear when bracket is used for text (fix: #551) * refactor: reflect code review --- src/js/convertor.js | 25 +++++++++++++++++++------ test/unit/convertor.spec.js | 16 ++++++++++++++++ 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/src/js/convertor.js b/src/js/convertor.js index 32cea6ff15..aee8639e1b 100644 --- a/src/js/convertor.js +++ b/src/js/convertor.js @@ -64,6 +64,17 @@ markdownit.inline.ruler.at('backticks', codeBackticks); markdownit.use(taskList); markdownit.use(codeBlock); +// This regular expression refere markdownIt. +// https://github.com/markdown-it/markdown-it/blob/master/lib/common/html_re.js +const attrName = '[a-zA-Z_:][a-zA-Z0-9:._-]*'; +const unquoted = '[^"\'=<>`\\x00-\\x20]+'; +const singleQuoted = "'[^']*'"; +const doubleQuoted = '"[^"]*"'; +const attrValue = `(?:${unquoted}|${singleQuoted}|${doubleQuoted})`; +const attribute = `(?:\\s+${attrName}(?:\\s*=\\s*${attrValue})?)*\\s*`; +const openingTag = `(\\\\<|<)([A-Za-z][A-Za-z0-9\\-]*${attribute})(\\/?>)`; +const HTML_TAG_RX = new RegExp(openingTag, 'g'); + /** * Class Convertor */ @@ -105,8 +116,10 @@ class Convertor { * @returns {string} html text */ _markdownToHtml(markdown, env) { - // should insert data-tomark-pass in the opening tag - markdown = markdown.replace(/<(?!\/)([^>]+)([/]?)>/g, '<$1 data-tomark-pass $2>'); + markdown = markdown.replace(HTML_TAG_RX, (match, $1, $2, $3) => { + return match[0] !== '\\' ? `${$1}${$2} data-tomark-pass ${$3}` : match; + }); + // eslint-disable-next-line const onerrorStripeRegex = /(]*)(onerror\s*=\s*[\"']?[^\"']*[\"']?)(.*)/i; while (onerrorStripeRegex.exec(markdown)) { @@ -176,13 +189,13 @@ class Convertor { /** * set link attribute to markdownitHighlight, markdownit * using linkAttribute of markdownItInlinePlugin - * @param {object} attribute markdown text + * @param {object} attr markdown text */ - setLinkAttribute(attribute) { - const keys = Object.keys(attribute); + setLinkAttribute(attr) { + const keys = Object.keys(attr); const setAttributeToToken = (tokens, idx) => { keys.forEach(key => { - tokens[idx].attrPush([key, attribute[key]]); + tokens[idx].attrPush([key, attr[key]]); }); }; diff --git a/test/unit/convertor.spec.js b/test/unit/convertor.spec.js index 403a8d4c6a..66cd19cb2f 100644 --- a/test/unit/convertor.spec.js +++ b/test/unit/convertor.spec.js @@ -330,4 +330,20 @@ describe('Convertor', () => { expect(convertor.toMarkdown(html)).toEqual(markdown); }); }); + + describe('should not insert data-tomark-pass', () => { + it('when <> include korean', () => { + const markdown = ''; + const html = '

<AS 안내>

'; + + expect(convertor.toHTML(markdown).replace('\n', '')).toEqual(html); + }); + + it('when < start with backslash', () => { + const markdown = '\\'; + const html = '

<AS>

'; + + expect(convertor.toHTML(markdown).replace('\n', '')).toEqual(html); + }); + }); });