文件
mynode/packages/tools/website/utils/openGraph.ts
chen 301194963b
一些检查失败了
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
feat: Initial clean release of CHM
2026-11-04 11:29:39 +08:00

46 行
1.4 KiB
TypeScript

import { MarkupLanguage, MarkupToHtml } from '@joplin/renderer';
import { readmeFileTitleAndBody, replaceGitHubByWebsiteLinks } from './parser';
import markupLanguageUtils from '@joplin/lib/markupLanguageUtils';
import { substrWithEllipsis } from '@joplin/lib/string-utils';
export interface OpenGraphTags {
title: string;
description: string;
url: string;
image?: string;
}
let markupToHtml_: MarkupToHtml = null;
const markupToHtml = (): MarkupToHtml => {
if (!markupToHtml_) markupToHtml_ = new MarkupToHtml();
return markupToHtml_;
};
const getImageUrl = (content: string): string | null => {
const imageUrls = markupLanguageUtils.extractImageUrls(MarkupLanguage.Any, content);
if (!imageUrls.length) return null;
let imageUrl = replaceGitHubByWebsiteLinks(imageUrls[0]);
if (!imageUrl.startsWith('https:')) {
if (!imageUrl.startsWith('/')) imageUrl = `/${imageUrl}`;
imageUrl = `https://joplinapp.org${imageUrl}`;
}
return imageUrl;
};
export const extractOpenGraphTags = async (sourcePath: string, url?: string): Promise<OpenGraphTags> => {
const doc = await readmeFileTitleAndBody(sourcePath);
const output: OpenGraphTags = {
title: substrWithEllipsis(doc.title, 0, 70),
description: substrWithEllipsis(markupToHtml().stripMarkup(MarkupLanguage.Markdown, doc.body), 0, 200),
url: url ? url : '',
};
const imageUrl = getImageUrl(doc.body);
if (imageUrl) output.image = imageUrl;
return output;
};