Skip to content
This repository has been archived by the owner on Jun 11, 2021. It is now read-only.

Commit

Permalink
only escape MD outside code blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
ara4n committed May 25, 2018
1 parent 4a4e064 commit f7c4ad3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
19 changes: 12 additions & 7 deletions lib/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,10 @@ var Markdown = function () {

if (node.object == "text") {
var leaves = node.getLeaves();
return leaves.map(this.serializeLeaves);
var inCodeBlock = !!document.getClosest(node.key, function (n) {
n.type === 'code';
});
return leaves.map(this.serializeLeaves, !inCodeBlock);
}

var children = node.nodes.map(function (node) {
Expand Down Expand Up @@ -276,10 +279,15 @@ var Markdown = function () {

}, {
key: "serializeLeaves",
value: function serializeLeaves(leaves) {
value: function serializeLeaves(leaves, escape) {
var _this3 = this;

var string = new String({ text: leaves.text });
var leavesText = leaves.text;
if (escape) {
// escape markdown characters
leavesText = leavesText.replace(/([\\`*{}\[\]()#+\-.!_>])/gi, "\\$1");
}
var string = new String({ text: leavesText });
var text = this.serializeString(string);

return leaves.marks.reduce(function (children, mark) {
Expand Down Expand Up @@ -322,9 +330,6 @@ var Markdown = function () {
}, {
key: "serializeString",
value: function serializeString(string) {
// escape markdown characters
var text = string.text.replace(/([\\`*{}\[\]()#+\-.!_>])/gi, "\\$1");

var _iteratorNormalCompletion3 = true;
var _didIteratorError3 = false;
var _iteratorError3 = undefined;
Expand All @@ -334,7 +339,7 @@ var Markdown = function () {
var rule = _step3.value;

if (!rule.serialize) continue;
var ret = rule.serialize(string, text);
var ret = rule.serialize(string, string.text);
if (ret) return ret;
}
} catch (err) {
Expand Down
19 changes: 12 additions & 7 deletions src/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,10 @@ class Markdown {
serializeNode(node, document) {
if (node.object == "text") {
const leaves = node.getLeaves();
return leaves.map(this.serializeLeaves);
const inCodeBlock = !!document.getClosest(
node.key, (n)=>{ n.type === 'code' }
);
return leaves.map(this.serializeLeaves, !inCodeBlock);
}

let children = node.nodes.map(node => this.serializeNode(node, document));
Expand All @@ -224,8 +227,13 @@ class Markdown {
* @return {String}
*/

serializeLeaves(leaves) {
const string = new String({ text: leaves.text });
serializeLeaves(leaves, escape) {
let leavesText = leaves.text;
if (escape) {
// escape markdown characters
leavesText = leavesText.replace(/([\\`*{}\[\]()#+\-.!_>])/gi, "\\$1");
}
const string = new String({ text: leavesText });
const text = this.serializeString(string);

return leaves.marks.reduce((children, mark) => {
Expand All @@ -245,12 +253,9 @@ class Markdown {
*/

serializeString(string) {
// escape markdown characters
const text = string.text.replace(/([\\`*{}\[\]()#+\-.!_>])/gi, "\\$1");

for (const rule of this.rules) {
if (!rule.serialize) continue;
const ret = rule.serialize(string, text);
const ret = rule.serialize(string, string.text);
if (ret) return ret;
}
}
Expand Down

0 comments on commit f7c4ad3

Please sign in to comment.