feat: Initial clean release of CHM
一些检查失败了
automerge / automerge (push) Has been cancelled
react-native-android-build-apk / AssembleRelease (push) Has been cancelled
Build macOS M1 / Main (push) Has been cancelled
Joplin Continuous Integration / Main (macos-15-intel) (push) Has been cancelled
Joplin Continuous Integration / Main (ubuntu-22.04) (push) Has been cancelled
Joplin Continuous Integration / Main (ubuntu-22.04-arm) (push) Has been cancelled
Joplin Continuous Integration / Main (windows-11-vs2026-arm) (push) Has been cancelled
Joplin Continuous Integration / Main (windows-2025) (push) Has been cancelled
Joplin Continuous Integration / ServerDockerImage (ubuntu-22.04) (push) Has been cancelled
Joplin Continuous Integration / ServerDockerImage (ubuntu-22.04-arm) (push) Has been cancelled
Joplin UI tests / Main (ubuntu-22.04) (push) Has been cancelled
Joplin UI tests / Main (windows-2025) (push) Has been cancelled
Joplin UI tests / Mobile (push) Has been cancelled
Close stale issues / ProcessStaleIssues (push) Has been cancelled
一些检查失败了
automerge / automerge (push) Has been cancelled
react-native-android-build-apk / AssembleRelease (push) Has been cancelled
Build macOS M1 / Main (push) Has been cancelled
Joplin Continuous Integration / Main (macos-15-intel) (push) Has been cancelled
Joplin Continuous Integration / Main (ubuntu-22.04) (push) Has been cancelled
Joplin Continuous Integration / Main (ubuntu-22.04-arm) (push) Has been cancelled
Joplin Continuous Integration / Main (windows-11-vs2026-arm) (push) Has been cancelled
Joplin Continuous Integration / Main (windows-2025) (push) Has been cancelled
Joplin Continuous Integration / ServerDockerImage (ubuntu-22.04) (push) Has been cancelled
Joplin Continuous Integration / ServerDockerImage (ubuntu-22.04-arm) (push) Has been cancelled
Joplin UI tests / Main (ubuntu-22.04) (push) Has been cancelled
Joplin UI tests / Main (windows-2025) (push) Has been cancelled
Joplin UI tests / Mobile (push) Has been cancelled
Close stale issues / ProcessStaleIssues (push) Has been cancelled
这个提交包含在:
@@ -0,0 +1,18 @@
|
||||
import { trimPostToMaximumLength } from './discourse';
|
||||
|
||||
describe('utils/discourse', () => {
|
||||
it('trimPostToMaximumLength should allow trimming posts to a maximum length', () => {
|
||||
const makeLongString = (length: number) => {
|
||||
const resultParts = [];
|
||||
while (resultParts.length < length) {
|
||||
resultParts.push('1');
|
||||
}
|
||||
return resultParts.join('');
|
||||
};
|
||||
|
||||
const longContent = makeLongString(70_000);
|
||||
const trimmed = trimPostToMaximumLength(longContent);
|
||||
expect(trimmed.length).toBeLessThan(65_000);
|
||||
expect(trimmed).toContain(`${longContent.substring(0, 65_000 - 153)}...\n\n**Note**:`);
|
||||
});
|
||||
});
|
||||
160
packages/tools/utils/discourse.ts
普通文件
160
packages/tools/utils/discourse.ts
普通文件
@@ -0,0 +1,160 @@
|
||||
import { substrWithEllipsis } from '@joplin/lib/string-utils';
|
||||
import { msleep } from '@joplin/utils/time';
|
||||
import fetch from 'node-fetch';
|
||||
|
||||
interface ApiConfig {
|
||||
baseUrl: string;
|
||||
key: string;
|
||||
username: string;
|
||||
newsCategoryId: number;
|
||||
}
|
||||
|
||||
interface DiscourseApiError extends Error {
|
||||
apiObject: {
|
||||
errors?: string[];
|
||||
error_type?: string;
|
||||
extras?: { wait_seconds?: number; time_left?: string };
|
||||
} | null;
|
||||
status: number;
|
||||
}
|
||||
|
||||
export enum HttpMethod {
|
||||
GET = 'GET',
|
||||
POST = 'POST',
|
||||
DELETE = 'DELETE',
|
||||
PUT = 'PUT',
|
||||
PATCH = 'PATCH',
|
||||
}
|
||||
|
||||
interface ForumTopPost {
|
||||
id: number;
|
||||
raw: string;
|
||||
title: string;
|
||||
}
|
||||
|
||||
interface ForumTopic {
|
||||
id: number;
|
||||
topic_id: string;
|
||||
}
|
||||
|
||||
export const config: ApiConfig = {
|
||||
baseUrl: 'https://discourse.joplinapp.org',
|
||||
key: '',
|
||||
username: '',
|
||||
newsCategoryId: 9,
|
||||
};
|
||||
|
||||
export const execApi = async (method: HttpMethod, path: string, body: Record<string, string | number> = null) => {
|
||||
interface Request {
|
||||
method: HttpMethod;
|
||||
headers: Record<string, string>;
|
||||
body?: string;
|
||||
}
|
||||
|
||||
const headers: Record<string, string> = {
|
||||
'Api-Key': config.key,
|
||||
'Api-Username': config.username,
|
||||
};
|
||||
|
||||
if (method !== HttpMethod.GET) headers['Content-Type'] = 'application/json;';
|
||||
|
||||
const request: Request = {
|
||||
method,
|
||||
headers,
|
||||
};
|
||||
|
||||
if (body) request.body = JSON.stringify(body);
|
||||
|
||||
while (true) {
|
||||
const response = await fetch(`${config.baseUrl}/${path}`, request);
|
||||
|
||||
if (!response.ok) {
|
||||
const errorText = await response.text();
|
||||
const error = new Error(`On ${method} ${path}: ${errorText}`) as DiscourseApiError;
|
||||
let apiObject: DiscourseApiError['apiObject'] = null;
|
||||
try {
|
||||
apiObject = JSON.parse(errorText);
|
||||
} catch (error) {
|
||||
// Ignore - it just means that the error object is a plain string
|
||||
}
|
||||
|
||||
// {
|
||||
// "errors": [
|
||||
// "You’ve performed this action too many times. Please wait 39 seconds before trying again."
|
||||
// ],
|
||||
// "error_type": "rate_limit",
|
||||
// "extras": {
|
||||
// "wait_seconds": 39,
|
||||
// "time_left": "39 seconds"
|
||||
// }
|
||||
// }
|
||||
|
||||
error.apiObject = apiObject;
|
||||
error.status = response.status;
|
||||
|
||||
if (apiObject?.extras?.wait_seconds) {
|
||||
await msleep(apiObject.extras.wait_seconds * 1000 + 1000);
|
||||
continue;
|
||||
}
|
||||
|
||||
throw error;
|
||||
}
|
||||
|
||||
return response.json();
|
||||
}
|
||||
};
|
||||
|
||||
export const getForumTopPostByExternalId = async (externalId: string): Promise<ForumTopPost> => {
|
||||
try {
|
||||
const existingForumTopic = await execApi(HttpMethod.GET, `t/external_id/${externalId}.json`);
|
||||
const existingForumPost = await execApi(HttpMethod.GET, `posts/${existingForumTopic.post_stream.posts[0].id}.json`);
|
||||
return {
|
||||
id: existingForumPost.id,
|
||||
title: existingForumTopic.title,
|
||||
raw: existingForumPost.raw,
|
||||
};
|
||||
} catch (error) {
|
||||
if (error.status === 404) return null;
|
||||
if (error.apiObject && error.apiObject.error_type === 'not_found') return null;
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
export const getTopicByExternalId = async (externalId: string): Promise<ForumTopic> => {
|
||||
try {
|
||||
const existingForumTopic = await execApi(HttpMethod.GET, `t/external_id/${externalId}.json`);
|
||||
return existingForumTopic;
|
||||
} catch (error) {
|
||||
if (error.status === 404) return null;
|
||||
if (error.apiObject && error.apiObject.error_type === 'not_found') return null;
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
export const createTopic = async (topic: Record<string, string | number>): Promise<ForumTopic> => {
|
||||
return execApi(HttpMethod.POST, 'posts', topic);
|
||||
};
|
||||
|
||||
export const createPost = async (topicId: number, post: Record<string, string | number>): Promise<ForumTopic> => {
|
||||
return execApi(HttpMethod.POST, 'posts', {
|
||||
topic_id: topicId,
|
||||
...post,
|
||||
});
|
||||
};
|
||||
|
||||
export const updatePost = async (postId: number, content: Record<string, string | number>): Promise<void> => {
|
||||
await execApi(HttpMethod.PUT, `posts/${postId}.json`, content);
|
||||
};
|
||||
|
||||
export const trimPostToMaximumLength = (postBody: string) => {
|
||||
// Discourse has a maximum post length of 65_000:
|
||||
const maximumLength = 65_000;
|
||||
if (postBody.length > maximumLength) {
|
||||
return [
|
||||
substrWithEllipsis(postBody, 0, maximumLength - 150),
|
||||
'**Note**: The full content of this post is longer than permitted by Discourse.',
|
||||
].join('\n\n');
|
||||
}
|
||||
|
||||
return postBody;
|
||||
};
|
||||
@@ -0,0 +1,53 @@
|
||||
import { readFile } from 'fs-extra';
|
||||
import { getRootDir } from '@joplin/utils';
|
||||
import { fetchWithRetry } from '@joplin/utils/net';
|
||||
import { githubOauthToken } from '../tool-utils';
|
||||
|
||||
export interface GithubSponsor {
|
||||
name: string;
|
||||
id: string;
|
||||
}
|
||||
|
||||
export interface Sponsors {
|
||||
github: GithubSponsor[];
|
||||
orgs: OrgSponsor[];
|
||||
}
|
||||
|
||||
export interface OrgSponsor {
|
||||
url: string;
|
||||
urlWebsite?: string;
|
||||
title: string;
|
||||
imageName: string;
|
||||
alt?: string;
|
||||
}
|
||||
|
||||
export const loadSponsors = async (): Promise<Sponsors> => {
|
||||
const sponsorsPath = `${await getRootDir()}/packages/tools/sponsors.json`;
|
||||
const output: Sponsors = JSON.parse(await readFile(sponsorsPath, 'utf8'));
|
||||
|
||||
output.orgs = output.orgs.map(o => {
|
||||
if (o.urlWebsite) o.url = o.urlWebsite;
|
||||
return o;
|
||||
});
|
||||
|
||||
const oauthToken = await githubOauthToken();
|
||||
|
||||
for (const ghSponsor of output.github) {
|
||||
const userResponse = await fetchWithRetry(`https://api.github.com/users/${ghSponsor.name}`, {
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': `token ${oauthToken}`,
|
||||
},
|
||||
});
|
||||
|
||||
if (!userResponse.ok) {
|
||||
const apiErrorMessage = await userResponse.text();
|
||||
throw new Error(`Could not get user: "${ghSponsor.name}": ${apiErrorMessage}`);
|
||||
}
|
||||
|
||||
const user = await userResponse.json();
|
||||
ghSponsor.id = user.id;
|
||||
}
|
||||
|
||||
return output;
|
||||
};
|
||||
@@ -0,0 +1,62 @@
|
||||
import parsePluralLocalizationForm from './parsePluralLocalizationForm';
|
||||
|
||||
describe('parsePluralLocalizationForm', () => {
|
||||
|
||||
|
||||
it('should parse the plural form', async () => {
|
||||
const pluralForms = [
|
||||
'nplurals=1; plural=0;',
|
||||
'nplurals=2; plural=(n != 0);',
|
||||
'nplurals=2; plural=(n != 1);',
|
||||
'nplurals=2; plural=(n > 1);',
|
||||
'nplurals=2; plural=(n%10!=1 || n%100==11);',
|
||||
'nplurals=3; plural=(n%10==1 && n%100!=11 ? 0 : n != 0 ? 1 : 2);',
|
||||
'nplurals=3; plural=(n%10==1 && n%100!=11 ? 0 : n%10>=2 && (n%100<10 || n%100>=20) ? 1 : 2);',
|
||||
'nplurals=3; plural=(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2);',
|
||||
'nplurals=3; plural=(n==0 ? 0 : n==1 ? 1 : 2);',
|
||||
'nplurals=3; plural=(n==1 ? 0 : (n==0 || (n%100 > 0 && n%100 < 20)) ? 1 : 2);',
|
||||
'nplurals=3; plural=(n==1 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2);',
|
||||
'nplurals=3; plural=(n==1) ? 0 : (n>=2 && n<=4) ? 1 : 2;',
|
||||
'nplurals=3; plural=(n==1) ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2;',
|
||||
'nplurals=4; plural=(n%100==1 ? 1 : n%100==2 ? 2 : n%100==3 || n%100==4 ? 3 : 0);',
|
||||
'nplurals=4; plural=(n==1 ? 0 : n==0 || ( n%100>1 && n%100<11) ? 1 : (n%100>10 && n%100<20 ) ? 2 : 3);',
|
||||
'nplurals=4; plural=(n==1 || n==11) ? 0 : (n==2 || n==12) ? 1 : (n > 2 && n < 20) ? 2 : 3;',
|
||||
'nplurals=4; plural=(n==1) ? 0 : (n==2) ? 1 : (n != 8 && n != 11) ? 2 : 3;',
|
||||
'nplurals=4; plural=(n==1) ? 0 : (n==2) ? 1 : (n == 3) ? 2 : 3;',
|
||||
'nplurals=5; plural=n==1 ? 0 : n==2 ? 1 : (n>2 && n<7) ? 2 :(n>6 && n<11) ? 3 : 4;',
|
||||
'nplurals=6; plural=(n==0 ? 0 : n==1 ? 1 : n==2 ? 2 : n%100>=3 && n%100<=10 ? 3 : n%100>=11 ? 4 : 5);',
|
||||
];
|
||||
|
||||
const pluralValues = [
|
||||
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,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4, 4, 4],
|
||||
[0, 1, 2, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4],
|
||||
];
|
||||
|
||||
for (let index = 0; index < pluralForms.length; index++) {
|
||||
const form = pluralForms[index];
|
||||
// eslint-disable-next-line no-new-func -- There's a regex to check the form but it's still slightly unsafe, eventually we should automatically generate all the functions in advance in build-translations.ts
|
||||
const pluralFn = new Function('n', parsePluralLocalizationForm(form));
|
||||
for (let i = 0; i < 128; ++i) {
|
||||
expect(pluralValues[index][i]).toBe(pluralFn(i));
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,23 @@
|
||||
|
||||
// Copied from https://github.com/eugeny-dementev/parse-gettext-plural-form
|
||||
// along with the tests
|
||||
const parsePluralLocalizationForm = (form: string) => {
|
||||
const pluralFormRegex = /^(\s*nplurals\s*=\s*[0-9]+\s*;\s*plural\s*=\s*(?:\s|[-?|&=!<>+*/%:;a-zA-Z0-9_()])+)$/m;
|
||||
|
||||
if (!pluralFormRegex.test(form)) throw new Error(`Plural-Forms is invalid: ${form}`);
|
||||
|
||||
if (!/;\s*$/.test(form)) {
|
||||
form += ';';
|
||||
}
|
||||
|
||||
const code = [
|
||||
'var plural;',
|
||||
'var nplurals;',
|
||||
form,
|
||||
'return (plural === true ? 1 : plural ? plural : 0);',
|
||||
].join('\n');
|
||||
|
||||
return code;
|
||||
};
|
||||
|
||||
export default parsePluralLocalizationForm;
|
||||
@@ -0,0 +1,93 @@
|
||||
import { execCommand, isMac } from '../tool-utils';
|
||||
import { existsSync, readFile } from 'fs-extra';
|
||||
const gettextParser = require('gettext-parser');
|
||||
|
||||
export interface TranslationStatus {
|
||||
locale?: string;
|
||||
languageName?: string;
|
||||
translatorName: string;
|
||||
percentDone: number;
|
||||
untranslatedCount: number;
|
||||
pluralForms?: string;
|
||||
}
|
||||
|
||||
export type Translations = Record<string, string[]>;
|
||||
|
||||
export const removePoHeaderDate = async (filePath: string) => {
|
||||
let sedPrefix = 'sed -i';
|
||||
if (isMac()) sedPrefix += ' ""'; // Note: on macOS it has to be 'sed -i ""' (BSD quirk)
|
||||
await execCommand(`${sedPrefix} -e'/POT-Creation-Date:/d' "${filePath}"`);
|
||||
await execCommand(`${sedPrefix} -e'/PO-Revision-Date:/d' "${filePath}"`);
|
||||
};
|
||||
|
||||
export const translationExecutablePath = (file: string) => {
|
||||
const potentialPaths = [
|
||||
'/usr/local/opt/gettext/bin/',
|
||||
'/opt/local/bin/',
|
||||
'/usr/local/bin/',
|
||||
'/opt/homebrew/bin/',
|
||||
];
|
||||
|
||||
for (const path of potentialPaths) {
|
||||
const pathFile = path + file;
|
||||
if (existsSync(pathFile)) {
|
||||
return pathFile;
|
||||
}
|
||||
}
|
||||
|
||||
throw new Error(`${file} could not be found. Please install via brew or MacPorts.\n`);
|
||||
};
|
||||
|
||||
export const mergePotToPo = async (potFilePath: string, poFilePath: string) => {
|
||||
let msgmergePath = 'msgmerge';
|
||||
if (isMac()) msgmergePath = translationExecutablePath('msgmerge'); // Needs to have been installed with `brew install gettext`
|
||||
|
||||
const command = `${msgmergePath} -U "${poFilePath}" "${potFilePath}"`;
|
||||
const result = await execCommand(command);
|
||||
if (result && result.trim()) console.info(result.trim());
|
||||
await removePoHeaderDate(poFilePath);
|
||||
};
|
||||
|
||||
export const parsePoFile = async (filePath: string) => {
|
||||
const content = await readFile(filePath);
|
||||
return gettextParser.po.parse(content);
|
||||
};
|
||||
|
||||
interface GettextTranslation {
|
||||
msgstr: string[];
|
||||
comments?: { flag?: string };
|
||||
}
|
||||
|
||||
interface GettextParsed {
|
||||
translations: Record<string, Record<string, GettextTranslation>>;
|
||||
}
|
||||
|
||||
// Convert the gettext translations, as returned by `gettextParser.po.parse()`
|
||||
// to a <string, string> map, with the English text on the left and the
|
||||
// translation on the right. If a particular translation is missing, no entry
|
||||
// will be returned. The caller should display the English text in this case.
|
||||
export const parseTranslations = (gettextTranslations: GettextParsed) => {
|
||||
const output: Translations = {};
|
||||
|
||||
// Translations are grouped by "msgctxt"
|
||||
|
||||
for (const msgctxt of Object.keys(gettextTranslations.translations)) {
|
||||
const translations = gettextTranslations.translations[msgctxt];
|
||||
|
||||
for (const n in translations) {
|
||||
if (!translations.hasOwnProperty(n)) continue;
|
||||
if (n === '') continue;
|
||||
const t = translations[n];
|
||||
let translated: string[] = [];
|
||||
if (t.comments && t.comments.flag && t.comments.flag.indexOf('fuzzy') >= 0) {
|
||||
// Don't include fuzzy translations
|
||||
} else {
|
||||
translated = t['msgstr'];
|
||||
}
|
||||
|
||||
if (translated.length) output[n] = translated;
|
||||
}
|
||||
}
|
||||
|
||||
return output;
|
||||
};
|
||||
在新工单中引用
屏蔽一个用户