UNPKG

5.09 kBJavaScriptView Raw
1(function(){
2
3if (
4 typeof self !== 'undefined' && !self.Prism ||
5 typeof global !== 'undefined' && !global.Prism
6) {
7 return;
8}
9
10if (Prism.languages.css) {
11 // check whether the selector is an advanced pattern before extending it
12 if (Prism.languages.css.selector.pattern)
13 {
14 Prism.languages.css.selector.inside['pseudo-class'] = /:[\w-]+/;
15 Prism.languages.css.selector.inside['pseudo-element'] = /::[\w-]+/;
16 }
17 else
18 {
19 Prism.languages.css.selector = {
20 pattern: Prism.languages.css.selector,
21 inside: {
22 'pseudo-class': /:[\w-]+/,
23 'pseudo-element': /::[\w-]+/
24 }
25 };
26 }
27}
28
29if (Prism.languages.markup) {
30 Prism.languages.markup.tag.inside.tag.inside['tag-id'] = /[\w-]+/;
31
32 var Tags = {
33 HTML: {
34 'a': 1, 'abbr': 1, 'acronym': 1, 'b': 1, 'basefont': 1, 'bdo': 1, 'big': 1, 'blink': 1, 'cite': 1, 'code': 1, 'dfn': 1, 'em': 1, 'kbd': 1, 'i': 1,
35 'rp': 1, 'rt': 1, 'ruby': 1, 's': 1, 'samp': 1, 'small': 1, 'spacer': 1, 'strike': 1, 'strong': 1, 'sub': 1, 'sup': 1, 'time': 1, 'tt': 1, 'u': 1,
36 'var': 1, 'wbr': 1, 'noframes': 1, 'summary': 1, 'command': 1, 'dt': 1, 'dd': 1, 'figure': 1, 'figcaption': 1, 'center': 1, 'section': 1, 'nav': 1,
37 'article': 1, 'aside': 1, 'hgroup': 1, 'header': 1, 'footer': 1, 'address': 1, 'noscript': 1, 'isIndex': 1, 'main': 1, 'mark': 1, 'marquee': 1,
38 'meter': 1, 'menu': 1
39 },
40 SVG: {
41 'animateColor': 1, 'animateMotion': 1, 'animateTransform': 1, 'glyph': 1, 'feBlend': 1, 'feColorMatrix': 1, 'feComponentTransfer': 1,
42 'feFuncR': 1, 'feFuncG': 1, 'feFuncB': 1, 'feFuncA': 1, 'feComposite': 1, 'feConvolveMatrix': 1, 'feDiffuseLighting': 1, 'feDisplacementMap': 1,
43 'feFlood': 1, 'feGaussianBlur': 1, 'feImage': 1, 'feMerge': 1, 'feMergeNode': 1, 'feMorphology': 1, 'feOffset': 1, 'feSpecularLighting': 1,
44 'feTile': 1, 'feTurbulence': 1, 'feDistantLight': 1, 'fePointLight': 1, 'feSpotLight': 1, 'linearGradient': 1, 'radialGradient': 1, 'altGlyph': 1,
45 'textPath': 1, 'tref': 1, 'altglyph': 1, 'textpath': 1, 'altglyphdef': 1, 'altglyphitem': 1, 'clipPath': 1, 'color-profile': 1, 'cursor': 1,
46 'font-face': 1, 'font-face-format': 1, 'font-face-name': 1, 'font-face-src': 1, 'font-face-uri': 1, 'foreignObject': 1, 'glyphRef': 1,
47 'hkern': 1, 'vkern': 1
48 },
49 MathML: {}
50 }
51}
52
53var language;
54
55Prism.hooks.add('wrap', function(env) {
56 if ((env.type == 'tag-id'
57 || (env.type == 'property' && env.content.indexOf('-') != 0)
58 || (env.type == 'rule'&& env.content.indexOf('@-') != 0)
59 || (env.type == 'pseudo-class'&& env.content.indexOf(':-') != 0)
60 || (env.type == 'pseudo-element'&& env.content.indexOf('::-') != 0)
61 || (env.type == 'attr-name' && env.content.indexOf('data-') != 0)
62 ) && env.content.indexOf('<') === -1
63 ) {
64 if (env.language == 'css'
65 || env.language == 'scss'
66 || env.language == 'markup'
67 ) {
68 var href = 'https://webplatform.github.io/docs/';
69 var content = env.content;
70
71 if (env.language == 'css' || env.language == 'scss') {
72 href += 'css/';
73
74 if (env.type == 'property') {
75 href += 'properties/';
76 }
77 else if (env.type == 'rule') {
78 href += 'atrules/';
79 content = content.substring(1);
80 }
81 else if (env.type == 'pseudo-class') {
82 href += 'selectors/pseudo-classes/';
83 content = content.substring(1);
84 }
85 else if (env.type == 'pseudo-element') {
86 href += 'selectors/pseudo-elements/';
87 content = content.substring(2);
88 }
89 }
90 else if (env.language == 'markup') {
91 if (env.type == 'tag-id') {
92 // Check language
93 language = getLanguage(env.content) || language;
94
95 if (language) {
96 href += language + '/elements/';
97 }
98 else {
99 return; // Abort
100 }
101 }
102 else if (env.type == 'attr-name') {
103 if (language) {
104 href += language + '/attributes/';
105 }
106 else {
107 return; // Abort
108 }
109 }
110 }
111
112 href += content;
113 env.tag = 'a';
114 env.attributes.href = href;
115 env.attributes.target = '_blank';
116 }
117 }
118});
119
120function getLanguage(tag) {
121 var tagL = tag.toLowerCase();
122
123 if (Tags.HTML[tagL]) {
124 return 'html';
125 }
126 else if (Tags.SVG[tag]) {
127 return 'svg';
128 }
129 else if (Tags.MathML[tag]) {
130 return 'mathml';
131 }
132
133 // Not in dictionary, perform check
134 if (Tags.HTML[tagL] !== 0 && typeof document !== 'undefined') {
135 var htmlInterface = (document.createElement(tag).toString().match(/\[object HTML(.+)Element\]/) || [])[1];
136
137 if (htmlInterface && htmlInterface != 'Unknown') {
138 Tags.HTML[tagL] = 1;
139 return 'html';
140 }
141 }
142
143 Tags.HTML[tagL] = 0;
144
145 if (Tags.SVG[tag] !== 0 && typeof document !== 'undefined') {
146 var svgInterface = (document.createElementNS('http://www.w3.org/2000/svg', tag).toString().match(/\[object SVG(.+)Element\]/) || [])[1];
147
148 if (svgInterface && svgInterface != 'Unknown') {
149 Tags.SVG[tag] = 1;
150 return 'svg';
151 }
152 }
153
154 Tags.SVG[tag] = 0;
155
156 // Lame way to detect MathML, but browsers don’t expose interface names there :(
157 if (Tags.MathML[tag] !== 0) {
158 if (tag.indexOf('m') === 0) {
159 Tags.MathML[tag] = 1;
160 return 'mathml';
161 }
162 }
163
164 Tags.MathML[tag] = 0;
165
166 return null;
167}
168
169})();
\No newline at end of file