Skip to content
This repository has been archived by the owner on Apr 20, 2021. It is now read-only.

Commit

Permalink
fix: relative urls inside html (#330)
Browse files Browse the repository at this point in the history
  • Loading branch information
henrikwirth authored Nov 30, 2020
1 parent 0b3fe07 commit 62ae032
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/models/gatsby-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ const defaultPluginOptions = {
// Transforms anchor links, video src's, and audio src's (that point to wp-content files) into local file static links
// Also fetches those files if they don't already exist
createStaticFiles: true,
// If pathPrefix is used, relative links that don't start with the prefix will get prefixed.
// Example: <a href="/relative/url/"></a> will be <a href="/path-prefix/relative/url/">
prefixRelativeUrls: true,
},
type: {
__all: {
Expand Down
42 changes: 40 additions & 2 deletions src/steps/source-nodes/create-nodes/process-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -764,7 +764,7 @@ const replaceFileLinks = async ({
}

// replaces any url which is a front-end WP url with a relative path
const replaceNodeHtmlLinks = ({ wpUrl, nodeString, node }) => {
const replaceNodeHtmlLinks = ({ wpUrl, nodeString, node, helpers, pluginOptions }) => {
const wpLinkRegex = new RegExp(
`["']${wpUrl}(?!/wp-content|/wp-admin|/wp-includes)(/[^'"]+)["']`,
`gim`
Expand All @@ -777,7 +777,10 @@ const replaceNodeHtmlLinks = ({ wpUrl, nodeString, node }) => {
if (path) {
try {
// remove \, " and ' characters from match
const normalizedMatch = match.replace(/['"\\]/g, ``)
const normalizedMatch = match
.replace(/['"\\]/g, ``)
// ensure that query params are properly quoted
.replace(/\?/, `\\?`)

const normalizedPath = path.replace(/\\/g, ``)

Expand All @@ -796,6 +799,41 @@ const replaceNodeHtmlLinks = ({ wpUrl, nodeString, node }) => {
})
}

// early return if no pathPrefix is used or plugin options deactivate behavior
if(!helpers.pathPrefix || !pluginOptions.html.prefixRelativeUrls) return nodeString

// Links inside nodeString look like: <a href=\\"/example\/link/\\">Example</a>
// Includes string starting with href=\\" or href=\" or href=\\' or href=\'
// excludes links starting with the prefix or with double slash (//)
// includes relative links starting with one slash (/)
// groups everything inside href
const relativeLinkRegex = new RegExp(`href=[\\\\]{0,2}["']((?!${helpers.pathPrefix}|/wp-content|/wp-admin|/wp-includes|//)/[^'"]+)[\\\\]{0,2}["']`, `gim`)
const relativeLinkMatches = execall(relativeLinkRegex, nodeString)

if (relativeLinkMatches.length) {
relativeLinkMatches.forEach(({ match, subMatches: [path] }) => {
if (path) {
try {
// remove \, " and ' characters from match
const normalizedPath = path.replace(/\\/g, ``)
// compare lower-cased version of the prefix
const prefixedPath = `${!normalizedPath.toLowerCase().startsWith(helpers.pathPrefix.toLowerCase()) ? helpers.pathPrefix : ``}${normalizedPath}`
// group the surrounding to use in replace $1 and $3
const relativeMatchRegex = new RegExp(`(href=[\\\\]{0,2}["'])((?!${helpers.pathPrefix}|/wp-content|/wp-admin|/wp-includes|//)${normalizedPath})([\\\\]{0,2}["'])`, `g`)
// replace normalized match with prefixed path
nodeString = nodeString.replace(relativeMatchRegex, `$1${prefixedPath}$3`)
} catch (e) {
console.error(e)
console.warning(
formatLogMessage(
`Failed to process relative inline html links in ${node.__typename} ${node.id}`
)
)
}
}
})
}

return nodeString
}

Expand Down

0 comments on commit 62ae032

Please sign in to comment.