Renderer.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. import { defaults } from './defaults.js';
  2. import {
  3. cleanUrl,
  4. escape
  5. } from './helpers.js';
  6. /**
  7. * Renderer
  8. */
  9. export class Renderer {
  10. constructor(options) {
  11. this.options = options || defaults;
  12. }
  13. code(code, infostring, escaped) {
  14. const lang = (infostring || '').match(/\S*/)[0];
  15. if (this.options.highlight) {
  16. const out = this.options.highlight(code, lang);
  17. if (out != null && out !== code) {
  18. escaped = true;
  19. code = out;
  20. }
  21. }
  22. code = code.replace(/\n$/, '') + '\n';
  23. if (!lang) {
  24. return '<pre><code>'
  25. + (escaped ? code : escape(code, true))
  26. + '</code></pre>\n';
  27. }
  28. return '<pre><code class="'
  29. + this.options.langPrefix
  30. + escape(lang, true)
  31. + '">'
  32. + (escaped ? code : escape(code, true))
  33. + '</code></pre>\n';
  34. }
  35. blockquote(quote) {
  36. return '<blockquote>\n' + quote + '</blockquote>\n';
  37. }
  38. html(html) {
  39. return html;
  40. }
  41. heading(text, level, raw, slugger) {
  42. if (this.options.headerIds) {
  43. return '<h'
  44. + level
  45. + ' id="'
  46. + this.options.headerPrefix
  47. + slugger.slug(raw)
  48. + '">'
  49. + text
  50. + '</h'
  51. + level
  52. + '>\n';
  53. }
  54. // ignore IDs
  55. return '<h' + level + '>' + text + '</h' + level + '>\n';
  56. }
  57. hr() {
  58. return this.options.xhtml ? '<hr/>\n' : '<hr>\n';
  59. }
  60. list(body, ordered, start) {
  61. const type = ordered ? 'ol' : 'ul',
  62. startatt = (ordered && start !== 1) ? (' start="' + start + '"') : '';
  63. return '<' + type + startatt + '>\n' + body + '</' + type + '>\n';
  64. }
  65. listitem(text) {
  66. return '<li>' + text + '</li>\n';
  67. }
  68. checkbox(checked) {
  69. return '<input '
  70. + (checked ? 'checked="" ' : '')
  71. + 'disabled="" type="checkbox"'
  72. + (this.options.xhtml ? ' /' : '')
  73. + '> ';
  74. }
  75. paragraph(text) {
  76. return '<p>' + text + '</p>\n';
  77. }
  78. table(header, body) {
  79. if (body) body = '<tbody>' + body + '</tbody>';
  80. return '<table>\n'
  81. + '<thead>\n'
  82. + header
  83. + '</thead>\n'
  84. + body
  85. + '</table>\n';
  86. }
  87. tablerow(content) {
  88. return '<tr>\n' + content + '</tr>\n';
  89. }
  90. tablecell(content, flags) {
  91. const type = flags.header ? 'th' : 'td';
  92. const tag = flags.align
  93. ? '<' + type + ' align="' + flags.align + '">'
  94. : '<' + type + '>';
  95. return tag + content + '</' + type + '>\n';
  96. }
  97. // span level renderer
  98. strong(text) {
  99. return '<strong>' + text + '</strong>';
  100. }
  101. em(text) {
  102. return '<em>' + text + '</em>';
  103. }
  104. codespan(text) {
  105. return '<code>' + text + '</code>';
  106. }
  107. br() {
  108. return this.options.xhtml ? '<br/>' : '<br>';
  109. }
  110. del(text) {
  111. return '<del>' + text + '</del>';
  112. }
  113. link(href, title, text) {
  114. href = cleanUrl(this.options.sanitize, this.options.baseUrl, href);
  115. if (href === null) {
  116. return text;
  117. }
  118. let out = '<a href="' + escape(href) + '"';
  119. if (title) {
  120. out += ' title="' + title + '"';
  121. }
  122. out += '>' + text + '</a>';
  123. return out;
  124. }
  125. image(href, title, text) {
  126. href = cleanUrl(this.options.sanitize, this.options.baseUrl, href);
  127. if (href === null) {
  128. return text;
  129. }
  130. let out = '<img src="' + href + '" alt="' + text + '"';
  131. if (title) {
  132. out += ' title="' + title + '"';
  133. }
  134. out += this.options.xhtml ? '/>' : '>';
  135. return out;
  136. }
  137. text(text) {
  138. return text;
  139. }
  140. }