prism-wpd.js 5.0 KB

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