render-page.mjs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import { get, has, isEmpty, reduce, times, toSafeInteger } from 'lodash-es'
  2. import * as cheerio from 'cheerio'
  3. export async function task ({ payload }) {
  4. WIKI.logger.info(`Rendering page ${payload.id}...`)
  5. try {
  6. await WIKI.ensureDb()
  7. const page = await WIKI.db.pages.getPageFromDb(payload.id)
  8. if (!page) {
  9. throw new Error('Invalid Page Id')
  10. }
  11. const site = await WIKI.db.sites.query().findById(page.siteId)
  12. let output = page.content
  13. // Empty content?
  14. if (isEmpty(output)) {
  15. WIKI.logger.warn(`Failed to render page ID ${payload.id} because content was empty: [ FAILED ]`)
  16. throw new Error(`Failed to render page ID ${payload.id} because content was empty.`)
  17. }
  18. // Parse to HTML
  19. switch (page.contentType) {
  20. case 'asciidoc': {
  21. const { render } = await import('../../renderers/asciidoc.mjs')
  22. output = await render(output, site.config?.editors?.asciidoc?.config ?? {})
  23. break
  24. }
  25. case 'markdown': {
  26. const { render } = await import('../../renderers/markdown.mjs')
  27. output = await render(output, site.config?.editors?.markdown?.config ?? {})
  28. break
  29. }
  30. }
  31. // Render HTML
  32. await WIKI.db.renderers.fetchDefinitions()
  33. const pipeline = await WIKI.db.renderers.getRenderingPipeline(page.contentType)
  34. for (const core of pipeline) {
  35. const { render } = (await import(`../../modules/rendering/${core.key}/renderer.mjs`))
  36. output = await render.call({
  37. config: core.config,
  38. children: core.children,
  39. page,
  40. site,
  41. input: output
  42. })
  43. }
  44. // Parse TOC
  45. // const $ = cheerio.load(output)
  46. // let isStrict = $('h1').length > 0 // <- Allows for documents using H2 as top level
  47. let toc = { root: [] }
  48. // $('h1,h2,h3,h4,h5,h6').each((idx, el) => {
  49. // const depth = toSafeInteger(el.name.substring(1)) - (isStrict ? 1 : 2)
  50. // let leafPathError = false
  51. // const leafPath = reduce(times(depth), (curPath, curIdx) => {
  52. // if (has(toc, curPath)) {
  53. // const lastLeafIdx = get(toc, curPath).length - 1
  54. // if (lastLeafIdx >= 0) {
  55. // curPath = `${curPath}[${lastLeafIdx}].children`
  56. // } else {
  57. // leafPathError = true
  58. // }
  59. // }
  60. // return curPath
  61. // }, 'root')
  62. // if (leafPathError) { return }
  63. // const leafSlug = $('.toc-anchor', el).first().attr('href')
  64. // $('.toc-anchor', el).remove()
  65. // get(toc, leafPath).push({
  66. // label: $(el).text().trim(),
  67. // key: leafSlug.substring(1),
  68. // children: []
  69. // })
  70. // })
  71. // Save to DB
  72. await WIKI.db.pages.query()
  73. .patch({
  74. render: output,
  75. toc: JSON.stringify(toc.root)
  76. })
  77. .where('id', payload.id)
  78. // Save to cache
  79. // await WIKI.db.pages.savePageToCache({
  80. // ...page,
  81. // render: output,
  82. // toc: JSON.stringify(toc.root)
  83. // })
  84. WIKI.logger.info(`Rendered page ${payload.id}: [ COMPLETED ]`)
  85. } catch (err) {
  86. WIKI.logger.error(`Rendering page ${payload.id}: [ FAILED ]`)
  87. WIKI.logger.error(err.message)
  88. throw err
  89. }
  90. }