Skip to content

Commit

Permalink
Revert "refactor: remove manual redirects fix"
Browse files Browse the repository at this point in the history
This reverts commit 1de56ca.
We want to keep this around and run it if its needed
due to regressions like openzim/zim-tools#227
  • Loading branch information
lidel committed Feb 28, 2021
1 parent 819c05c commit a52d9c9
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/site-transforms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,34 @@ export const fixFavicon = ({
cli.action.stop()
}


// Fix any broken redirects (https://github.com/openzim/zim-tools/issues/224)
export const fixRedirects = async ({
unpackedZimDir,
wikiFolder
}: Directories) => {
const done = `${unpackedZimDir}/redirects_fixed`
if (existsSync(done)) {
return
}

cli.action.start(' Fixing redirects ')
const fixupLog = `${unpackedZimDir}_redirect-fixups.log`
if (existsSync(fixupLog)) {
unlinkSync(fixupLog)
}
const output = process.env.DEBUG ? `>> ${fixupLog}` : '> /dev/null'
const util = require('util')
const exec = util.promisify(require('child_process').exec)
// redirect files are smaller than 1k so we can skip bigger ones, making the performance acceptable
const findRedirects = String.raw`find ${wikiFolder} -type f -size -800c -exec fgrep -l "0;url=A/" {} + -exec sed -i "s|0;url=A/|0;url=|" {} + ${output} || true`
const { stdout, stderr } = await exec(findRedirects, {env: {'LC_ALL': 'C'}})
if (!stderr) closeSync(openSync(done, 'w'))
cli.action.stop()
if (stdout) console.log('redirect fix stdout:', stdout)
if (stderr) console.error('redirect fix stderr:', stderr)
}

// https://github.com/ipfs/distributed-wikipedia-mirror/issues/80
export const fixExceptions = async ({
unpackedZimDir,
Expand Down
2 changes: 2 additions & 0 deletions src/zim-to-website.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
includeSourceZim,
copyImageAssetsIntoWiki,
fixFavicon,
fixRedirects,
fixExceptions,
// generateMainPage,
insertIndexRedirect,
Expand Down Expand Up @@ -49,6 +50,7 @@ export const zimToWebsite = async (options: Options) => {
fixFavicon(directories)
moveArticleFolderToWiki(directories)
await fixExceptions(directories)
await fixRedirects(directories)
insertIndexRedirect(options)
appendJavascript(options, directories)
// usually main page is ok, so we dont need below
Expand Down

0 comments on commit a52d9c9

Please sign in to comment.