Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Markdown: Improved URLs #1955

Merged
merged 3 commits into from
Jul 13, 2019
Merged
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
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.