一些检查失败了
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
44 行
1.4 KiB
TypeScript
44 行
1.4 KiB
TypeScript
import { CommandRuntime, CommandDeclaration } from '@joplin/lib/services/CommandService';
|
|
import { _ } from '@joplin/lib/locale';
|
|
import HtmlToMd from '@joplin/lib/HtmlToMd';
|
|
import cleanMarkdownNbsp from '../utils/cleanMarkdownNbsp';
|
|
import { processImagesInPastedHtml } from '../utils/resourceHandling';
|
|
import { WindowCommandDependencies } from '../utils/types';
|
|
|
|
import { clipboard } from 'electron';
|
|
|
|
export const declaration: CommandDeclaration = {
|
|
name: 'pasteAsMarkdown',
|
|
label: () => _('Paste as Markdown'),
|
|
};
|
|
|
|
let htmlToMd_: HtmlToMd | null = null;
|
|
|
|
const htmlToMd = () => {
|
|
if (!htmlToMd_) {
|
|
htmlToMd_ = new HtmlToMd();
|
|
}
|
|
return htmlToMd_;
|
|
};
|
|
|
|
export const runtime = (comp: WindowCommandDependencies): CommandRuntime => {
|
|
return {
|
|
execute: async () => {
|
|
let html = clipboard.readHTML();
|
|
if (html) {
|
|
// Download images and convert them to Joplin resources
|
|
html = await processImagesInPastedHtml(html, { useInternalUrls: true });
|
|
const markdown = cleanMarkdownNbsp(htmlToMd().parse(html, { tightLists: true, collapseMultipleBlankLines: true }));
|
|
void comp.editorRef.current.execCommand({ name: 'insertText', value: markdown });
|
|
} else {
|
|
// Fall back to plain text if no HTML is available
|
|
const text = clipboard.readText();
|
|
if (text) {
|
|
void comp.editorRef.current.execCommand({ name: 'insertText', value: text });
|
|
}
|
|
}
|
|
},
|
|
enabledCondition: 'oneNoteSelected && markdownEditorVisible',
|
|
};
|
|
};
|