Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ export default function parse(md, prevLinks) {
links = prevLinks || {},
last = 0,
chunk, prev, token, inner, t;
// escape special characters
md = md.replace(/\\([*_[\]{}`<>()#+\-!|.])/g, m => `&%${m.charCodeAt(1)}%;`);

function tag(token) {
let desc = TAGS[token[1] || ''];
Expand Down Expand Up @@ -105,5 +107,14 @@ export default function parse(md, prevLinks) {
out += chunk;
}

return (out + md.substring(last) + flush()).replace(/^\n+|\n+$/g, '');
return (
out +
md
.substring(last)
// unscape special characters
.replace(/&%[0-9]+%;/g, (m) =>
String.fromCharCode(+m.substring(2, m.length - 2))
) +
flush()
).replace(/^\n+|\n+$/g, '');
}
17 changes: 17 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,4 +167,21 @@ describe('snarkdown()', () => {
expect(snarkdown('`')).to.equal('`');
});
});

describe('escaping', () => {
it('should escape special charcters', () => {
expect(snarkdown('\\*foo')).to.equal('*foo');
expect(snarkdown('this\\*is\\*important')).to.equal('this*is*important');
expect(snarkdown('\\_foo')).to.equal('_foo');
expect(snarkdown('\\[foo')).to.equal('[foo');
expect(snarkdown('\\]foo')).to.equal(']foo');
expect(snarkdown('\\(foo')).to.equal('(foo');
expect(snarkdown('\\)foo')).to.equal(')foo');
expect(snarkdown('\\{foo')).to.equal('{foo');
expect(snarkdown('\\}foo')).to.equal('}foo');
expect(snarkdown('\\-foo')).to.equal('-foo');
expect(snarkdown('\\+foo')).to.equal('+foo');
expect(snarkdown('\\#foo')).to.equal('#foo');
});
});
});