Skip to content

Commit

Permalink
Markdown: Improved URLs (#1955)
Browse files Browse the repository at this point in the history
This fixes the known issue that inline styles (bold, italic, strike) break URLs. Inline styles are now properly highlighted inside URLs.
  • Loading branch information
RunDevelopment authored Jul 13, 2019
1 parent d58d2ae commit b9ec6fd
Show file tree
Hide file tree
Showing 7 changed files with 179 additions and 32 deletions.
24 changes: 15 additions & 9 deletions components/prism-markdown.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(function (Prism) {

// Allow only one line break
var inner = /\\.|[^\\\n\r_]|(?:\r?\n|\r)(?!\r?\n|\r)/.source;
var inner = /(?:\\.|[^\\\n\r]|(?:\r?\n|\r)(?!\r?\n|\r))/.source;

/**
* This function is intended for the creation of the bold or italic pattern.
Expand Down Expand Up @@ -124,7 +124,7 @@
// __strong__

// allow one nested instance of italic text using the same delimiter
pattern: createInline(/__(?:<inner>|_(?:<inner>)+_)+__/.source, true),
pattern: createInline(/__(?:(?!_)<inner>|_(?:(?!_)<inner>)+_)+__/.source, true),
lookbehind: true,
greedy: true,
inside: {
Expand All @@ -141,7 +141,7 @@
// _em_

// allow one nested instance of bold text using the same delimiter
pattern: createInline(/_(?:<inner>|__(?:<inner>)+__)+_/.source, true),
pattern: createInline(/_(?:(?!_)<inner>|__(?:(?!_)<inner>)+__)+_/.source, true),
lookbehind: true,
greedy: true,
inside: {
Expand All @@ -156,9 +156,7 @@
'strike': {
// ~~strike through~~
// ~strike~

// extra _ is because the inner pattern intentionally doesn't include it because of bold and italic
pattern: createInline(/(~~?)(?:<inner>|_)+?\2/.source, false),
pattern: createInline(/(~~?)(?:(?!~)<inner>)+?\2/.source, false),
lookbehind: true,
greedy: true,
inside: {
Expand All @@ -172,21 +170,29 @@
},
'url': {
// [example](http://example.com "Optional title")
// [example][id]
// [example] [id]
pattern: /!?\[[^\]]+\](?:\([^\s)]+(?:[\t ]+"(?:\\.|[^"\\])*")?\)| ?\[[^\]\n]*\])/,
pattern: createInline(/!?\[(?:(?!\])<inner>)+\](?:\([^\s)]+(?:[\t ]+"(?:\\.|[^"\\])*")?\)| ?\[(?:(?!\])<inner>)+\])/.source, false),
lookbehind: true,
greedy: true,
inside: {
'variable': {
pattern: /(!?\[)[^\]]+(?=\]$)/,
pattern: /(\[)[^\]]+(?=\]$)/,
lookbehind: true
},
'content': {
pattern: /(^!?\[)[^\]]+(?=\])/,
lookbehind: true,
inside: {} // see below
},
'string': {
pattern: /"(?:\\.|[^"\\])*"(?=\)$)/
}
}
}
});

['bold', 'italic', 'strike'].forEach(function (token) {
['url', 'bold', 'italic', 'strike'].forEach(function (token) {
['url', 'bold', 'italic', 'strike'].forEach(function (inside) {
if (token !== inside) {
Prism.languages.markdown[token].inside.content.inside[inside] = Prism.languages.markdown[inside];
Expand Down
2 changes: 1 addition & 1 deletion components/prism-markdown.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 0 additions & 12 deletions known-failures.html
Original file line number Diff line number Diff line change
Expand Up @@ -153,18 +153,6 @@ <h3>Functions with a single string parameter not using parentheses are not highl
</section>


<section class="language-markdown">

<h3>Nesting of elements is not fully supported</h3>
<pre><code>[Link partially *italic* DOESN'T work](http://example.com)
_ [But link inside italic DOES work](http://example.com) _

[Link partially **bold** DOESN'T work](http://example.com)
__ [But link inside bold DOES work](http://example.com) __</code></pre>

</section>


<section class="language-nasm">

<h3>Numbers with underscores</h3>
Expand Down
12 changes: 10 additions & 2 deletions tests/languages/markdown/bold_feature.test
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,11 @@ __foo[bar](baz)__
["content", [
"foo",
["url", [
"[bar](baz)"
"[",
["content", [
"bar"
]],
"](baz)"
]]
]],
["punctuation", "__"]
Expand Down Expand Up @@ -183,7 +187,11 @@ __foo[bar](baz)__
["content", [
"foo",
["url", [
"[bar](baz)"
"[",
["content", [
"bar"
]],
"](baz)"
]]
]],
["punctuation", "**"]
Expand Down
12 changes: 10 additions & 2 deletions tests/languages/markdown/italic_feature.test
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,11 @@ _foo[bar](baz)_
["content", [
"foo",
["url", [
"[bar](baz)"
"[",
["content", [
"bar"
]],
"](baz)"
]]
]],
["punctuation", "_"]
Expand Down Expand Up @@ -183,7 +187,11 @@ _foo[bar](baz)_
["content", [
"foo",
["url", [
"[bar](baz)"
"[",
["content", [
"bar"
]],
"](baz)"
]]
]],
["punctuation", "*"]
Expand Down
12 changes: 10 additions & 2 deletions tests/languages/markdown/strike_feature.test
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,11 @@ bar~
["content", [
"foo",
["url", [
"[bar](baz)"
"[",
["content", [
"bar"
]],
"](baz)"
]]
]],
["punctuation", "~"]
Expand Down Expand Up @@ -183,7 +187,11 @@ bar~
["content", [
"foo",
["url", [
"[bar](baz)"
"[",
["content", [
"bar"
]],
"](baz)"
]]
]],
["punctuation", "~~"]
Expand Down
137 changes: 133 additions & 4 deletions tests/languages/markdown/url_feature.test
Original file line number Diff line number Diff line change
@@ -1,25 +1,154 @@
[foo](http://prismjs.com)
![foo](http://prismjs.com "Foo\"bar")
[foo][bar]
[foo] [bar]

[foo
bar](http://prismjs.com)

[foo *bar* baz](http://prismjs.com)
[foo _bar_ baz](http://prismjs.com)
[foo **bar** baz](http://prismjs.com)
[foo __bar__ baz](http://prismjs.com)
[foo ~bar~ baz](http://prismjs.com)
[foo ~~bar~~ baz](http://prismjs.com)

----------------------------------------------------

[
["url", [
"[foo](http://prismjs.com)"
"[",
["content", [
"foo"
]],
"](http://prismjs.com)"
]],
["url", [
"![foo](http://prismjs.com ",
"![",
["content", [
"foo"
]],
"](http://prismjs.com ",
["string", "\"Foo\\\"bar\""],
")"
]],
["url", [
"[foo] [",
"[",
["content", [
"foo"
]],
"][",
["variable", "bar"],
"]"
]],
["url", [
"[",
["content", [
"foo"
]],
"] [",
["variable", "bar"],
"]"
]],
["url", [
"[",
["content", [
"foo\r\nbar"
]],
"](http://prismjs.com)"
]],
["url", [
"[",
["content", [
"foo ",
["italic", [
["punctuation", "*"],
["content", [
"bar"
]],
["punctuation", "*"]
]],
" baz"
]],
"](http://prismjs.com)"
]],
["url", [
"[",
["content", [
"foo ",
["italic", [
["punctuation", "_"],
["content", [
"bar"
]],
["punctuation", "_"]
]],
" baz"
]],
"](http://prismjs.com)"
]],
["url", [
"[",
["content", [
"foo ",
["bold", [
["punctuation", "**"],
["content", [
"bar"
]],
["punctuation", "**"]
]],
" baz"
]],
"](http://prismjs.com)"
]],
["url", [
"[",
["content", [
"foo ",
["bold", [
["punctuation", "__"],
["content", [
"bar"
]],
["punctuation", "__"]
]],
" baz"
]],
"](http://prismjs.com)"
]],
["url", [
"[",
["content", [
"foo ",
["strike", [
["punctuation", "~"],
["content", [
"bar"
]],
["punctuation", "~"]
]],
" baz"
]],
"](http://prismjs.com)"
]],
["url", [
"[",
["content", [
"foo ",
["strike", [
["punctuation", "~~"],
["content", [
"bar"
]],
["punctuation", "~~"]
]],
" baz"
]],
"](http://prismjs.com)"
]]
]

----------------------------------------------------

Checks for URLs.
Checks for URLs.

0 comments on commit b9ec6fd

Please sign in to comment.