From 4c551e8bcc88e29959aa91461dec316f7485dd70 Mon Sep 17 00:00:00 2001 From: Golmote Date: Thu, 27 Aug 2015 20:58:43 +0200 Subject: [PATCH] Markup: Simplify patterns + handle \r\n and \r --- components/prism-markdown.js | 30 ++++++++++++++---------------- components/prism-markdown.min.js | 2 +- 2 files changed, 15 insertions(+), 17 deletions(-) diff --git a/components/prism-markdown.js b/components/prism-markdown.js index 94a71546a1..f089f15167 100644 --- a/components/prism-markdown.js +++ b/components/prism-markdown.js @@ -2,15 +2,13 @@ Prism.languages.markdown = Prism.languages.extend('markup', {}); Prism.languages.insertBefore('markdown', 'prolog', { 'blockquote': { // > ... - pattern: /(^|\n)>(?:[\t ]*>)*/, - lookbehind: true, + pattern: /^>(?:[\t ]*>)*/m, alias: 'punctuation' }, 'code': [ { // Prefixed by 4 spaces or 1 tab - pattern: /(^|\n)(?: {4}|\t).+/, - lookbehind: true, + pattern: /^(?: {4}|\t).+/m, alias: 'keyword' }, { @@ -27,7 +25,7 @@ Prism.languages.insertBefore('markdown', 'prolog', { // title 2 // ------- - pattern: /\w+.*\n(?:==+|--+)/, + pattern: /\w+.*(?:\r?\n|\r)(?:==+|--+)/, alias: 'important', inside: { punctuation: /==+$|--+$/ @@ -36,7 +34,7 @@ Prism.languages.insertBefore('markdown', 'prolog', { { // # title 1 // ###### title 6 - pattern: /((?:^|\n)\s*)#+.+/, + pattern: /(^\s*)#+.+/m, lookbehind: true, alias: 'important', inside: { @@ -49,7 +47,7 @@ Prism.languages.insertBefore('markdown', 'prolog', { // --- // * * * // ----------- - pattern: /((?:^|\n)\s*)([*-])([\t ]*\2){2,}(?=\s*(?:\n|$))/, + pattern: /(^\s*)([*-])([\t ]*\2){2,}(?=\s*$)/m, lookbehind: true, alias: 'punctuation' }, @@ -58,7 +56,7 @@ Prism.languages.insertBefore('markdown', 'prolog', { // + item // - item // 1. item - pattern: /((?:^|\n)\s*)(?:[*+-]|\d+\.)(?=[\t ].)/, + pattern: /(^\s*)(?:[*+-]|\d+\.)(?=[\t ].)/m, lookbehind: true, alias: 'punctuation' }, @@ -67,14 +65,14 @@ Prism.languages.insertBefore('markdown', 'prolog', { // [id]: http://example.com 'Optional title' // [id]: http://example.com (Optional title) // [id]: "Optional title" - pattern: /!?\[[^\]]+\]:[\t ]+(?:\S+|<(?:[^>]|\\>)+>)(?:[\t ]+(?:"(?:[^"]|\\")*"|'(?:[^']|\\')*'|\((?:[^)]|\\\))*\)))?/, + pattern: /!?\[[^\]]+\]:[\t ]+(?:\S+|<(?:\\.|[^>\\])+>)(?:[\t ]+(?:"(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*'|\((?:\\.|[^)\\])*\)))?/, inside: { 'variable': { pattern: /^(!?\[)[^\]]+/, lookbehind: true }, - 'string': /(?:"(?:[^"]|\\")*"|'(?:[^']|\\')*'|\((?:[^)]|\\\))*\))$/, - 'punctuation': /[[\]\(\)<>:]/ + 'string': /(?:"(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*'|\((?:\\.|[^)\\])*\))$/, + 'punctuation': /^[\[\]!:]|[<>]/ }, alias: 'url' }, @@ -83,10 +81,10 @@ Prism.languages.insertBefore('markdown', 'prolog', { // __strong__ // Allow only one line break - pattern: /(^|[^\\])(\*\*|__)(?:\n(?!\n)|.)+?\2/, + pattern: /(^|[^\\])(\*\*|__)(?:(?:\r?\n|\r)(?!\r?\n|\r)|.)+?\2/, lookbehind: true, inside: { - 'punctuation': /^\*\*|^__|\*\*\s*$|__\s*$/ + 'punctuation': /^\*\*|^__|\*\*$|__$/ } }, 'italic': { @@ -94,7 +92,7 @@ Prism.languages.insertBefore('markdown', 'prolog', { // _em_ // Allow only one line break - pattern: /(^|[^\\])(?:\*(?:\n(?!\n)|.)+?\*|_(?:\n(?!\n)|.)+?_)/, + pattern: /(^|[^\\])([*_])(?:(?:\r?\n|\r)(?!\r?\n|\r)|.)+?\2/, lookbehind: true, inside: { 'punctuation': /^[*_]|[*_]$/ @@ -103,14 +101,14 @@ Prism.languages.insertBefore('markdown', 'prolog', { 'url': { // [example](http://example.com "Optional title") // [example] [id] - pattern: /!?\[[^\]]+\](?:\([^\s)]+(?:[\t ]+"(?:[^"]|\\")*")?\)| ?\[[^\]\n]*\])/, + pattern: /!?\[[^\]]+\](?:\([^\s)]+(?:[\t ]+"(?:\\.|[^"\\])*")?\)| ?\[[^\]\n]*\])/, inside: { 'variable': { pattern: /(!?\[)[^\]]+(?=\]$)/, lookbehind: true }, 'string': { - pattern: /"(?:[^"]|\\")*"(?=\)$)/ + pattern: /"(?:\\.|[^"\\])*"(?=\)$)/ } } } diff --git a/components/prism-markdown.min.js b/components/prism-markdown.min.js index b685298ebd..bf40e3a1df 100644 --- a/components/prism-markdown.min.js +++ b/components/prism-markdown.min.js @@ -1 +1 @@ -Prism.languages.markdown=Prism.languages.extend("markup",{}),Prism.languages.insertBefore("markdown","prolog",{blockquote:{pattern:/(^|\n)>(?:[\t ]*>)*/,lookbehind:!0,alias:"punctuation"},code:[{pattern:/(^|\n)(?: {4}|\t).+/,lookbehind:!0,alias:"keyword"},{pattern:/``.+?``|`[^`\n]+`/,alias:"keyword"}],title:[{pattern:/\w+.*\n(?:==+|--+)/,alias:"important",inside:{punctuation:/==+$|--+$/}},{pattern:/((?:^|\n)\s*)#+.+/,lookbehind:!0,alias:"important",inside:{punctuation:/^#+|#+$/}}],hr:{pattern:/((?:^|\n)\s*)([*-])([\t ]*\2){2,}(?=\s*(?:\n|$))/,lookbehind:!0,alias:"punctuation"},list:{pattern:/((?:^|\n)\s*)(?:[*+-]|\d+\.)(?=[\t ].)/,lookbehind:!0,alias:"punctuation"},"url-reference":{pattern:/!?\[[^\]]+\]:[\t ]+(?:\S+|<(?:[^>]|\\>)+>)(?:[\t ]+(?:"(?:[^"]|\\")*"|'(?:[^']|\\')*'|\((?:[^)]|\\\))*\)))?/,inside:{variable:{pattern:/^(!?\[)[^\]]+/,lookbehind:!0},string:/(?:"(?:[^"]|\\")*"|'(?:[^']|\\')*'|\((?:[^)]|\\\))*\))$/,punctuation:/[[\]\(\)<>:]/},alias:"url"},bold:{pattern:/(^|[^\\])(\*\*|__)(?:\n(?!\n)|.)+?\2/,lookbehind:!0,inside:{punctuation:/^\*\*|^__|\*\*\s*$|__\s*$/}},italic:{pattern:/(^|[^\\])(?:\*(?:\n(?!\n)|.)+?\*|_(?:\n(?!\n)|.)+?_)/,lookbehind:!0,inside:{punctuation:/^[*_]|[*_]$/}},url:{pattern:/!?\[[^\]]+\](?:\([^\s)]+(?:[\t ]+"(?:[^"]|\\")*")?\)| ?\[[^\]\n]*\])/,inside:{variable:{pattern:/(!?\[)[^\]]+(?=\]$)/,lookbehind:!0},string:{pattern:/"(?:[^"]|\\")*"(?=\)$)/}}}}),Prism.languages.markdown.bold.inside.url=Prism.util.clone(Prism.languages.markdown.url),Prism.languages.markdown.italic.inside.url=Prism.util.clone(Prism.languages.markdown.url),Prism.languages.markdown.bold.inside.italic=Prism.util.clone(Prism.languages.markdown.italic),Prism.languages.markdown.italic.inside.bold=Prism.util.clone(Prism.languages.markdown.bold); \ No newline at end of file +Prism.languages.markdown=Prism.languages.extend("markup",{}),Prism.languages.insertBefore("markdown","prolog",{blockquote:{pattern:/^>(?:[\t ]*>)*/m,alias:"punctuation"},code:[{pattern:/^(?: {4}|\t).+/m,alias:"keyword"},{pattern:/``.+?``|`[^`\n]+`/,alias:"keyword"}],title:[{pattern:/\w+.*(?:\r?\n|\r)(?:==+|--+)/,alias:"important",inside:{punctuation:/==+$|--+$/}},{pattern:/(^\s*)#+.+/m,lookbehind:!0,alias:"important",inside:{punctuation:/^#+|#+$/}}],hr:{pattern:/(^\s*)([*-])([\t ]*\2){2,}(?=\s*$)/m,lookbehind:!0,alias:"punctuation"},list:{pattern:/(^\s*)(?:[*+-]|\d+\.)(?=[\t ].)/m,lookbehind:!0,alias:"punctuation"},"url-reference":{pattern:/!?\[[^\]]+\]:[\t ]+(?:\S+|<(?:\\.|[^>\\])+>)(?:[\t ]+(?:"(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*'|\((?:\\.|[^)\\])*\)))?/,inside:{variable:{pattern:/^(!?\[)[^\]]+/,lookbehind:!0},string:/(?:"(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*'|\((?:\\.|[^)\\])*\))$/,punctuation:/^[\[\]!:]|[<>]/},alias:"url"},bold:{pattern:/(^|[^\\])(\*\*|__)(?:(?:\r?\n|\r)(?!\r?\n|\r)|.)+?\2/,lookbehind:!0,inside:{punctuation:/^\*\*|^__|\*\*$|__$/}},italic:{pattern:/(^|[^\\])([*_])(?:(?:\r?\n|\r)(?!\r?\n|\r)|.)+?\2/,lookbehind:!0,inside:{punctuation:/^[*_]|[*_]$/}},url:{pattern:/!?\[[^\]]+\](?:\([^\s)]+(?:[\t ]+"(?:\\.|[^"\\])*")?\)| ?\[[^\]\n]*\])/,inside:{variable:{pattern:/(!?\[)[^\]]+(?=\]$)/,lookbehind:!0},string:{pattern:/"(?:\\.|[^"\\])*"(?=\)$)/}}}}),Prism.languages.markdown.bold.inside.url=Prism.util.clone(Prism.languages.markdown.url),Prism.languages.markdown.italic.inside.url=Prism.util.clone(Prism.languages.markdown.url),Prism.languages.markdown.bold.inside.italic=Prism.util.clone(Prism.languages.markdown.italic),Prism.languages.markdown.italic.inside.bold=Prism.util.clone(Prism.languages.markdown.bold); \ No newline at end of file