Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

renderContent.js 3.0 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
  1. const liquid = require('./liquid')
  2. const cheerio = require('cheerio')
  3. const Entities = require('html-entities').XmlEntities
  4. const entities = new Entities()
  5. const stripHtmlComments = require('strip-html-comments')
  6. const createProcessor = require('./create-processor')
  7. // used below to remove extra newlines in TOC lists
  8. const endLine = '</a>\r?\n'
  9. const blankLine = '\\s*?[\r\n]*'
  10. const startNextLine = '[^\\S\r\n]*?[-\\*] <a'
  11. const blankLineInList = new RegExp(
  12. `(${endLine})${blankLine}(${startNextLine})`,
  13. 'mg'
  14. )
  15. // used below to remove unwanted newlines from inline tags in tables
  16. const inlineTags = ['a', 'code', 'em']
  17. const inlineTagString = `(?:${inlineTags.join('|')})`
  18. const inlineTagRegex = new RegExp(`\n?(</?${inlineTagString}>?)\n?`, 'gm')
  19. // parse multiple times because some templates contain more templates. :]
  20. module.exports = async function renderContent (
  21. template = '',
  22. context = {},
  23. options = {}
  24. ) {
  25. try {
  26. // remove any newlines that precede html comments, then remove the comments
  27. if (template) {
  28. template = stripHtmlComments(template.replace(/\n<!--/g, '<!--'))
  29. }
  30. template = await liquid.parseAndRender(template, context)
  31. // this workaround loses syntax highlighting but correctly handles tags like <em> and entities like &lt;
  32. template = template.replace(
  33. /``` ?shell\r?\n\s*?(\S[\s\S]*?)\r?\n.*?```/gm,
  34. '<pre><code class="hljs language-shell">$1</code></pre>'
  35. )
  36. // clean up empty lines in TOC lists left by unrendered list items (due to productVersions)
  37. // for example, remove the blank line here:
  38. // - <a>foo</a>
  39. //
  40. // - <a>bar</a>
  41. if (template.includes('</a>')) {
  42. template = template.replace(blankLineInList, '$1$2')
  43. }
  44. // this removes any extra newlines left by (now resolved) liquid
  45. // statements so that extra space doesn't mess with list numbering
  46. template = template.replace(/(\r?\n){3}/g, '\n\n')
  47. const processor = createProcessor(context)
  48. const vFile = await processor.process(template)
  49. let html = vFile.toString()
  50. // Remove unwanted newlines (which appear as spaces) from inline tags inside tables
  51. if (html.includes('<table>')) html = removeNewlinesFromInlineTags(html)
  52. if (options.textOnly) {
  53. html = cheerio
  54. .load(html)
  55. .text()
  56. .trim()
  57. }
  58. if (options.cheerioObject) {
  59. return cheerio.load(html, { xmlMode: true })
  60. }
  61. if (options.encodeEntities) html = entities.encode(html)
  62. return html.trim()
  63. } catch (error) {
  64. if (options.filename) {
  65. console.error(`renderContent failed on file: ${options.filename}`)
  66. }
  67. throw error
  68. }
  69. }
  70. function removeNewlinesFromInlineTags (html) {
  71. const $ = cheerio.load(html)
  72. // see https://cheerio.js.org/#html-htmlstring-
  73. $(inlineTags.join(','))
  74. .parents('td')
  75. .get()
  76. .map(tag =>
  77. $(tag).html(
  78. $(tag)
  79. .html()
  80. .replace(inlineTagRegex, '$1')
  81. )
  82. )
  83. return $('body').html()
  84. }
  85. Object.assign(module.exports, {
  86. liquid
  87. })
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...