diff --git a/markdownify/__init__.py b/markdownify/__init__.py index cd66a39..ceeb793 100644 --- a/markdownify/__init__.py +++ b/markdownify/__init__.py @@ -208,8 +208,20 @@ def escape(self, text): if not text: return '' if self.options['escape_misc']: - text = re.sub(r'([\\&<`[>~#=+|-])', r'\\\1', text) - text = re.sub(r'([0-9])([.)])', r'\1\\\2', text) + text = re.sub(r'([\\&<`[>~=+|])', r'\\\1', text) + # A sequence of one or more consecutive '-', preceded and + # followed by whitespace or start/end of fragment, might + # be confused with an underline of a header, or with a + # list marker. + text = re.sub(r'(\s|^)(-+(?:\s|$))', r'\1\\\2', text) + # A sequence of up to six consecutive '#', preceded and + # followed by whitespace or start/end of fragment, might + # be confused with an ATX heading. + text = re.sub(r'(\s|^)(#{1,6}(?:\s|$))', r'\1\\\2', text) + # '.' or ')' preceded by up to nine digits might be + # confused with a list item. + text = re.sub(r'((?:\s|^)[0-9]{1,9})([.)](?:\s|$))', r'\1\\\2', + text) if self.options['escape_asterisks']: text = text.replace('*', r'\*') if self.options['escape_underscores']: diff --git a/tests/test_escaping.py b/tests/test_escaping.py index eaef77d..6eb8e07 100644 --- a/tests/test_escaping.py +++ b/tests/test_escaping.py @@ -28,20 +28,43 @@ def test_single_escaping_entities(): assert md('&amp;') == r'\&' -def text_misc(): +def test_misc(): assert md('\\*') == r'\\\*' - assert md('') == r'\' + assert md('<foo>') == r'\' assert md('# foo') == r'\# foo' + assert md('#5') == r'#5' + assert md('5#') == '5#' + assert md('####### foo') == r'####### foo' assert md('> foo') == r'\> foo' assert md('~~foo~~') == r'\~\~foo\~\~' assert md('foo\n===\n') == 'foo\n\\=\\=\\=\n' - assert md('---\n') == '\\-\\-\\-\n' + assert md('---\n') == '\\---\n' + assert md('- test') == r'\- test' + assert md('x - y') == r'x \- y' + assert md('test-case') == 'test-case' + assert md('x-') == 'x-' + assert md('-y') == '-y' assert md('+ x\n+ y\n') == '\\+ x\n\\+ y\n' assert md('`x`') == r'\`x\`' assert md('[text](link)') == r'\[text](link)' assert md('1. x') == r'1\. x' + # assert md('1. x') == r'1\. x' + assert md('1. x') == r'1\. x' + assert md(' 1. x') == r' 1\. x' + assert md('123456789. x') == r'123456789\. x' + assert md('1234567890. x') == r'1234567890. x' + assert md('A1. x') == r'A1. x' + assert md('1.2') == r'1.2' assert md('not a number. x') == r'not a number. x' assert md('1) x') == r'1\) x' + # assert md('1) x') == r'1\) x' + assert md('1) x') == r'1\) x' + assert md(' 1) x') == r' 1\) x' + assert md('123456789) x') == r'123456789\) x' + assert md('1234567890) x') == r'1234567890) x' + assert md('(1) x') == r'(1) x' + assert md('A1) x') == r'A1) x' + assert md('1)x') == r'1)x' assert md('not a number) x') == r'not a number) x' assert md('|not table|') == r'\|not table\|' - assert md(r'\ &amp; | ` `', escape_misc=False) == r'\ & | ` `' + assert md(r'\ <foo> &amp; | ` `', escape_misc=False) == r'\ & | ` `'