Skip to content

Commit 4d84515

Browse files
committed
Correct the logic of page acquisition
1 parent 6ae0429 commit 4d84515

File tree

2 files changed

+38
-29
lines changed

2 files changed

+38
-29
lines changed

src/export_command/wikimedia_function/bot.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,5 @@ export async function logout(): Promise<void> {
5151
console.log(result);
5252
// clear bot
5353
bot = undefined;
54-
vscode.window.showInformationMessage("result: \"Success\"");
54+
vscode.window.showInformationMessage('result: "Success"');
5555
}

src/export_command/wikimedia_function/core.ts

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ import { OldTokensConvert, OldTokensResult } from '../../interface_definition/ol
1212
import { bot } from './bot';
1313
import { TokensConvert, TokensResult } from '../../interface_definition/tokensInteface';
1414

15+
/**
16+
*
17+
*/
1518
export enum InfoType {
1619
PageTitle = "PageTitle",
1720
PageID = "PageID",
@@ -24,22 +27,27 @@ export interface IPageInfos {
2427
[key: string]: string | undefined;
2528
}
2629

27-
export function getContentInfo(content: string): { content: string, info: IPageInfos | null } {
30+
interface IContentInfo {
31+
content: string;
32+
info?: IPageInfos;
33+
}
34+
35+
export function getContentInfo(content: string): IContentInfo {
2836
const info: string | undefined = content.match(/(?<=\<%\-\-\s*\[PAGE_INFO\])[\s\S]*?(?=\[END_PAGE_INFO\]\s*\-\-%\>)/)?.[0];
2937

30-
let pageInfo: IPageInfos | null = null;
38+
let pageInfo: IPageInfos | undefined = undefined;
3139
if (info) {
3240
content = content.replace(/\<%\-\-\s*\[PAGE_INFO\][\s\S]*?\[END_PAGE_INFO\]\s*\-\-%\>\s*/, "");
33-
const foo = (infoName: string): string | undefined => {
41+
const getInfo = (infoName: string): string | undefined => {
3442
const reg = new RegExp(`(?<=${infoName}\\s*=\\s*#).*?(?=#)`);
3543
return info.match(reg)?.[0];
3644
};
3745
pageInfo = {
38-
PageTitle: foo(InfoType.PageTitle),
39-
PageID: foo(InfoType.PageID),
40-
RevisionID: foo(InfoType.RevisionID),
41-
ContentModel: foo(InfoType.ContentModel),
42-
ContentFormat: foo(InfoType.ContentFormat)
46+
PageTitle: getInfo(InfoType.PageTitle),
47+
PageID: getInfo(InfoType.PageID),
48+
RevisionID: getInfo(InfoType.RevisionID),
49+
ContentModel: getInfo(InfoType.ContentModel),
50+
ContentFormat: getInfo(InfoType.ContentFormat)
4351
};
4452
}
4553

@@ -55,7 +63,7 @@ export async function writePage(): Promise<void> {
5563
let args: any;
5664
let result: any;
5765
let token: string | undefined;
58-
let errors: any[] = [{}, {}];
66+
let errors: any[] = [undefined, undefined];
5967

6068
try {
6169
args = {
@@ -74,22 +82,23 @@ export async function writePage(): Promise<void> {
7482
console.log(error);
7583
errors[0] = error;
7684
}
77-
78-
try {
79-
args = {
80-
'action': "tokens",
81-
'type': "edit"
82-
};
83-
result = await bot.request(args);
84-
const reOld: OldTokensResult = OldTokensConvert.toOldTokensResult(result);
85-
token = reOld.tokens?.edittoken;
86-
if (token) {
87-
return token;
85+
if (errors[0] !== undefined) {
86+
try {
87+
args = {
88+
'action': "tokens",
89+
'type': "edit"
90+
};
91+
result = await bot.request(args);
92+
const reOld: OldTokensResult = OldTokensConvert.toOldTokensResult(result);
93+
token = reOld.tokens?.edittoken;
94+
if (token) {
95+
return token;
96+
}
97+
}
98+
catch (error) {
99+
console.log(error);
100+
errors[1] = error;
88101
}
89-
}
90-
catch (error) {
91-
console.log(error);
92-
errors[1] = error;
93102
}
94103

95104
throw new Error(`Could not get edit token: NEW: ${errors[0].name}; OLD: ${errors[1].name}`);
@@ -106,7 +115,7 @@ export async function writePage(): Promise<void> {
106115
return undefined;
107116
}
108117

109-
const contentInfo: { content: string, info: IPageInfos | null } = getContentInfo(wikiContent);
118+
const contentInfo: IContentInfo = getContentInfo(wikiContent);
110119
console.log(contentInfo);
111120

112121
const wikiTitle: string | undefined = await vscode.window.showInputBox({
@@ -217,9 +226,9 @@ export async function readPage(): Promise<void> {
217226
const normalized: Jump | undefined = re.query?.normalized?.[0];
218227
const redirects: Jump | undefined = re.query?.redirects?.[0];
219228
vscode.window.showInformationMessage(
220-
`Opened page "${page.title}" with Model ${content?.contentmodel}.` +
221-
(normalized ? ` Normalized: "${normalized.from}" => "${normalized.to}".` : "") +
222-
(redirects ? ` Redirect: "${redirects.from}" => "${redirects.to}"` : "")
229+
`Opened page "${page.title}" with Model ${content?.contentmodel}. ` +
230+
`Normalized: ${normalized ? `${normalized.from} => ${normalized.to}` : undefined}. ` +
231+
`Redirect: ${redirects ? `${redirects.from} => ${redirects.to}` : undefined}`
223232
);
224233

225234
const infoHead: string =

0 commit comments

Comments
 (0)