fix: handle link reformatting when not using namespacing + 5 letters locales support (#1165, #1166)

pull/1173/head
NGPixel 5 years ago
parent 76b97e9f94
commit d78127dc29

@ -1,6 +1,8 @@
const _ = require('lodash') const _ = require('lodash')
const cheerio = require('cheerio') const cheerio = require('cheerio')
const uslug = require('uslug') const uslug = require('uslug')
const pageHelper = require('../../../helpers/page')
const URL = require('url').URL
/* global WIKI */ /* global WIKI */
@ -23,6 +25,7 @@ module.exports = {
let internalRefs = [] let internalRefs = []
const reservedPrefixes = /^\/[a-z]\//gi const reservedPrefixes = /^\/[a-z]\//gi
const exactReservedPaths = /^\/[a-z]$/gi
const isHostSet = WIKI.config.host.length > 7 && WIKI.config.host !== 'http://' const isHostSet = WIKI.config.host.length > 7 && WIKI.config.host !== 'http://'
if (!isHostSet) { if (!isHostSet) {
@ -50,21 +53,44 @@ module.exports = {
} }
// -> Check for system prefix // -> Check for system prefix
if (!reservedPrefixes.test(href)) { if (!reservedPrefixes.test(href) && !exactReservedPaths.test(href)) {
$(elm).addClass(`is-internal-link`) let pagePath = null
// -> Reformat paths // -> Add locale prefix if using namespacing
if (href.indexOf('/') !== 0) { if (WIKI.config.lang.namespacing) {
href = `/${this.page.localeCode}/${this.page.path}/${href}` // -> Reformat paths
} else if (href.charAt(3) !== '/') { if (href.indexOf('/') !== 0) {
href = `/${this.page.localeCode}${href}` href = `/${this.page.localeCode}/${this.page.path}/${href}`
} 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) {
href = `/${this.page.path}/${href}`
}
try {
const parsedUrl = new URL(`http://x${href}`)
pagePath = pageHelper.parsePath(parsedUrl.pathname)
} catch (err) {
return
}
} }
// -> Save internal references // -> Save internal references
internalRefs.push({ internalRefs.push({
localeCode: href.substring(1, 3), localeCode: pagePath.locale,
path: _.head(href.substring(4).split('#')) path: pagePath.path
}) })
$(elm).addClass(`is-internal-link`)
} else { } else {
$(elm).addClass(`is-system-link`) $(elm).addClass(`is-system-link`)
} }
@ -97,12 +123,15 @@ module.exports = {
// -> Apply tag to internal links for found pages // -> Apply tag to internal links for found pages
$('a.is-internal-link').each((i, elm) => { $('a.is-internal-link').each((i, elm) => {
const href = $(elm).attr('href') const href = $(elm).attr('href')
const hrefObj = { let hrefObj = {}
localeCode: href.substring(1, 3), try {
path: _.head(href.substring(4).split('#')) const parsedUrl = new URL(`http://x${href}`)
hrefObj = pageHelper.parsePath(parsedUrl.pathname)
} catch (err) {
return
} }
if (_.some(results, r => { if (_.some(results, r => {
return r.localeCode === hrefObj.localeCode && r.path === hrefObj.path return r.localeCode === hrefObj.locale && r.path === hrefObj.path
})) { })) {
$(elm).addClass(`is-valid-page`) $(elm).addClass(`is-valid-page`)
} else { } else {

Loading…
Cancel
Save