renderer.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. const _ = require('lodash')
  2. const cheerio = require('cheerio')
  3. /* global WIKI */
  4. module.exports = {
  5. async render() {
  6. const $ = cheerio.load(this.input)
  7. if ($.root().children().length < 1) {
  8. return ''
  9. }
  10. for (let child of this.children) {
  11. const renderer = require(`../${_.kebabCase(child.key)}/renderer.js`)
  12. renderer.init($, child.config)
  13. }
  14. // --------------------------------
  15. // Detect internal / external links
  16. // --------------------------------
  17. let internalRefs = []
  18. const reservedPrefixes = /^\/[a-z]\//gi
  19. const isHostSet = WIKI.config.host.length > 7 && WIKI.config.host !== 'http://'
  20. if (!isHostSet) {
  21. WIKI.logger.warn('Host is not set. You must set the Site Host under General in the Administration Area!')
  22. }
  23. $('a').each((i, elm) => {
  24. let href = $(elm).attr('href')
  25. // -> Ignore empty links
  26. if (!href || href.length < 1) {
  27. return
  28. }
  29. // -> Strip host from local links
  30. if (isHostSet && href.indexOf(WIKI.config.site.host) === 0) {
  31. href = href.replace(WIKI.config.site.host, '')
  32. }
  33. // -> Assign local / external tag
  34. if (href.indexOf('://') < 0) {
  35. // -> Remove trailing slash
  36. if (_.endsWith('/')) {
  37. href = href.slice(0, -1)
  38. }
  39. // -> Check for system prefix
  40. if (!reservedPrefixes.test(href)) {
  41. $(elm).addClass(`is-internal-link`)
  42. // -> Reformat paths
  43. if (href.indexOf('/') !== 0) {
  44. href = `/${this.page.localeCode}/${this.page.path}/${href}`
  45. } else if (href.charAt(3) !== '/') {
  46. href = `/${this.page.localeCode}${href}`
  47. }
  48. // -> Save internal references
  49. internalRefs.push({
  50. localeCode: href.substring(1, 3),
  51. path: _.head(href.substring(4).split('#'))
  52. })
  53. } else {
  54. $(elm).addClass(`is-system-link`)
  55. }
  56. } else {
  57. $(elm).addClass(`is-external-link`)
  58. }
  59. // -> Update element
  60. $(elm).attr('href', href)
  61. })
  62. // --------------------------------
  63. // Detect internal link states
  64. // --------------------------------
  65. if (internalRefs.length > 0) {
  66. // -> Find matching pages
  67. const results = await WIKI.models.pages.query().column('path', 'localeCode').where(builder => {
  68. internalRefs.forEach((ref, idx) => {
  69. if (idx < 1) {
  70. builder.where(ref)
  71. } else {
  72. builder.orWhere(ref)
  73. }
  74. })
  75. })
  76. // -> Apply tag to internal links for found pages
  77. $('a.is-internal-link').each((i, elm) => {
  78. const href = $(elm).attr('href')
  79. const hrefObj = {
  80. localeCode: href.substring(1, 3),
  81. path: _.head(href.substring(4).split('#'))
  82. }
  83. if (_.some(results, r => {
  84. return r.localeCode === hrefObj.localeCode && r.path === hrefObj.path
  85. })) {
  86. $(elm).addClass(`is-valid-page`)
  87. } else {
  88. $(elm).addClass(`is-invalid-page`)
  89. }
  90. })
  91. }
  92. return $.html('body').replace('<body>', '').replace('</body>', '')
  93. }
  94. }