Skip to content

Commit 44b2068

Browse files
authored
Merge pull request #23 from qiancai/fix-github-mention-postprocess
markdown-translator: post-process GitHub mention links after JA translation
2 parents 78f62c3 + e4fc137 commit 44b2068

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import * as fs from "fs";
2+
3+
const GITHUB_MENTION_LINK_REGEX =
4+
/@\s*\[[^\]]*\]\(https?:\/\/github\.com\/([^)\s\/]+)\/?\)/g;
5+
6+
export const fixMarkdownGithubMentions = (markdown) =>
7+
markdown.replaceAll(GITHUB_MENTION_LINK_REGEX, "@[$1](https://github.com/$1)");
8+
9+
export const postProcessFileGithubMentions = (filePath) => {
10+
if (!fs.existsSync(filePath)) return;
11+
12+
const content = fs.readFileSync(filePath, "utf8");
13+
const updatedContent = fixMarkdownGithubMentions(content);
14+
15+
if (updatedContent !== content) {
16+
fs.writeFileSync(filePath, updatedContent);
17+
}
18+
};

markdown-translator/src/index_ja.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { visit } from "unist-util-visit";
2323
import { getMdFileList, writeFileSync, handleAstNode } from "./lib.js";
2424
import { loadVariables, variablesReplace } from "./variables.js";
2525
import { postProcessFileFrontmatterAliases } from "./frontmatterAliases.js";
26+
import { postProcessFileGithubMentions } from "./GithubMentions.js";
2627

2728
const pSum = {
2829
sum: 0,
@@ -128,6 +129,7 @@ const main = async (dir = "markdowns", outputDir = "output") => {
128129
replaceDeprecatedContent(filePath);
129130
await translateSingleMdToJa(filePath, outputFilePath);
130131
postProcessFileFrontmatterAliases(outputFilePath, "ja");
132+
postProcessFileGithubMentions(outputFilePath);
131133
// break;
132134
}
133135

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import test from "node:test";
2+
import assert from "node:assert/strict";
3+
import fs from "node:fs";
4+
import os from "node:os";
5+
import path from "node:path";
6+
7+
import {
8+
postProcessFileGithubMentions,
9+
fixMarkdownGithubMentions,
10+
} from "../src/GithubMentions.js";
11+
12+
test("fixMarkdownGithubMentions: replaces github profile mention links", () => {
13+
const input = "Thanks @ [Aki](https://github.com/aki).\n";
14+
const expected = "Thanks @ [aki](https://github.com/aki).\n";
15+
16+
assert.equal(fixMarkdownGithubMentions(input), expected);
17+
});
18+
19+
test("fixMarkdownGithubMentions: leaves non-github links unchanged", () => {
20+
const input = "Thanks @ [Aki](https://example.com/aki).\n";
21+
assert.equal(fixMarkdownGithubMentions(input), input);
22+
});
23+
24+
test("fixMarkdownGithubMentions: handles multiple mentions in one line", () => {
25+
const input =
26+
"Reviewers: @ [Alpha](https://github.com/alpha), @ [Beta](https://github.com/beta/)\n";
27+
const expected =
28+
"Reviewers: @ [alpha](https://github.com/alpha), @ [beta](https://github.com/beta)\n";
29+
assert.equal(fixMarkdownGithubMentions(input), expected);
30+
});
31+
32+
test("postProcessFileGithubMentions: updates file in-place", () => {
33+
const tempDir = fs.mkdtempSync(
34+
path.join(os.tmpdir(), "github-mentions-postprocess-")
35+
);
36+
37+
try {
38+
const filePath = path.join(tempDir, "doc.md");
39+
fs.writeFileSync(
40+
filePath,
41+
"Reviewers: @ [Alpha](https://github.com/alpha), @ [Beta](https://github.com/beta/)\n"
42+
);
43+
44+
postProcessFileGithubMentions(filePath);
45+
46+
const updated = fs.readFileSync(filePath, "utf8");
47+
assert.equal(
48+
updated,
49+
"Reviewers: @ [alpha](https://github.com/alpha), @ [beta](https://github.com/beta)\n"
50+
);
51+
52+
assert.doesNotThrow(() =>
53+
postProcessFileGithubMentions(path.join(tempDir, "missing.md"))
54+
);
55+
} finally {
56+
fs.rmSync(tempDir, { recursive: true, force: true });
57+
}
58+
});

0 commit comments

Comments
 (0)