prism-markdown.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. Prism.languages.markdown = Prism.languages.extend('markup', {});
  2. Prism.languages.insertBefore('markdown', 'prolog', {
  3. 'blockquote': {
  4. // > ...
  5. pattern: /(^|\n)>(?:[\t ]*>)*/,
  6. lookbehind: true,
  7. alias: 'punctuation'
  8. },
  9. 'code': [
  10. {
  11. // Prefixed by 4 spaces or 1 tab
  12. pattern: /(^|\n)(?: {4}|\t).+/,
  13. lookbehind: true,
  14. alias: 'keyword'
  15. },
  16. {
  17. // `code`
  18. // ``code``
  19. pattern: /``.+?``|`[^`\n]+`/,
  20. alias: 'keyword'
  21. }
  22. ],
  23. 'title': [
  24. {
  25. // title 1
  26. // =======
  27. // title 2
  28. // -------
  29. pattern: /\w+.*\n(?:==+|--+)/,
  30. alias: 'important',
  31. inside: {
  32. punctuation: /==+$|--+$/
  33. }
  34. },
  35. {
  36. // # title 1
  37. // ###### title 6
  38. pattern: /((?:^|\n)\s*)#+.+/,
  39. lookbehind: true,
  40. alias: 'important',
  41. inside: {
  42. punctuation: /^#+|#+$/
  43. }
  44. }
  45. ],
  46. 'hr': {
  47. // ***
  48. // ---
  49. // * * *
  50. // -----------
  51. pattern: /((?:^|\n)\s*)([*-])([\t ]*\2){2,}(?=\s*(?:\n|$))/,
  52. lookbehind: true,
  53. alias: 'punctuation'
  54. },
  55. 'list': {
  56. // * item
  57. // + item
  58. // - item
  59. // 1. item
  60. pattern: /((?:^|\n)\s*)(?:[*+-]|\d+\.)(?=[\t ].)/,
  61. lookbehind: true,
  62. alias: 'punctuation'
  63. },
  64. 'url-reference': {
  65. // [id]: http://example.com "Optional title"
  66. // [id]: http://example.com 'Optional title'
  67. // [id]: http://example.com (Optional title)
  68. // [id]: <http://example.com> "Optional title"
  69. pattern: /!?\[[^\]]+\]:[\t ]+(?:\S+|<(?:[^>]|\\>)+>)(?:[\t ]+(?:"(?:[^"]|\\")*"|'(?:[^']|\\')*'|\((?:[^)]|\\\))*\)))?/,
  70. inside: {
  71. 'variable': {
  72. pattern: /^(!?\[)[^\]]+/,
  73. lookbehind: true
  74. },
  75. 'string': /(?:"(?:[^"]|\\")*"|'(?:[^']|\\')*'|\((?:[^)]|\\\))*\))$/,
  76. 'punctuation': /[[\]\(\)<>:]/
  77. },
  78. alias: 'url'
  79. },
  80. 'bold': {
  81. // **strong**
  82. // __strong__
  83. // Allow only one line break
  84. pattern: /(^|[^\\])(\*\*|__)(?:\n(?!\n)|.)+?\2/,
  85. lookbehind: true,
  86. inside: {
  87. 'punctuation': /^\*\*|^__|\*\*\s*$|__\s*$/
  88. }
  89. },
  90. 'italic': {
  91. // *em*
  92. // _em_
  93. // Allow only one line break
  94. pattern: /(^|[^\\])(?:\*(?:\n(?!\n)|.)+?\*|_(?:\n(?!\n)|.)+?_)/,
  95. lookbehind: true,
  96. inside: {
  97. 'punctuation': /^[*_]|[*_]$/
  98. }
  99. },
  100. 'url': {
  101. // [example](http://example.com "Optional title")
  102. // [example] [id]
  103. pattern: /!?\[[^\]]+\](?:\([^\s)]+(?:[\t ]+"(?:[^"]|\\")*")?\)| ?\[[^\]\n]*\])/,
  104. inside: {
  105. 'variable': {
  106. pattern: /(!?\[)[^\]]+(?=\]$)/,
  107. lookbehind: true
  108. },
  109. 'string': {
  110. pattern: /"(?:[^"]|\\")*"(?=\)$)/
  111. }
  112. }
  113. }
  114. });
  115. Prism.languages.markdown['bold'].inside['url'] = Prism.util.clone(Prism.languages.markdown['url']);
  116. Prism.languages.markdown['italic'].inside['url'] = Prism.util.clone(Prism.languages.markdown['url']);
  117. Prism.languages.markdown['bold'].inside['italic'] = Prism.util.clone(Prism.languages.markdown['italic']);
  118. Prism.languages.markdown['italic'].inside['bold'] = Prism.util.clone(Prism.languages.markdown['bold']);