From a98b5ea6fe8e8cc98a55e0fd793e6e8660ea31c1 Mon Sep 17 00:00:00 2001 From: Armand Philippot Date: Thu, 30 Dec 2021 19:47:21 +0100 Subject: chore: add prismjs for syntax highlighting --- public/prism/prism-cshtml.js | 234 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 234 insertions(+) create mode 100644 public/prism/prism-cshtml.js (limited to 'public/prism/prism-cshtml.js') diff --git a/public/prism/prism-cshtml.js b/public/prism/prism-cshtml.js new file mode 100644 index 0000000..df73bac --- /dev/null +++ b/public/prism/prism-cshtml.js @@ -0,0 +1,234 @@ +// Docs: +// https://docs.microsoft.com/en-us/aspnet/core/razor-pages/?view=aspnetcore-5.0&tabs=visual-studio +// https://docs.microsoft.com/en-us/aspnet/core/mvc/views/razor?view=aspnetcore-5.0 + +(function (Prism) { + var commentLike = /\/(?![/*])|\/\/.*[\r\n]|\/\*[^*]*(?:\*(?!\/)[^*]*)*\*\// + .source; + var stringLike = + /@(?!")|"(?:[^\r\n\\"]|\\.)*"|@"(?:[^\\"]|""|\\[\s\S])*"(?!")/.source + + '|' + + /'(?:(?:[^\r\n'\\]|\\.|\\[Uux][\da-fA-F]{1,8})'|(?=[^\\](?!')))/.source; + + /** + * Creates a nested pattern where all occurrences of the string `<>` are replaced with the pattern itself. + * + * @param {string} pattern + * @param {number} depthLog2 + * @returns {string} + */ + function nested(pattern, depthLog2) { + for (var i = 0; i < depthLog2; i++) { + pattern = pattern.replace(//g, function () { + return '(?:' + pattern + ')'; + }); + } + return pattern + .replace(//g, '[^\\s\\S]') + .replace(//g, '(?:' + stringLike + ')') + .replace(//g, '(?:' + commentLike + ')'); + } + + var round = nested(/\((?:[^()'"@/]|||)*\)/.source, 2); + var square = nested(/\[(?:[^\[\]'"@/]|||)*\]/.source, 2); + var curly = nested(/\{(?:[^{}'"@/]|||)*\}/.source, 2); + var angle = nested(/<(?:[^<>'"@/]|||)*>/.source, 2); + + // Note about the above bracket patterns: + // They all ignore HTML expressions that might be in the C# code. This is a problem because HTML (like strings and + // comments) is parsed differently. This is a huge problem because HTML might contain brackets and quotes which + // messes up the bracket and string counting implemented by the above patterns. + // + // This problem is not fixable because 1) HTML expression are highly context sensitive and very difficult to detect + // and 2) they require one capturing group at every nested level. See the `tagRegion` pattern to admire the + // complexity of an HTML expression. + // + // To somewhat alleviate the problem a bit, the patterns for characters (e.g. 'a') is very permissive, it also + // allows invalid characters to support HTML expressions like this:

That's it!

. + + var tagAttrs = + /(?:\s(?:\s*[^\s>\/=]+(?:\s*=\s*(?:"[^"]*"|'[^']*'|[^\s'">=]+(?=[\s>]))|(?=[\s/>])))+)?/ + .source; + var tagContent = /(?!\d)[^\s>\/=$<%]+/.source + tagAttrs + /\s*\/?>/.source; + var tagRegion = + /\B@?/.source + + '(?:' + + /<([a-zA-Z][\w:]*)/.source + + tagAttrs + + /\s*>/.source + + '(?:' + + (/[^<]/.source + + '|' + + // all tags that are not the start tag + // eslint-disable-next-line regexp/strict + /<\/?(?!\1\b)/.source + + tagContent + + '|' + + // nested start tag + nested( + // eslint-disable-next-line regexp/strict + /<\1/.source + + tagAttrs + + /\s*>/.source + + '(?:' + + (/[^<]/.source + + '|' + + // all tags that are not the start tag + // eslint-disable-next-line regexp/strict + /<\/?(?!\1\b)/.source + + tagContent + + '|' + + '') + + ')*' + + // eslint-disable-next-line regexp/strict + /<\/\1\s*>/.source, + 2 + )) + + ')*' + + // eslint-disable-next-line regexp/strict + /<\/\1\s*>/.source + + '|' + + /