Skip to content

Commit b61b45a

Browse files
fix(migrate): edit url
1 parent 19fa062 commit b61b45a

File tree

1 file changed

+56
-40
lines changed

1 file changed

+56
-40
lines changed

migrate/migrate-bot.js

Lines changed: 56 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ async function retry(fn, retries = 3, delay = 1000) {
4141
throw lastError;
4242
}
4343

44-
4544
function extractLink(title) {
4645
const urlRegex = /https?:\/\/.*?cppreference\.com\/w\/[^\s]+/g;
4746
const match = title.match(urlRegex);
@@ -123,49 +122,67 @@ ${html}
123122
console.log("Raw content:", content);
124123

125124
if (content.includes("```mdx")) {
126-
content = content.slice(content.indexOf("```mdx") + 6, content.lastIndexOf("```")).trim();
125+
content = content
126+
.slice(content.indexOf("```mdx") + 6, content.lastIndexOf("```"))
127+
.trim();
127128
}
128129

129130
// Auto Import
130131
const components = [
131-
'Behavior',
132-
'Decl',
133-
'DeclDoc',
134-
'DescList',
135-
'Desc',
136-
'ParamDocList',
137-
'ParamDoc',
138-
'DocLink',
139-
'CHeader',
140-
'CppHeader',
141-
'FeatureTestMacro',
142-
'FeatureTestMacroValue',
143-
'DR',
144-
'DRList',
145-
'Revision',
146-
'RevisionBlock',
147-
'AutoCollapse',
148-
'FlexTable',
149-
'WG21PaperLink',
150-
]
151-
152-
const usedComponents = components.filter((comp) => content.includes(`<${comp} `) || content.includes(`<${comp}>`));
132+
"Behavior",
133+
"Decl",
134+
"DeclDoc",
135+
"DescList",
136+
"Desc",
137+
"ParamDocList",
138+
"ParamDoc",
139+
"DocLink",
140+
"CHeader",
141+
"CppHeader",
142+
"FeatureTestMacro",
143+
"FeatureTestMacroValue",
144+
"DR",
145+
"DRList",
146+
"Revision",
147+
"RevisionBlock",
148+
"AutoCollapse",
149+
"FlexTable",
150+
"WG21PaperLink",
151+
];
152+
153+
const usedComponents = components.filter(
154+
(comp) => content.includes(`<${comp} `) || content.includes(`<${comp}>`),
155+
);
153156

154157
// Remove all existing import statements
155-
content = content.split('\n').filter(line => !line.startsWith('import ')).join('\n');
158+
content = content
159+
.split("\n")
160+
.filter((line) => !line.startsWith("import "))
161+
.join("\n");
156162

157163
// Sort used components alphabetically
158164
usedComponents.sort();
159165

160166
if (usedComponents.length > 0) {
161-
const importStatements = `import { ${usedComponents.join(', ')} } from '@components/index';\n\n`;
167+
const importStatements = `import { ${usedComponents.join(", ")} } from '@components/index';\n\n`;
162168
content = importStatements + content;
163169
}
164170

165171
// Verify content
166-
let normalElements = ["<div", "<section", "<span", "<table", "<thead", "<tbody", "<tr", "<td", "<th"], normalElementsCount = 0;
172+
let normalElements = [
173+
"<div",
174+
"<section",
175+
"<span",
176+
"<table",
177+
"<thead",
178+
"<tbody",
179+
"<tr",
180+
"<td",
181+
"<th",
182+
],
183+
normalElementsCount = 0;
167184
for (const elem of normalElements) {
168-
normalElementsCount += (content.match(new RegExp(elem, 'g')) || []).length;
185+
normalElementsCount += (content.match(new RegExp(elem, "g")) || []).length;
169186
}
170187

171188
console.log(`Normal HTML elements count: ${normalElementsCount}`);
@@ -177,20 +194,20 @@ ${html}
177194
return content;
178195
}
179196

180-
function getLocalPath(url) {
181-
// https://en.cppreference.com/w/cpp/comments.html -> src/content/docs/cpp/comments.mdx
197+
// https://cppreference.com/w/cpp/comments => src/content/docs/cpp/comments.mdx
198+
function getRelativePath(url) {
182199
const match = url.match(/https?:\/\/.*?cppreference\.com\/w\/(.+)\.html$/);
183200
if (!match) {
184201
throw new Error(`无法从URL解析路径: ${url}`);
185202
}
186203
const relative = match[1]; // "cpp/comments"
204+
return `src/content/docs/${relative}.mdx`;
205+
}
206+
207+
function getLocalPath(url) {
187208
return path.join(
188209
__dirname,
189-
"..",
190-
"src",
191-
"content",
192-
"docs",
193-
`${relative}.mdx`,
210+
"..", getRelativePath(url)
194211
);
195212
}
196213

@@ -207,12 +224,11 @@ description: Auto‑generated from cppreference
207224

208225
async function createPullRequest(issue, filePath, url) {
209226
const branchName = `migrate/${issue.number}-${Date.now().toString(36)}`;
210-
const commitMessage = `Migrate ${url}`;
211-
const prTitle = `feat: migrate ${url.split('/w/').pop().replace('.html', '')} from cppref [#${issue.number}]`;
212-
const prBody =
213-
`自动迁移自 ${url}
227+
const prTitle = `feat: migrate ${url.split("/w/").pop().replace(".html", "")} from cppref [#${issue.number}]`;
228+
const commitMessage = prTitle;
229+
const prBody = `自动迁移自 ${url}
214230
215-
[编辑 ${filePath.split("/").slice(-3).join("/")}](https://github.com/cppdoc-cc/cppdoc/edit/${branchName}/${filePath})
231+
[编辑 ${getRelativePath(url)}](https://github.com/cppdoc-cc/cppdoc/edit/${branchName}/${getRelativePath(url)})
216232
217233
<small>Close #${issue.number}</small>
218234
`;

0 commit comments

Comments
 (0)