Skip to content

Commit

Permalink
fix: redirects converted from _exceptions
Browse files Browse the repository at this point in the history
Closes #89
  • Loading branch information
lidel committed Mar 9, 2021
1 parent e6d82dc commit ec01bdc
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
28 changes: 28 additions & 0 deletions src/article-transforms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,31 @@ export const reworkScriptSrcs = (
attribs.src = src
}
}

export const reworkRedirect = (
$html: any,
selector = 'meta[http-equiv="refresh"]',
fns: ((href: string) => string)[] = [
moveRelativeLinksDownOneLevel
]
) => {
const redirects = $html(selector)

for (const redirect of Object.values<any>(redirects)) {
const attribs = redirect.attribs

if (!attribs || !attribs.content) {
continue
}

let { content } = attribs

let [ delay, url ] = content.split(';url=')

for (const fn of fns) {
url = fn(url)
}

attribs.content = `${delay};url=${url}`
}
}
4 changes: 3 additions & 1 deletion src/site-transforms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ import {
moveRelativeLinksDownOneLevel,
prefixRelativeRoot,
reworkLinks,
reworkScriptSrcs
reworkScriptSrcs,
reworkRedirect
} from './article-transforms'
import { Directories, Options } from './domain'
import { downloadFile } from './utils/download-file'
Expand Down Expand Up @@ -151,6 +152,7 @@ export const fixExceptions = async ({
reworkLinks($fileHtml, 'link[href^="../"]', linkFixups)
reworkScriptSrcs($fileHtml, 'img', linkFixups)
reworkScriptSrcs($fileHtml, 'script', linkFixups)
reworkRedirect($fileHtml, 'meta[http-equiv="refresh"]', linkFixups)

// console.log(` fixed relative paths in ${filePath}`)
// renameSync(filePath, `${filePath}.original`)
Expand Down

0 comments on commit ec01bdc

Please sign in to comment.