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

allow alternative urls for images #159

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions docs/plugin-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
- [Plugin Options](#plugin-options)
- [url: String](#url-string)
- [verbose: Boolean](#verbose-boolean)
- [alternativeWordpressUrls: String[]](#alternativeWordpressUrls)
- [debug: Object](#debug-object)
- [debug.graphql: Object](#debuggraphql-object)
- [debug.graphql.showQueryVarsOnError: Boolean](#debuggraphqlshowqueryvarsonerror-boolean)
Expand Down Expand Up @@ -63,6 +64,12 @@ Wether there will be verbose output in the terminal. Set to `false` to turn off.
},
```

## alternativeWordpressUrls: String[]

In case the external wordpress url of images / relative links does not match to urls within the database
this all image `src` tags with a base url of one of the provided URLs will be treated as if they exist
on the main wordpress url, see [url](##url).

## debug: Object

An object which contains options related to debugging. See below for options.
Expand Down
44 changes: 38 additions & 6 deletions src/steps/source-nodes/create-nodes/process-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,20 @@ const dbIdToMediaItemRelayId = (dbId) => (dbId ? btoa(`post:${dbId}`) : null)
const getCheerioImgRelayId = (cheerioImg) =>
dbIdToMediaItemRelayId(getCheerioImgDbId(cheerioImg))

export const ensureSrcHasHostname = ({ src, wpUrl }) => {
export const ensureSrcHasHostname = ({ src, wpUrl, alternativeWordpressUrls }) => {
const { protocol, host } = url.parse(wpUrl)

if (src.startsWith(`/`)) {
src = `${protocol}//${host}${src}`
} else if (alternativeWordpressUrls) {
// check all possible wp root urls and replace it with the actual wp url
for (const altWpUrl of alternativeWordpressUrls) {
if (src.startsWith(altWpUrl)) {
// remove the prefix and replace it with the real wp url
src = `${protocol}//${host}` + src.substring(altWpUrl.length)
break
}
}
}

return src
Expand All @@ -105,6 +114,7 @@ const fetchNodeHtmlImageMediaItemNodes = async ({
let src = ensureSrcHasHostname({
src: cheerioImg.attribs.src,
wpUrl,
alternativeWordpressUrls: pluginOptions.alternativeWordpressUrls
})

return src
Expand Down Expand Up @@ -167,6 +177,7 @@ const fetchNodeHtmlImageMediaItemNodes = async ({
const htmlImgSrc = ensureSrcHasHostname({
src: cheerioImg.attribs.src,
wpUrl,
pluginOptions.alternativeWordpressUrls
})

const possibleHtmlSrcs = [
Expand Down Expand Up @@ -346,13 +357,24 @@ const replaceNodeHtmlImages = async ({
).filter(({ match, subMatches }) => {
// @todo make it a plugin option to fetch non-wp images
// here we're filtering out image tags that don't contain our site url
const isHostedInWp =
let isHostedInWp =
// if it has the full WP url
match.includes(wpHostname) ||
// or it's an absolute path
subMatches[0].includes('src=\\"/')

return isHostedInWp
const alternativeWpUrls = pluginOptions.alternativeWordpressUrls
if (!isHostedInWp && alternativeWpUrls) {
for (const altWpUrl of alternativeWpUrls) {
const { hostname: altWpHostname } = url.parse(altWpUrl)
if (match.includes(altWpHostname)) {
isHostedInWp = true
break
}
}
}

return isHostedInWp;
})

if (imageUrlMatches.length && imgTagMatches.length) {
Expand Down Expand Up @@ -570,13 +592,23 @@ const replaceNodeHtmlImages = 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, alternativeWordpressUrls }) => {
const wpLinkRegex = new RegExp(
`["']${wpUrl}(?!/wp-content|/wp-admin|/wp-includes)(/[^'"]+)["']`,
`gim`
)

const linkMatches = execall(wpLinkRegex, nodeString)
let linkMatches = execall(wpLinkRegex, nodeString)
if (alternativeWordpressUrls) {
for (const altWpUrl of alternativeWordpressUrls) {
const wpLinkRegex2 = new RegExp(
`["']${altWpUrl}(?!/wp-content|/wp-admin|/wp-includes)(/[^'"]+)["']`,
`gim`
)
const additionalLinkMatches = execall(wpLinkRegex2, nodeString)
linkMatches.push(...additionalLinkMatches)
}
}

if (linkMatches.length) {
linkMatches.forEach(({ match, subMatches: [path] }) => {
Expand All @@ -599,7 +631,7 @@ const replaceNodeHtmlLinks = ({ wpUrl, nodeString, node }) => {
)
}
}
})
});
}

return nodeString
Expand Down