markdown.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*---------------------------------------------------------------------------------------------
  2. * Copyright (c) Microsoft Corporation. All rights reserved.
  3. * Licensed under the MIT License. See License.txt in the project root for license information.
  4. *--------------------------------------------------------------------------------------------*/
  5. define(["require", "exports"], function (require, exports) {
  6. 'use strict';
  7. Object.defineProperty(exports, "__esModule", { value: true });
  8. exports.conf = {
  9. comments: {
  10. blockComment: ['<!--', '-->',]
  11. },
  12. brackets: [
  13. ['{', '}'],
  14. ['[', ']'],
  15. ['(', ')']
  16. ],
  17. autoClosingPairs: [
  18. { open: '{', close: '}' },
  19. { open: '[', close: ']' },
  20. { open: '(', close: ')' },
  21. { open: '<', close: '>', notIn: ['string'] }
  22. ],
  23. surroundingPairs: [
  24. { open: '(', close: ')' },
  25. { open: '[', close: ']' },
  26. { open: '`', close: '`' },
  27. ],
  28. folding: {
  29. markers: {
  30. start: new RegExp("^\\s*<!--\\s*#?region\\b.*-->"),
  31. end: new RegExp("^\\s*<!--\\s*#?endregion\\b.*-->")
  32. }
  33. }
  34. };
  35. exports.language = {
  36. defaultToken: '',
  37. tokenPostfix: '.md',
  38. // escape codes
  39. control: /[\\`*_\[\]{}()#+\-\.!]/,
  40. noncontrol: /[^\\`*_\[\]{}()#+\-\.!]/,
  41. escapes: /\\(?:@control)/,
  42. // escape codes for javascript/CSS strings
  43. jsescapes: /\\(?:[btnfr\\"']|[0-7][0-7]?|[0-3][0-7]{2})/,
  44. // non matched elements
  45. empty: [
  46. 'area', 'base', 'basefont', 'br', 'col', 'frame',
  47. 'hr', 'img', 'input', 'isindex', 'link', 'meta', 'param'
  48. ],
  49. tokenizer: {
  50. root: [
  51. // markdown tables
  52. [/^\s*\|/, '@rematch', '@table_header'],
  53. // headers (with #)
  54. [/^(\s{0,3})(#+)((?:[^\\#]|@escapes)+)((?:#+)?)/, ['white', 'keyword', 'keyword', 'keyword']],
  55. // headers (with =)
  56. [/^\s*(=+|\-+)\s*$/, 'keyword'],
  57. // headers (with ***)
  58. [/^\s*((\*[ ]?)+)\s*$/, 'meta.separator'],
  59. // quote
  60. [/^\s*>+/, 'comment'],
  61. // list (starting with * or number)
  62. [/^\s*([\*\-+:]|\d+\.)\s/, 'keyword'],
  63. // code block (4 spaces indent)
  64. [/^(\t|[ ]{4})[^ ].*$/, 'string'],
  65. // code block (3 tilde)
  66. [/^\s*~~~\s*((?:\w|[\/\-#])+)?\s*$/, { token: 'string', next: '@codeblock' }],
  67. // github style code blocks (with backticks and language)
  68. [/^\s*```\s*((?:\w|[\/\-#])+).*$/, { token: 'string', next: '@codeblockgh', nextEmbedded: '$1' }],
  69. // github style code blocks (with backticks but no language)
  70. [/^\s*```\s*$/, { token: 'string', next: '@codeblock' }],
  71. // markup within lines
  72. { include: '@linecontent' },
  73. ],
  74. table_header: [
  75. { include: '@table_common' },
  76. [/[^\|]+/, 'keyword.table.header'],
  77. ],
  78. table_body: [
  79. { include: '@table_common' },
  80. { include: '@linecontent' },
  81. ],
  82. table_common: [
  83. [/\s*[\-:]+\s*/, { token: 'keyword', switchTo: 'table_body' }],
  84. [/^\s*\|/, 'keyword.table.left'],
  85. [/^\s*[^\|]/, '@rematch', '@pop'],
  86. [/^\s*$/, '@rematch', '@pop'],
  87. [/\|/, {
  88. cases: {
  89. '@eos': 'keyword.table.right',
  90. '@default': 'keyword.table.middle',
  91. }
  92. }],
  93. ],
  94. codeblock: [
  95. [/^\s*~~~\s*$/, { token: 'string', next: '@pop' }],
  96. [/^\s*```\s*$/, { token: 'string', next: '@pop' }],
  97. [/.*$/, 'variable.source'],
  98. ],
  99. // github style code blocks
  100. codeblockgh: [
  101. [/```\s*$/, { token: 'variable.source', next: '@pop', nextEmbedded: '@pop' }],
  102. [/[^`]+/, 'variable.source'],
  103. ],
  104. linecontent: [
  105. // escapes
  106. [/&\w+;/, 'string.escape'],
  107. [/@escapes/, 'escape'],
  108. // various markup
  109. [/\b__([^\\_]|@escapes|_(?!_))+__\b/, 'strong'],
  110. [/\*\*([^\\*]|@escapes|\*(?!\*))+\*\*/, 'strong'],
  111. [/\b_[^_]+_\b/, 'emphasis'],
  112. [/\*([^\\*]|@escapes)+\*/, 'emphasis'],
  113. [/`([^\\`]|@escapes)+`/, 'variable'],
  114. // links
  115. [/\{+[^}]+\}+/, 'string.target'],
  116. [/(!?\[)((?:[^\]\\]|@escapes)*)(\]\([^\)]+\))/, ['string.link', '', 'string.link']],
  117. [/(!?\[)((?:[^\]\\]|@escapes)*)(\])/, 'string.link'],
  118. // or html
  119. { include: 'html' },
  120. ],
  121. // Note: it is tempting to rather switch to the real HTML mode instead of building our own here
  122. // but currently there is a limitation in Monarch that prevents us from doing it: The opening
  123. // '<' would start the HTML mode, however there is no way to jump 1 character back to let the
  124. // HTML mode also tokenize the opening angle bracket. Thus, even though we could jump to HTML,
  125. // we cannot correctly tokenize it in that mode yet.
  126. html: [
  127. // html tags
  128. [/<(\w+)\/>/, 'tag'],
  129. [/<(\w+)/, {
  130. cases: {
  131. '@empty': { token: 'tag', next: '@tag.$1' },
  132. '@default': { token: 'tag', next: '@tag.$1' }
  133. }
  134. }],
  135. [/<\/(\w+)\s*>/, { token: 'tag' }],
  136. [/<!--/, 'comment', '@comment']
  137. ],
  138. comment: [
  139. [/[^<\-]+/, 'comment.content'],
  140. [/-->/, 'comment', '@pop'],
  141. [/<!--/, 'comment.content.invalid'],
  142. [/[<\-]/, 'comment.content']
  143. ],
  144. // Almost full HTML tag matching, complete with embedded scripts & styles
  145. tag: [
  146. [/[ \t\r\n]+/, 'white'],
  147. [/(type)(\s*=\s*)(")([^"]+)(")/, ['attribute.name.html', 'delimiter.html', 'string.html',
  148. { token: 'string.html', switchTo: '@tag.$S2.$4' },
  149. 'string.html']],
  150. [/(type)(\s*=\s*)(')([^']+)(')/, ['attribute.name.html', 'delimiter.html', 'string.html',
  151. { token: 'string.html', switchTo: '@tag.$S2.$4' },
  152. 'string.html']],
  153. [/(\w+)(\s*=\s*)("[^"]*"|'[^']*')/, ['attribute.name.html', 'delimiter.html', 'string.html']],
  154. [/\w+/, 'attribute.name.html'],
  155. [/\/>/, 'tag', '@pop'],
  156. [/>/, {
  157. cases: {
  158. '$S2==style': { token: 'tag', switchTo: 'embeddedStyle', nextEmbedded: 'text/css' },
  159. '$S2==script': {
  160. cases: {
  161. '$S3': { token: 'tag', switchTo: 'embeddedScript', nextEmbedded: '$S3' },
  162. '@default': { token: 'tag', switchTo: 'embeddedScript', nextEmbedded: 'text/javascript' }
  163. }
  164. },
  165. '@default': { token: 'tag', next: '@pop' }
  166. }
  167. }],
  168. ],
  169. embeddedStyle: [
  170. [/[^<]+/, ''],
  171. [/<\/style\s*>/, { token: '@rematch', next: '@pop', nextEmbedded: '@pop' }],
  172. [/</, '']
  173. ],
  174. embeddedScript: [
  175. [/[^<]+/, ''],
  176. [/<\/script\s*>/, { token: '@rematch', next: '@pop', nextEmbedded: '@pop' }],
  177. [/</, '']
  178. ],
  179. }
  180. };
  181. });