Skip to content

Commit

Permalink
feat: add support for externally defined redirects
Browse files Browse the repository at this point in the history
  • Loading branch information
AdriandeCita committed Dec 12, 2022
1 parent f193f2f commit 5c9a7bc
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/_starter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const start = async () => {
pathToCache: `${baseDir}/cache/`,
sourceLocale: 'en-US',
targetLocale: 'uk',
redirectMap: {},
});

await runner.init();
Expand Down
17 changes: 15 additions & 2 deletions src/registry/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { promises as fs } from 'fs';

import parseFrontMatter from 'gray-matter';
import { Root } from 'rehype-parse/lib';

import {
createHtmlParser,
Expand All @@ -20,6 +21,7 @@ import findHeadings from './utils/find-headings';
import findReferences from './utils/find-references';
import { getNewCommits } from './utils/git-commit-data';
import walk from './utils/walk';
import rewriteRedirects from './utils/rewrite-redirects';

/**
* Transforms a list of paths to content files
Expand Down Expand Up @@ -71,6 +73,7 @@ export interface RegistryInitOptions {
pathToOriginalContent: string;
targetLocale: string;
pathToLocalizedContent: string;
redirectMap?: Record<string, string>;
}

class Registry {
Expand Down Expand Up @@ -502,8 +505,9 @@ class Registry {

const linkedContentAst = await mdParseAndProcess.run(parsedInput);
const processedRehypeAst = await htmlProcess.run(linkedContentAst as any); // TODO: type AST transformations
const postProcessedAst = this.postProcessHtmlContent(processedRehypeAst);

return htmlProcess.stringify(processedRehypeAst as any);
return htmlProcess.stringify(postProcessedAst);
};

// TODO: seems unused now
Expand All @@ -512,9 +516,18 @@ class Registry {

const linkedContentAst = await htmlParseAndProcess.run(parsedInputAst);
const processedHtmlAst = await htmlProcess.run(linkedContentAst);
const postProcessedAst = this.postProcessHtmlContent(processedHtmlAst);

return htmlProcess.stringify(processedHtmlAst);
return htmlProcess.stringify(postProcessedAst);
}

postProcessHtmlContent = (ast: Root): Root => {
const { redirectMap } = this._options;

rewriteRedirects({ redirectMap })(ast);

return ast;
};
}

export default Registry;
28 changes: 28 additions & 0 deletions src/registry/utils/rewrite-redirects.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { visit } from 'unist-util-visit';
import { HtmlNode } from './interfaces';

interface Options {
redirectMap?: Record<string, string>;
}

const rewriteRedirects = (options: Options) => {
const { redirectMap = {} } = options;

const references = Object.keys(redirectMap);

return (tree) => {
visit(
tree,
(node: HtmlNode) => !!(node.tagName === 'a' && node.properties?.href),
(node: HtmlNode) => {
const href = node.properties?.href;

if (references.length && redirectMap[href]) {
node.properties.href = redirectMap[href];
}
},
);
};
};

export default rewriteRedirects;

0 comments on commit 5c9a7bc

Please sign in to comment.