aboutsummaryrefslogtreecommitdiff
path: root/node_modules/prismjs/plugins/autolinker/prism-autolinker.js
diff options
context:
space:
mode:
authorruki <waruqi@gmail.com>2018-11-08 00:38:48 +0800
committerruki <waruqi@gmail.com>2018-11-07 21:53:09 +0800
commit26105034da4fcce7ac883c899d781f016559310d (patch)
treec459a5dc4e3aa0972d9919033ece511ce76dd129 /node_modules/prismjs/plugins/autolinker/prism-autolinker.js
parent2c77f00f1a7ecb6c8192f9c16d3b2001b254a107 (diff)
downloadxmake-docs-26105034da4fcce7ac883c899d781f016559310d.tar.gz
xmake-docs-26105034da4fcce7ac883c899d781f016559310d.zip
switch to vuepress
Diffstat (limited to 'node_modules/prismjs/plugins/autolinker/prism-autolinker.js')
-rw-r--r--node_modules/prismjs/plugins/autolinker/prism-autolinker.js81
1 files changed, 81 insertions, 0 deletions
diff --git a/node_modules/prismjs/plugins/autolinker/prism-autolinker.js b/node_modules/prismjs/plugins/autolinker/prism-autolinker.js
new file mode 100644
index 00000000..3913c98a
--- /dev/null
+++ b/node_modules/prismjs/plugins/autolinker/prism-autolinker.js
@@ -0,0 +1,81 @@
+(function(){
+
+if (
+ typeof self !== 'undefined' && !self.Prism ||
+ typeof global !== 'undefined' && !global.Prism
+) {
+ return;
+}
+
+var url = /\b([a-z]{3,7}:\/\/|tel:)[\w\-+%~/.:=&]+(?:\?[\w\-+%~/.:#=?&!$'()*,;]*)?(?:#[\w\-+%~/.:#=?&!$'()*,;]*)?/,
+ email = /\b\S+@[\w.]+[a-z]{2}/,
+ linkMd = /\[([^\]]+)]\(([^)]+)\)/,
+
+ // Tokens that may contain URLs and emails
+ candidates = ['comment', 'url', 'attr-value', 'string'];
+
+Prism.plugins.autolinker = {
+ processGrammar: function (grammar) {
+ // Abort if grammar has already been processed
+ if (!grammar || grammar['url-link']) {
+ return;
+ }
+ Prism.languages.DFS(grammar, function (key, def, type) {
+ if (candidates.indexOf(type) > -1 && Prism.util.type(def) !== 'Array') {
+ if (!def.pattern) {
+ def = this[key] = {
+ pattern: def
+ };
+ }
+
+ def.inside = def.inside || {};
+
+ if (type == 'comment') {
+ def.inside['md-link'] = linkMd;
+ }
+ if (type == 'attr-value') {
+ Prism.languages.insertBefore('inside', 'punctuation', { 'url-link': url }, def);
+ }
+ else {
+ def.inside['url-link'] = url;
+ }
+
+ def.inside['email-link'] = email;
+ }
+ });
+ grammar['url-link'] = url;
+ grammar['email-link'] = email;
+ }
+};
+
+Prism.hooks.add('before-highlight', function(env) {
+ Prism.plugins.autolinker.processGrammar(env.grammar);
+});
+
+Prism.hooks.add('wrap', function(env) {
+ if (/-link$/.test(env.type)) {
+ env.tag = 'a';
+
+ var href = env.content;
+
+ if (env.type == 'email-link' && href.indexOf('mailto:') != 0) {
+ href = 'mailto:' + href;
+ }
+ else if (env.type == 'md-link') {
+ // Markdown
+ var match = env.content.match(linkMd);
+
+ href = match[2];
+ env.content = match[1];
+ }
+
+ env.attributes.href = href;
+ }
+
+ // Silently catch any error thrown by decodeURIComponent (#1186)
+ try {
+ env.content = decodeURIComponent(env.content);
+ } catch(e) {}
+});
+
+})();