2018-09-16 04:35:03 +00:00
|
|
|
const _ = require('lodash')
|
|
|
|
const cheerio = require('cheerio')
|
2019-10-28 01:08:42 +00:00
|
|
|
const uslug = require('uslug')
|
2019-10-31 04:07:37 +00:00
|
|
|
const pageHelper = require('../../../helpers/page')
|
|
|
|
const URL = require('url').URL
|
2018-09-16 04:35:03 +00:00
|
|
|
|
2020-06-14 01:43:21 +00:00
|
|
|
const mustacheRegExp = /(\{|{?){2}(.+?)(\}|}?){2}/i
|
|
|
|
|
2019-09-02 19:33:40 +00:00
|
|
|
/* global WIKI */
|
|
|
|
|
2018-09-10 00:33:10 +00:00
|
|
|
module.exports = {
|
|
|
|
async render() {
|
2020-01-03 06:07:29 +00:00
|
|
|
const $ = cheerio.load(this.input, {
|
2020-03-21 03:23:13 +00:00
|
|
|
decodeEntities: true
|
2020-01-03 06:07:29 +00:00
|
|
|
})
|
2018-09-16 04:35:03 +00:00
|
|
|
|
|
|
|
if ($.root().children().length < 1) {
|
|
|
|
return ''
|
|
|
|
}
|
|
|
|
|
2019-12-12 04:35:54 +00:00
|
|
|
// --------------------------------
|
|
|
|
// STEP: PRE
|
|
|
|
// --------------------------------
|
|
|
|
|
|
|
|
for (let child of _.reject(this.children, ['step', 'post'])) {
|
2018-09-16 04:35:03 +00:00
|
|
|
const renderer = require(`../${_.kebabCase(child.key)}/renderer.js`)
|
2020-06-18 22:39:25 +00:00
|
|
|
await renderer.init($, child.config)
|
2018-09-16 04:35:03 +00:00
|
|
|
}
|
|
|
|
|
2019-09-02 19:33:40 +00:00
|
|
|
// --------------------------------
|
|
|
|
// Detect internal / external links
|
|
|
|
// --------------------------------
|
|
|
|
|
|
|
|
let internalRefs = []
|
2020-01-02 18:33:27 +00:00
|
|
|
const reservedPrefixes = /^\/[a-z]\//i
|
|
|
|
const exactReservedPaths = /^\/[a-z]$/i
|
2019-09-02 19:33:40 +00:00
|
|
|
|
|
|
|
const isHostSet = WIKI.config.host.length > 7 && WIKI.config.host !== 'http://'
|
|
|
|
if (!isHostSet) {
|
|
|
|
WIKI.logger.warn('Host is not set. You must set the Site Host under General in the Administration Area!')
|
|
|
|
}
|
|
|
|
|
|
|
|
$('a').each((i, elm) => {
|
|
|
|
let href = $(elm).attr('href')
|
|
|
|
|
2019-12-31 19:41:05 +00:00
|
|
|
// -> Ignore empty / anchor links, e-mail addresses, and telephone numbers
|
|
|
|
if (!href || href.length < 1 || href.indexOf('#') === 0 ||
|
|
|
|
href.indexOf('mailto:') === 0 || href.indexOf('tel:') === 0) {
|
2019-09-02 19:33:40 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// -> Strip host from local links
|
2022-04-03 19:48:40 +00:00
|
|
|
if (isHostSet && href.indexOf(`${WIKI.config.host}/`) === 0) {
|
2019-09-09 01:09:46 +00:00
|
|
|
href = href.replace(WIKI.config.host, '')
|
2019-09-02 19:33:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// -> Assign local / external tag
|
|
|
|
if (href.indexOf('://') < 0) {
|
|
|
|
// -> Remove trailing slash
|
|
|
|
if (_.endsWith('/')) {
|
|
|
|
href = href.slice(0, -1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// -> Check for system prefix
|
2019-11-23 21:13:41 +00:00
|
|
|
if (reservedPrefixes.test(href) || exactReservedPaths.test(href)) {
|
|
|
|
$(elm).addClass(`is-system-link`)
|
|
|
|
} else if (href.indexOf('.') >= 0) {
|
|
|
|
$(elm).addClass(`is-asset-link`)
|
|
|
|
} else {
|
2019-10-31 04:07:37 +00:00
|
|
|
let pagePath = null
|
|
|
|
|
|
|
|
// -> Add locale prefix if using namespacing
|
|
|
|
if (WIKI.config.lang.namespacing) {
|
|
|
|
// -> Reformat paths
|
|
|
|
if (href.indexOf('/') !== 0) {
|
2020-01-03 01:30:48 +00:00
|
|
|
if (this.config.absoluteLinks) {
|
|
|
|
href = `/${this.page.localeCode}/${href}`
|
|
|
|
} else {
|
|
|
|
href = (this.page.path === 'home') ? `/${this.page.localeCode}/${href}` : `/${this.page.localeCode}/${this.page.path}/${href}`
|
|
|
|
}
|
2019-10-31 04:07:37 +00:00
|
|
|
} else if (href.charAt(3) !== '/') {
|
|
|
|
href = `/${this.page.localeCode}${href}`
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
const parsedUrl = new URL(`http://x${href}`)
|
|
|
|
pagePath = pageHelper.parsePath(parsedUrl.pathname)
|
|
|
|
} catch (err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// -> Reformat paths
|
|
|
|
if (href.indexOf('/') !== 0) {
|
2020-01-03 01:30:48 +00:00
|
|
|
if (this.config.absoluteLinks) {
|
|
|
|
href = `/${href}`
|
|
|
|
} else {
|
|
|
|
href = (this.page.path === 'home') ? `/${href}` : `/${this.page.path}/${href}`
|
|
|
|
}
|
2019-10-31 04:07:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
const parsedUrl = new URL(`http://x${href}`)
|
|
|
|
pagePath = pageHelper.parsePath(parsedUrl.pathname)
|
|
|
|
} catch (err) {
|
|
|
|
return
|
|
|
|
}
|
2019-09-02 19:33:40 +00:00
|
|
|
}
|
|
|
|
// -> Save internal references
|
|
|
|
internalRefs.push({
|
2019-10-31 04:07:37 +00:00
|
|
|
localeCode: pagePath.locale,
|
|
|
|
path: pagePath.path
|
2019-09-02 19:33:40 +00:00
|
|
|
})
|
2019-10-31 04:07:37 +00:00
|
|
|
|
|
|
|
$(elm).addClass(`is-internal-link`)
|
2019-09-02 19:33:40 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$(elm).addClass(`is-external-link`)
|
2020-02-23 01:21:26 +00:00
|
|
|
if (this.config.openExternalLinkNewTab) {
|
|
|
|
$(elm).attr('target', '_blank')
|
2020-05-07 20:45:11 +00:00
|
|
|
$(elm).attr('rel', this.config.relAttributeExternalLink)
|
2020-02-23 01:21:26 +00:00
|
|
|
}
|
2019-09-02 19:33:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// -> Update element
|
|
|
|
$(elm).attr('href', href)
|
|
|
|
})
|
|
|
|
|
|
|
|
// --------------------------------
|
|
|
|
// Detect internal link states
|
|
|
|
// --------------------------------
|
|
|
|
|
2019-09-03 02:18:59 +00:00
|
|
|
const pastLinks = await this.page.$relatedQuery('links')
|
|
|
|
|
2019-09-02 19:33:40 +00:00
|
|
|
if (internalRefs.length > 0) {
|
|
|
|
// -> Find matching pages
|
2019-09-03 02:18:59 +00:00
|
|
|
const results = await WIKI.models.pages.query().column('id', 'path', 'localeCode').where(builder => {
|
2019-09-02 19:33:40 +00:00
|
|
|
internalRefs.forEach((ref, idx) => {
|
|
|
|
if (idx < 1) {
|
|
|
|
builder.where(ref)
|
|
|
|
} else {
|
|
|
|
builder.orWhere(ref)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
// -> Apply tag to internal links for found pages
|
|
|
|
$('a.is-internal-link').each((i, elm) => {
|
|
|
|
const href = $(elm).attr('href')
|
2019-10-31 04:07:37 +00:00
|
|
|
let hrefObj = {}
|
|
|
|
try {
|
|
|
|
const parsedUrl = new URL(`http://x${href}`)
|
|
|
|
hrefObj = pageHelper.parsePath(parsedUrl.pathname)
|
|
|
|
} catch (err) {
|
|
|
|
return
|
2019-09-02 19:33:40 +00:00
|
|
|
}
|
|
|
|
if (_.some(results, r => {
|
2019-10-31 04:07:37 +00:00
|
|
|
return r.localeCode === hrefObj.locale && r.path === hrefObj.path
|
2019-09-02 19:33:40 +00:00
|
|
|
})) {
|
|
|
|
$(elm).addClass(`is-valid-page`)
|
|
|
|
} else {
|
|
|
|
$(elm).addClass(`is-invalid-page`)
|
|
|
|
}
|
|
|
|
})
|
2019-09-03 02:18:59 +00:00
|
|
|
|
|
|
|
// -> Add missing links
|
|
|
|
const missingLinks = _.differenceWith(internalRefs, pastLinks, (nLink, pLink) => {
|
|
|
|
return nLink.localeCode === pLink.localeCode && nLink.path === pLink.path
|
|
|
|
})
|
|
|
|
if (missingLinks.length > 0) {
|
|
|
|
if (WIKI.config.db.type === 'postgres') {
|
|
|
|
await WIKI.models.pageLinks.query().insert(missingLinks.map(lnk => ({
|
|
|
|
pageId: this.page.id,
|
|
|
|
path: lnk.path,
|
|
|
|
localeCode: lnk.localeCode
|
|
|
|
})))
|
|
|
|
} else {
|
|
|
|
for (const lnk of missingLinks) {
|
|
|
|
await WIKI.models.pageLinks.query().insert({
|
|
|
|
pageId: this.page.id,
|
|
|
|
path: lnk.path,
|
|
|
|
localeCode: lnk.localeCode
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// -> Remove outdated links
|
|
|
|
if (pastLinks) {
|
|
|
|
const outdatedLinks = _.differenceWith(pastLinks, internalRefs, (nLink, pLink) => {
|
|
|
|
return nLink.localeCode === pLink.localeCode && nLink.path === pLink.path
|
|
|
|
})
|
|
|
|
if (outdatedLinks.length > 0) {
|
|
|
|
await WIKI.models.pageLinks.query().delete().whereIn('id', _.map(outdatedLinks, 'id'))
|
|
|
|
}
|
2019-09-02 19:33:40 +00:00
|
|
|
}
|
|
|
|
|
2019-10-28 01:08:42 +00:00
|
|
|
// --------------------------------
|
|
|
|
// Add header handles
|
|
|
|
// --------------------------------
|
|
|
|
|
|
|
|
let headers = []
|
|
|
|
$('h1,h2,h3,h4,h5,h6').each((i, elm) => {
|
2021-10-02 00:22:55 +00:00
|
|
|
let headerSlug = uslug($(elm).text())
|
|
|
|
// -> If custom ID is defined, try to use that instead
|
2019-10-28 01:08:42 +00:00
|
|
|
if ($(elm).attr('id')) {
|
2021-10-02 00:22:55 +00:00
|
|
|
headerSlug = $(elm).attr('id')
|
2019-10-28 01:08:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// -> Cannot start with a number (CSS selector limitation)
|
|
|
|
if (headerSlug.match(/^\d/)) {
|
|
|
|
headerSlug = `h-${headerSlug}`
|
|
|
|
}
|
|
|
|
|
|
|
|
// -> Make sure header is unique
|
|
|
|
if (headers.indexOf(headerSlug) >= 0) {
|
|
|
|
let isUnique = false
|
|
|
|
let hIdx = 1
|
|
|
|
while (!isUnique) {
|
|
|
|
const headerSlugTry = `${headerSlug}-${hIdx}`
|
|
|
|
if (headers.indexOf(headerSlugTry) < 0) {
|
|
|
|
isUnique = true
|
|
|
|
headerSlug = headerSlugTry
|
|
|
|
}
|
|
|
|
hIdx++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// -> Add anchor
|
|
|
|
$(elm).attr('id', headerSlug).addClass('toc-header')
|
|
|
|
$(elm).prepend(`<a class="toc-anchor" href="#${headerSlug}">¶</a> `)
|
|
|
|
|
|
|
|
headers.push(headerSlug)
|
|
|
|
})
|
|
|
|
|
2020-06-14 01:43:21 +00:00
|
|
|
// --------------------------------
|
2021-10-09 18:09:54 +00:00
|
|
|
// Wrap non-empty root text nodes
|
2020-06-14 01:43:21 +00:00
|
|
|
// --------------------------------
|
|
|
|
|
|
|
|
$('body').contents().toArray().forEach(item => {
|
2021-10-09 18:09:54 +00:00
|
|
|
if (item && item.type === 'text' && item.parent.name === 'body' && item.data !== `\n` && item.data !== `\r`) {
|
2020-06-14 01:43:21 +00:00
|
|
|
$(item).wrap('<div></div>')
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2020-05-08 21:00:02 +00:00
|
|
|
// --------------------------------
|
|
|
|
// Escape mustache expresions
|
|
|
|
// --------------------------------
|
|
|
|
|
|
|
|
function iterateMustacheNode (node) {
|
|
|
|
const list = $(node).contents().toArray()
|
|
|
|
list.forEach(item => {
|
2021-04-12 15:39:35 +00:00
|
|
|
if (item && item.type === 'text') {
|
2021-03-26 03:50:59 +00:00
|
|
|
const rawText = $(item).text().replace(/\r?\n|\r/g, '')
|
2020-06-14 01:43:21 +00:00
|
|
|
if (mustacheRegExp.test(rawText)) {
|
2020-05-08 21:00:02 +00:00
|
|
|
$(item).parent().attr('v-pre', true)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
iterateMustacheNode(item)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
iterateMustacheNode($.root())
|
2019-12-12 04:35:54 +00:00
|
|
|
|
2021-03-12 00:23:24 +00:00
|
|
|
$('pre').each((idx, elm) => {
|
|
|
|
$(elm).attr('v-pre', true)
|
|
|
|
})
|
|
|
|
|
2019-12-12 04:35:54 +00:00
|
|
|
// --------------------------------
|
|
|
|
// STEP: POST
|
|
|
|
// --------------------------------
|
|
|
|
|
2020-05-08 21:00:02 +00:00
|
|
|
let output = decodeEscape($.html('body').replace('<body>', '').replace('</body>', ''))
|
|
|
|
|
2020-01-03 06:07:29 +00:00
|
|
|
for (let child of _.sortBy(_.filter(this.children, ['step', 'post']), ['order'])) {
|
2019-12-12 04:35:54 +00:00
|
|
|
const renderer = require(`../${_.kebabCase(child.key)}/renderer.js`)
|
2020-01-03 06:07:29 +00:00
|
|
|
output = await renderer.init(output, child.config)
|
2019-12-12 04:35:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return output
|
2018-09-10 00:33:10 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-21 03:23:13 +00:00
|
|
|
|
|
|
|
function decodeEscape (string) {
|
|
|
|
return string.replace(/&#x([0-9a-f]{1,6});/ig, (entity, code) => {
|
|
|
|
code = parseInt(code, 16)
|
|
|
|
|
|
|
|
// Don't unescape ASCII characters, assuming they're encoded for a good reason
|
|
|
|
if (code < 0x80) return entity
|
|
|
|
|
|
|
|
return String.fromCodePoint(code)
|
|
|
|
})
|
|
|
|
}
|