Skip to content

Commit

Permalink
Merge pull request #31 from nhn/fix/escape-all-emphasis-characters
Browse files Browse the repository at this point in the history
fix: change logic to escape emphasis characters (fix #30)
  • Loading branch information
seonim-ryu committed Feb 5, 2020
1 parent 2b4f573 commit fc00ffa
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 32 deletions.
7 changes: 4 additions & 3 deletions libs/to-mark/src/renderer.basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@ var FIND_LAST_RETURN_RX = /\n$/g,
var basicRenderer = Renderer.factory({
//inlines
'TEXT_NODE': function(node) {
var managedText;

managedText = this.trim(this.getSpaceCollapsedText(node.nodeValue));
var managedText = this.trim(this.getSpaceCollapsedText(node.nodeValue));

if (this._isNeedEscapeBackSlash(managedText)) {
managedText = this.escapeTextBackSlash(managedText);
}

managedText = this.escapeEmphasisCharacters(managedText);

if (this._isNeedEscapeHtml(managedText)) {
managedText = this.escapeTextHtml(managedText);
}
Expand Down
22 changes: 14 additions & 8 deletions libs/to-mark/src/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -328,11 +328,9 @@ Renderer.prototype.escapeTextForLink = function(text) {
* @returns {string} processed text
*/
Renderer.prototype.escapeTextHtml = function(text) {
text = text.replace(Renderer.markdownTextToEscapeHtmlRx, function(matched) {
return text.replace(Renderer.markdownTextToEscapeHtmlRx, function(matched) {
return '\\' + matched;
});

return text;
};

/**
Expand All @@ -344,11 +342,20 @@ Renderer.prototype.escapeTextHtml = function(text) {
* @returns {string} processed text
*/
Renderer.prototype.escapeTextBackSlash = function(text) {
text = text.replace(Renderer.markdownTextToEscapeBackSlashRx, function(matched) {
return text.replace(Renderer.markdownTextToEscapeBackSlashRx, function(matched) {
return '\\' + matched;
});
};

return text;
/**
* Escapes in markdown emphasis characters ('*', '_', '~')
* @param {string} text Text to escape
* @returns {string} escaped text
*/
Renderer.prototype.escapeEmphasisCharacters = function(text) {
return text.replace(Renderer.markdownTextToEscapeEmphasisCharsRx, function(matched) {
return '\\' + matched;
});
};

Renderer.markdownTextToEscapeRx = {
Expand All @@ -362,9 +369,6 @@ Renderer.markdownTextToEscapeRx = {

link: /!?\[.*\]\(.*\)/,
reflink: /!?\[.*\]\s*\[([^\]]*)\]/,
strong: /__(\S|\S[\s\S]*\S)__|\*\*(\S|\S[\s\S]*\S)\*\*/,
em: /_(\S|\S[\s\S]*\S)_|\*(\S|\S[\s\S]*\S)\*/,
strikeThrough: /~~(\S|\S[\s\S]*\S)~~/,
code: /(`+)\s*([\s\S]*?[^`])\s*\1(?!`)/,

verticalBar: /\u007C/,
Expand All @@ -377,6 +381,8 @@ Renderer.markdownTextToEscapeHtmlRx = /<([a-zA-Z_][a-zA-Z0-9\-\._]*)(\s|[^\\/>])

Renderer.markdownTextToEscapeBackSlashRx = /\\[!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~\\]/g;

Renderer.markdownTextToEscapeEmphasisCharsRx = /[*_~]/g;

Renderer.prototype._isNeedEscape = function(text) {
var res = false;
var markdownTextToEscapeRx = Renderer.markdownTextToEscapeRx;
Expand Down
12 changes: 12 additions & 0 deletions libs/to-mark/test/renderer.basic.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,18 @@ describe('basicRenderer', function() {
// until then, we tolerate over escaping the > char
expect(getMarkdownText('1. &lt;text&gt;')).toEqual('1\\. \\<text\\>');
});

it('should escape all markdown emphasis characters.', function() {
expect(getMarkdownText('foo*bar')).toBe('foo\\*bar');
expect(getMarkdownText('foo*bar*baz')).toBe('foo\\*bar\\*baz');
expect(getMarkdownText('foo**bar**baz')).toBe('foo\\*\\*bar\\*\\*baz');

expect(getMarkdownText('foo_bar')).toBe('foo\\_bar');
expect(getMarkdownText('foo_bar_baz')).toBe('foo\\_bar\\_baz');
expect(getMarkdownText('foo__bar__baz')).toBe('foo\\_\\_bar\\_\\_baz');

expect(getMarkdownText('foo~bar~~baz~')).toBe('foo\\~bar\\~\\~baz\\~');
});
});

describe('inline', function() {
Expand Down
21 changes: 0 additions & 21 deletions libs/to-mark/test/renderer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,27 +298,6 @@ describe('renderer', function() {
expect(renderer._isNeedEscape('~awefwaef')).toEqual(false);
});

it('em, strong', function() {
expect(renderer._isNeedEscape('*a*')).toEqual(true);
expect(renderer._isNeedEscape('_a_')).toEqual(true);
expect(renderer._isNeedEscape('*em*')).toEqual(true);
expect(renderer._isNeedEscape('_em_')).toEqual(true);
expect(renderer._isNeedEscape('**strong**')).toEqual(true);
expect(renderer._isNeedEscape('__strong__')).toEqual(true);

expect(renderer._isNeedEscape('_ em_')).toEqual(false);
});

it('strikeThrough', function() {
expect(renderer._isNeedEscape('~~a~~')).toEqual(true);
expect(renderer._isNeedEscape('~~true~~')).toEqual(true);
expect(renderer._isNeedEscape('~~strike through~~')).toEqual(true);

expect(renderer._isNeedEscape('~~strike~')).toEqual(false);
expect(renderer._isNeedEscape('~~strike~through~')).toEqual(false);
expect(renderer._isNeedEscape('~strike~')).toEqual(false);
});

it('link,img', function() {
expect(renderer._isNeedEscape('[abaewf](afewf)')).toEqual(true);
expect(renderer._isNeedEscape('![abaewf](afewf)')).toEqual(true);
Expand Down

0 comments on commit fc00ffa

Please sign in to comment.