unsafe.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /**
  2. * @import {ConstructName, Unsafe} from 'mdast-util-to-markdown'
  3. */
  4. /**
  5. * List of constructs that occur in phrasing (paragraphs, headings), but cannot
  6. * contain things like attention (emphasis, strong), images, or links.
  7. * So they sort of cancel each other out.
  8. * Note: could use a better name.
  9. *
  10. * @type {Array<ConstructName>}
  11. */
  12. const fullPhrasingSpans = [
  13. 'autolink',
  14. 'destinationLiteral',
  15. 'destinationRaw',
  16. 'reference',
  17. 'titleQuote',
  18. 'titleApostrophe'
  19. ]
  20. /** @type {Array<Unsafe>} */
  21. export const unsafe = [
  22. {character: '\t', after: '[\\r\\n]', inConstruct: 'phrasing'},
  23. {character: '\t', before: '[\\r\\n]', inConstruct: 'phrasing'},
  24. {
  25. character: '\t',
  26. inConstruct: ['codeFencedLangGraveAccent', 'codeFencedLangTilde']
  27. },
  28. {
  29. character: '\r',
  30. inConstruct: [
  31. 'codeFencedLangGraveAccent',
  32. 'codeFencedLangTilde',
  33. 'codeFencedMetaGraveAccent',
  34. 'codeFencedMetaTilde',
  35. 'destinationLiteral',
  36. 'headingAtx'
  37. ]
  38. },
  39. {
  40. character: '\n',
  41. inConstruct: [
  42. 'codeFencedLangGraveAccent',
  43. 'codeFencedLangTilde',
  44. 'codeFencedMetaGraveAccent',
  45. 'codeFencedMetaTilde',
  46. 'destinationLiteral',
  47. 'headingAtx'
  48. ]
  49. },
  50. {character: ' ', after: '[\\r\\n]', inConstruct: 'phrasing'},
  51. {character: ' ', before: '[\\r\\n]', inConstruct: 'phrasing'},
  52. {
  53. character: ' ',
  54. inConstruct: ['codeFencedLangGraveAccent', 'codeFencedLangTilde']
  55. },
  56. // An exclamation mark can start an image, if it is followed by a link or
  57. // a link reference.
  58. {
  59. character: '!',
  60. after: '\\[',
  61. inConstruct: 'phrasing',
  62. notInConstruct: fullPhrasingSpans
  63. },
  64. // A quote can break out of a title.
  65. {character: '"', inConstruct: 'titleQuote'},
  66. // A number sign could start an ATX heading if it starts a line.
  67. {atBreak: true, character: '#'},
  68. {character: '#', inConstruct: 'headingAtx', after: '(?:[\r\n]|$)'},
  69. // Dollar sign and percentage are not used in markdown.
  70. // An ampersand could start a character reference.
  71. {character: '&', after: '[#A-Za-z]', inConstruct: 'phrasing'},
  72. // An apostrophe can break out of a title.
  73. {character: "'", inConstruct: 'titleApostrophe'},
  74. // A left paren could break out of a destination raw.
  75. {character: '(', inConstruct: 'destinationRaw'},
  76. // A left paren followed by `]` could make something into a link or image.
  77. {
  78. before: '\\]',
  79. character: '(',
  80. inConstruct: 'phrasing',
  81. notInConstruct: fullPhrasingSpans
  82. },
  83. // A right paren could start a list item or break out of a destination
  84. // raw.
  85. {atBreak: true, before: '\\d+', character: ')'},
  86. {character: ')', inConstruct: 'destinationRaw'},
  87. // An asterisk can start thematic breaks, list items, emphasis, strong.
  88. {atBreak: true, character: '*', after: '(?:[ \t\r\n*])'},
  89. {character: '*', inConstruct: 'phrasing', notInConstruct: fullPhrasingSpans},
  90. // A plus sign could start a list item.
  91. {atBreak: true, character: '+', after: '(?:[ \t\r\n])'},
  92. // A dash can start thematic breaks, list items, and setext heading
  93. // underlines.
  94. {atBreak: true, character: '-', after: '(?:[ \t\r\n-])'},
  95. // A dot could start a list item.
  96. {atBreak: true, before: '\\d+', character: '.', after: '(?:[ \t\r\n]|$)'},
  97. // Slash, colon, and semicolon are not used in markdown for constructs.
  98. // A less than can start html (flow or text) or an autolink.
  99. // HTML could start with an exclamation mark (declaration, cdata, comment),
  100. // slash (closing tag), question mark (instruction), or a letter (tag).
  101. // An autolink also starts with a letter.
  102. // Finally, it could break out of a destination literal.
  103. {atBreak: true, character: '<', after: '[!/?A-Za-z]'},
  104. {
  105. character: '<',
  106. after: '[!/?A-Za-z]',
  107. inConstruct: 'phrasing',
  108. notInConstruct: fullPhrasingSpans
  109. },
  110. {character: '<', inConstruct: 'destinationLiteral'},
  111. // An equals to can start setext heading underlines.
  112. {atBreak: true, character: '='},
  113. // A greater than can start block quotes and it can break out of a
  114. // destination literal.
  115. {atBreak: true, character: '>'},
  116. {character: '>', inConstruct: 'destinationLiteral'},
  117. // Question mark and at sign are not used in markdown for constructs.
  118. // A left bracket can start definitions, references, labels,
  119. {atBreak: true, character: '['},
  120. {character: '[', inConstruct: 'phrasing', notInConstruct: fullPhrasingSpans},
  121. {character: '[', inConstruct: ['label', 'reference']},
  122. // A backslash can start an escape (when followed by punctuation) or a
  123. // hard break (when followed by an eol).
  124. // Note: typical escapes are handled in `safe`!
  125. {character: '\\', after: '[\\r\\n]', inConstruct: 'phrasing'},
  126. // A right bracket can exit labels.
  127. {character: ']', inConstruct: ['label', 'reference']},
  128. // Caret is not used in markdown for constructs.
  129. // An underscore can start emphasis, strong, or a thematic break.
  130. {atBreak: true, character: '_'},
  131. {character: '_', inConstruct: 'phrasing', notInConstruct: fullPhrasingSpans},
  132. // A grave accent can start code (fenced or text), or it can break out of
  133. // a grave accent code fence.
  134. {atBreak: true, character: '`'},
  135. {
  136. character: '`',
  137. inConstruct: ['codeFencedLangGraveAccent', 'codeFencedMetaGraveAccent']
  138. },
  139. {character: '`', inConstruct: 'phrasing', notInConstruct: fullPhrasingSpans},
  140. // Left brace, vertical bar, right brace are not used in markdown for
  141. // constructs.
  142. // A tilde can start code (fenced).
  143. {atBreak: true, character: '~'}
  144. ]