From a0128bdaad3bc63670e0989c348fa20dcec231e3 Mon Sep 17 00:00:00 2001 From: YISH Date: Fri, 2 Feb 2024 21:10:01 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20add=20support=20for=20wikilinks?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pnpm-lock.yaml | 4 ++++ src/export_templates.ts | 34 ++++++++++++++++++---------------- src/exporto0o.ts | 5 +++-- src/lang/de-DE.ts | 3 ++- src/lang/en-US.ts | 3 ++- src/lang/zh-CN.ts | 3 ++- src/pandoc.ts | 11 +++++++++-- src/ui/SettingTab.tsx | 15 +++++++++++---- src/ui/legacy/setting_tab.ts | 12 +++++++----- tests/pandocVersion.spec.ts | 4 ++-- typings/obsidian.d.ts | 3 ++- 11 files changed, 62 insertions(+), 35 deletions(-) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 1204c72..924eabe 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1,5 +1,9 @@ lockfileVersion: '6.0' +settings: + autoInstallPeers: true + excludeLinksFromLockfile: false + dependencies: semver: specifier: ^7.5.1 diff --git a/src/export_templates.ts b/src/export_templates.ts index 9b5a6f5..7a6e852 100644 --- a/src/export_templates.ts +++ b/src/export_templates.ts @@ -22,21 +22,21 @@ export default { name: 'Markdown', type: 'pandoc', arguments: - '-f markdown --resource-path="${currentDir}" --resource-path="${attachmentFolderPath}" --lua-filter="${luaDir}/markdown.lua" -s -o "${outputPath}" -t commonmark_x-attributes', + '-f ${fromFormat} --resource-path="${currentDir}" --resource-path="${attachmentFolderPath}" --lua-filter="${luaDir}/markdown.lua" -s -o "${outputPath}" -t commonmark_x-attributes', extension: '.md', }, 'Markdown (Hugo)': { name: 'Markdown (Hugo)', type: 'pandoc', arguments: - '-f markdown --resource-path="${currentDir}" --resource-path="${attachmentFolderPath}" --lua-filter="${luaDir}/markdown+hugo.lua" -s -o "${outputPath}" -t commonmark_x-attributes', + '-f ${fromFormat} --resource-path="${currentDir}" --resource-path="${attachmentFolderPath}" --lua-filter="${luaDir}/markdown+hugo.lua" -s -o "${outputPath}" -t commonmark_x-attributes', extension: '.md', }, 'Html': { name: 'Html', type: 'pandoc', arguments: - '-f markdown --resource-path="${currentDir}" --resource-path="${attachmentFolderPath}" --lua-filter="${luaDir}/math_block.lua" --embed-resources --standalone --metadata title="${currentFileName}" -s -o "${outputPath}" -t html', + '-f ${fromFormat} --resource-path="${currentDir}" --resource-path="${attachmentFolderPath}" --lua-filter="${luaDir}/math_block.lua" --embed-resources --standalone --metadata title="${currentFileName}" -s -o "${outputPath}" -t html', customArguments: '--mathjax="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-svg-full.js"', extension: '.html', }, @@ -44,21 +44,21 @@ export default { name: 'TextBundle', type: 'pandoc', arguments: - '-f markdown --resource-path="${currentDir}" --resource-path="${attachmentFolderPath}" --lua-filter="${luaDir}/markdown.lua" -V media_dir="${outputDir}/${outputFileName}.textbundle/assets" -s -o "${outputDir}/${outputFileName}.textbundle/text.md" -t commonmark_x-attributes', + '-f ${fromFormat} --resource-path="${currentDir}" --resource-path="${attachmentFolderPath}" --lua-filter="${luaDir}/markdown.lua" -V media_dir="${outputDir}/${outputFileName}.textbundle/assets" -s -o "${outputDir}/${outputFileName}.textbundle/text.md" -t commonmark_x-attributes', extension: '.md', }, 'Typst': { name: 'Typst', type: 'pandoc', arguments: - '-f markdown --resource-path="${currentDir}" --resource-path="${attachmentFolderPath}" --lua-filter="${luaDir}/markdown.lua" -s -o "${outputPath}" -t typst', + '-f ${fromFormat} --resource-path="${currentDir}" --resource-path="${attachmentFolderPath}" --lua-filter="${luaDir}/markdown.lua" -s -o "${outputPath}" -t typst', extension: '.typ', }, 'PDF': { name: 'PDF', type: 'pandoc', arguments: - '-f markdown --resource-path="${currentDir}" --resource-path="${attachmentFolderPath}" --lua-filter="${luaDir}/pdf.lua" ${ options.textemplate ? `--resource-path="${pluginDir}/textemplate" --template="${options.textemplate}"` : ` ` } -s -o "${outputPath}" -t pdf', + '-f ${fromFormat} --resource-path="${currentDir}" --resource-path="${attachmentFolderPath}" --lua-filter="${luaDir}/pdf.lua" ${ options.textemplate ? `--resource-path="${pluginDir}/textemplate" --template="${options.textemplate}"` : ` ` } -s -o "${outputPath}" -t pdf', customArguments: '--pdf-engine=pdflatex', optionsMeta: { 'textemplate': 'preset:textemplate', // reference from `PresetOptionsMeta` in `src/settings.ts` @@ -68,32 +68,32 @@ export default { 'Word (.docx)': { name: 'Word (.docx)', type: 'pandoc', - arguments: '-f markdown --resource-path="${currentDir}" --resource-path="${attachmentFolderPath}" -s -o "${outputPath}" -t docx', + arguments: '-f ${fromFormat} --resource-path="${currentDir}" --resource-path="${attachmentFolderPath}" -s -o "${outputPath}" -t docx', extension: '.docx', }, 'OpenOffice': { name: 'OpenOffice', type: 'pandoc', - arguments: '-f markdown --resource-path="${currentDir}" --resource-path="${attachmentFolderPath}" -s -o "${outputPath}" -t odt', + arguments: '-f ${fromFormat} --resource-path="${currentDir}" --resource-path="${attachmentFolderPath}" -s -o "${outputPath}" -t odt', extension: '.odt', }, 'RTF': { name: 'RTF', type: 'pandoc', - arguments: '-f markdown --resource-path="${currentDir}" --resource-path="${attachmentFolderPath}" -s -o "${outputPath}" -t rtf', + arguments: '-f ${fromFormat} --resource-path="${currentDir}" --resource-path="${attachmentFolderPath}" -s -o "${outputPath}" -t rtf', extension: '.rtf', }, 'Epub': { name: 'Epub', type: 'pandoc', - arguments: '-f markdown --resource-path="${currentDir}" --resource-path="${attachmentFolderPath}" -s -o "${outputPath}" -t epub', + arguments: '-f ${fromFormat} --resource-path="${currentDir}" --resource-path="${attachmentFolderPath}" -s -o "${outputPath}" -t epub', extension: '.epub', }, 'Latex': { name: 'Latex', type: 'pandoc', arguments: - '-f markdown --resource-path="${currentDir}" --resource-path="${attachmentFolderPath}" ${ options.textemplate ? `--resource-path="${pluginDir}/textemplate" --template="${options.textemplate}"` : ` ` } --extract-media="${outputDir}" -s -o "${outputPath}" -t latex', + '-f ${fromFormat} --resource-path="${currentDir}" --resource-path="${attachmentFolderPath}" ${ options.textemplate ? `--resource-path="${pluginDir}/textemplate" --template="${options.textemplate}"` : ` ` } --extract-media="${outputDir}" -s -o "${outputPath}" -t latex', optionsMeta: { 'textemplate': 'preset:textemplate', // reference from `PresetOptionsMeta` in `src/settings.ts` }, @@ -102,32 +102,34 @@ export default { 'Media Wiki': { name: 'Media Wiki', type: 'pandoc', - arguments: '-f markdown --resource-path="${currentDir}" --resource-path="${attachmentFolderPath}" -s -o "${outputPath}" -t mediawiki', + arguments: + '-f ${fromFormat} --resource-path="${currentDir}" --resource-path="${attachmentFolderPath}" -s -o "${outputPath}" -t mediawiki', extension: '.mediawiki', }, 'reStructuredText': { name: 'reStructuredText', type: 'pandoc', - arguments: '-f markdown --resource-path="${currentDir}" --resource-path="${attachmentFolderPath}" -s -o "${outputPath}" -t rst', + arguments: '-f ${fromFormat} --resource-path="${currentDir}" --resource-path="${attachmentFolderPath}" -s -o "${outputPath}" -t rst', extension: '.rst', }, 'Textile': { name: 'Textile', type: 'pandoc', - arguments: '-f markdown --resource-path="${currentDir}" --resource-path="${attachmentFolderPath}" -s -o "${outputPath}" -t textile', + arguments: + '-f ${fromFormat} --resource-path="${currentDir}" --resource-path="${attachmentFolderPath}" -s -o "${outputPath}" -t textile', extension: '.textile', }, 'OPML': { name: 'OPML', type: 'pandoc', - arguments: '-f markdown --resource-path="${currentDir}" --resource-path="${attachmentFolderPath}" -s -o "${outputPath}" -t opml', + arguments: '-f ${fromFormat} --resource-path="${currentDir}" --resource-path="${attachmentFolderPath}" -s -o "${outputPath}" -t opml', extension: '.opml', }, 'Bibliography (.bib)': { name: 'Bibliography', type: 'pandoc', arguments: - '-f markdown --resource-path="${currentDir}" --resource-path="${attachmentFolderPath}" --lua-filter="${luaDir}/citefilter.lua" -o "${outputPath}" --to=bibtex "${currentPath}"', + '-f ${fromFormat} --resource-path="${currentDir}" --resource-path="${attachmentFolderPath}" --lua-filter="${luaDir}/citefilter.lua" -o "${outputPath}" --to=bibtex "${currentPath}"', extension: '.bib', }, 'Custom': { diff --git a/src/exporto0o.ts b/src/exporto0o.ts index 386c69f..912ecd1 100644 --- a/src/exporto0o.ts +++ b/src/exporto0o.ts @@ -7,7 +7,7 @@ import { MessageBox } from './ui/message_box'; import { Notice, TFile } from 'obsidian'; import { exec, renderTemplate, getPlatformValue } from './utils'; import type ExportPlugin from './main'; -import { normalizePandocPath } from './pandoc'; +import pandoc from './pandoc'; export async function exportToOo( plugin: ExportPlugin, @@ -100,6 +100,7 @@ export async function exportToOo( // now: new Date() metadata: frontMatter, options, + fromFormat: app.vault.config.useMarkdownLinks ? 'markdown' : 'markdown+wikilinks_title_after_pipe', }; const showCommandLineOutput = setting.type === 'custom' && setting.showCommandOutput; @@ -149,7 +150,7 @@ export async function exportToOo( // process Environment variables.. const env = (variables.env = createEnv(getPlatformValue(globalSetting.env) ?? {}, variables)); - const pandocPath = normalizePandocPath(getPlatformValue(globalSetting.pandocPath)); + const pandocPath = pandoc.normalizePath(getPlatformValue(globalSetting.pandocPath)); const cmdTpl = setting.type === 'pandoc' diff --git a/src/lang/de-DE.ts b/src/lang/de-DE.ts index c52672c..02482ed 100644 --- a/src/lang/de-DE.ts +++ b/src/lang/de-DE.ts @@ -33,7 +33,8 @@ export default { general: 'Allgemein', name: 'Name', title: 'Export-Einstellungen', - version: strTpl`Version: ${0}`, + pandocVersion: strTpl`Version: ${0}`, + pandocVersionWithWarning: strTpl`Version: ${0}, please upgrade version to ${1}`, pandocNotFound: 'Pandoc.exe wurde nicht gefunden. Bitte geben Sie den Pfad zur Pandoc.exe ein oder fügen Sie ihn den Window Systemumgebungsvariablen hinzu.', defaultFolderForExportedFile: 'Standardordner für exportierte Dateien', diff --git a/src/lang/en-US.ts b/src/lang/en-US.ts index 43dc12c..98bd678 100644 --- a/src/lang/en-US.ts +++ b/src/lang/en-US.ts @@ -32,7 +32,8 @@ export default { general: 'General', name: 'Name', title: 'Export Settings', - version: strTpl`Version: ${0}`, + pandocVersion: strTpl`Version: ${0}`, + pandocVersionWithWarning: strTpl`Version: ${0}, please upgrade version to ${1}`, pandocNotFound: 'Pandoc not found, please fill in the Pandoc file path, or add it to the system environment variables.', defaultFolderForExportedFile: 'Default Folder for Exported File', openExportedFileLocation: 'Open exported file location', diff --git a/src/lang/zh-CN.ts b/src/lang/zh-CN.ts index fbfb4e6..cfe23c2 100644 --- a/src/lang/zh-CN.ts +++ b/src/lang/zh-CN.ts @@ -34,7 +34,8 @@ export default { general: '通用', name: '名称', customLocation: '自定义', - version: strTpl`版本: ${0}`, + pandocVersion: strTpl`版本: ${0}`, + pandocVersionWithWarning: strTpl`Version: ${0}, 请升级版本到 ${1}`, pandocNotFound: '找不到 Pandoc,请填写 Pandoc 文件路径,或者将其添加到系统环境变量中。', pandocPath: 'Pandoc 路径', defaultFolderForExportedFile: '默认的导出文件夹', diff --git a/src/pandoc.ts b/src/pandoc.ts index 0dc4b94..84e6938 100644 --- a/src/pandoc.ts +++ b/src/pandoc.ts @@ -1,10 +1,9 @@ import { exec } from './utils'; import semver from 'semver/preload'; -import type { SemVer } from 'semver'; export const normalizePandocPath = (path?: string) => (path?.includes(' ') ? `"${path}"` : `${path ?? 'pandoc'}`); -export async function getPandocVersion(path?: string, env?: Record): Promise { +export async function getPandocVersion(path?: string, env?: Record) { path = normalizePandocPath(path); let version = await exec(`${path} --version`, { env }); version = version.substring(0, version.indexOf('\n')).replace('pandoc.exe', '').replace('pandoc', '').trim(); @@ -15,3 +14,11 @@ export async function getPandocVersion(path?: string, env?: Record { const { plugin, lang } = props; const [settings, setSettings0] = createStore(plugin.settings); - const [pandocVersion, setPandocVersion] = createSignal(); + const [pandocVersion, setPandocVersion] = createSignal(); const envVars = createMemo(() => Object.entries(Object.assign({}, getPlatformValue(DEFAULT_ENV), getPlatformValue(settings.env) ?? {})).map(([n, v]) => `${n}="${v}"`).join('\n')); const setSettings: typeof setSettings0 = (...args: unknown[]) => { (setSettings0 as ((...args: unknown[]) => void))(...args); @@ -70,7 +71,13 @@ const SettingTab = (props: { lang: Lang, plugin: UniversalExportPlugin }) => { const pandocDescription = createMemo(() => { const version = pandocVersion(); - return version ? lang.settingTab.version(version) : lang.settingTab.pandocNotFound; + if (version) { + if (app.vault.config.useMarkdownLinks && version.compare(pandoc.requiredVersion) === -1) { + return lang.settingTab.pandocVersionWithWarning(pandoc.requiredVersion) + } + return lang.settingTab.pandocVersion(version) + } + return lang.settingTab.pandocNotFound; }); const [modal, setModal] = createSignal<() => JSX.Element>(); @@ -215,7 +222,7 @@ const SettingTab = (props: { lang: Lang, plugin: UniversalExportPlugin }) => { createEffect(async () => { try { const env = createEnv(getPlatformValue(settings.env) ?? {}); - setPandocVersion((await getPandocVersion(getPlatformValue(settings.pandocPath), env)).version); + setPandocVersion(await pandoc.getVersion(getPlatformValue(settings.pandocPath), env)); } catch { setPandocVersion(undefined); } diff --git a/src/ui/legacy/setting_tab.ts b/src/ui/legacy/setting_tab.ts index ef1882c..ea1e31e 100644 --- a/src/ui/legacy/setting_tab.ts +++ b/src/ui/legacy/setting_tab.ts @@ -2,7 +2,7 @@ import { App, PluginSettingTab, Setting, TextComponent } from 'obsidian'; import * as ct from 'electron'; import { CustomExportSetting, ExportSetting, PandocExportSetting, UniversalExportPluginSettings } from '../../settings'; import { setPlatformValue, getPlatformValue } from '../../utils'; -import { getPandocVersion } from '../../pandoc'; +import pandoc from '../../pandoc'; import { Modal } from 'obsidian'; import export_command_templates from '../../export_templates'; @@ -98,9 +98,10 @@ export default class extends PluginSettingTab { .setHeading(); const pandocPathSetting = new Setting(containerEl); - getPandocVersion(getPlatformValue(globalSetting.pandocPath)) + pandoc + .getVersion(getPlatformValue(globalSetting.pandocPath)) .then(ver => { - pandocPathSetting.setDesc(lang.settingTab.version(ver.version)); + pandocPathSetting.setDesc(lang.settingTab.pandocVersion(ver.version)); }) .catch(() => { pandocPathSetting.setDesc(lang.settingTab.pandocNotFound); @@ -110,9 +111,10 @@ export default class extends PluginSettingTab { cb.setPlaceholder(lang.settingTab.pandocPathPlaceholder).onChange(v => { if (globalSetting.pandocPath !== v) { globalSetting.pandocPath = setPlatformValue(globalSetting.pandocPath, v); - getPandocVersion(getPlatformValue(globalSetting.pandocPath)) + pandoc + .getVersion(getPlatformValue(globalSetting.pandocPath)) .then(ver => { - pandocPathSetting.setDesc(lang.settingTab.version(ver.version)); + pandocPathSetting.setDesc(lang.settingTab.pandocVersion(ver.version)); }) .catch(() => { pandocPathSetting.setDesc(lang.settingTab.pandocNotFound); diff --git a/tests/pandocVersion.spec.ts b/tests/pandocVersion.spec.ts index 8b20c76..7c06ecf 100644 --- a/tests/pandocVersion.spec.ts +++ b/tests/pandocVersion.spec.ts @@ -1,7 +1,7 @@ -import { getPandocVersion } from '../src/pandoc'; +import pandoc from '../src/pandoc'; test('test get pandoc version', async () => { - const out = await getPandocVersion(); + const out = await pandoc.getVersion(); expect(out.compare('3.1.5')).toBe(1); }); \ No newline at end of file diff --git a/typings/obsidian.d.ts b/typings/obsidian.d.ts index 36793b6..3617e19 100644 --- a/typings/obsidian.d.ts +++ b/typings/obsidian.d.ts @@ -26,7 +26,8 @@ declare module 'obsidian' { export interface Vault { config: { - attachmentFolderPath: string + attachmentFolderPath: string, + useMarkdownLinks: boolean, } on(name: 'raw', callback: (file: string) => void, ctx?: any): EventRef; }