From 1f5adece65506a6976d5fefe61c68cfa74f04ef2 Mon Sep 17 00:00:00 2001 From: Jacob Mischka Date: Mon, 26 Jul 2021 13:07:47 -0500 Subject: [PATCH] Add checks to ensure in-word formatting chars aren't replaced Basic text formatting sequences (_, *, __, **) are now required to not be entirely within a word; that is, they must have a non-word character on either side to be replaced. This prevents words that include those characters (snake_case words, or some proper nouns that may include an Embedded*Asterisk__Or**Two for some reason) from being captured. Fixes #21 --- src/index.js | 2 +- test/index.js | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 3938c5c..530d0f7 100644 --- a/src/index.js +++ b/src/index.js @@ -24,7 +24,7 @@ function encodeAttr(str) { /** Parse Markdown into an HTML String. */ export default function parse(md, prevLinks) { - let tokenizer = /((?:^|\n+)(?:\n---+|\* \*(?: \*)+)\n)|(?:^``` *(\w*)\n([\s\S]*?)\n```$)|((?:(?:^|\n+)(?:\t| {2,}).+)+\n*)|((?:(?:^|\n)([>*+-]|\d+\.)\s+.*)+)|(?:!\[([^\]]*?)\]\(([^)]+?)\))|(\[)|(\](?:\(([^)]+?)\))?)|(?:(?:^|\n+)([^\s].*)\n(-{3,}|={3,})(?:\n+|$))|(?:(?:^|\n+)(#{1,6})\s*(.+)(?:\n+|$))|(?:`([^`].*?)`)|( \n\n*|\n{2,}|__|\*\*|[_*]|~~)/gm, + let tokenizer = /((?:^|\n+)(?:\n---+|\* \*(?: \*)+)\n)|(?:^``` *(\w*)\n([\s\S]*?)\n```$)|((?:(?:^|\n+)(?:\t| {2,}).+)+\n*)|((?:(?:^|\n)([>*+-]|\d+\.)\s+.*)+)|(?:!\[([^\]]*?)\]\(([^)]+?)\))|(\[)|(\](?:\(([^)]+?)\))?)|(?:(?:^|\n+)([^\s].*)\n(-{3,}|={3,})(?:\n+|$))|(?:(?:^|\n+)(#{1,6})\s*(.+)(?:\n+|$))|(?:`([^`].*?)`)|( \n\n*|\n{2,}|(?<=\W|^|$)(? { it('parses italics with _', () => { expect(snarkdown('I _like_ tiny libraries')).to.equal('I like tiny libraries'); }); + + it('ignores words with special formatting', () => { + expect(snarkdown('I like tiny*libraries')).to.equal('I like tiny*libraries'); + expect(snarkdown('I like tiny_libraries')).to.equal('I like tiny_libraries'); + expect(snarkdown('I like tiny**libraries')).to.equal('I like tiny**libraries'); + expect(snarkdown('I like tiny__libraries')).to.equal('I like tiny__libraries'); + }); }); describe('titles', () => {