Skip to content

Commit

Permalink
✨ add support for wikilinks
Browse files Browse the repository at this point in the history
  • Loading branch information
mokeyish authored Feb 2, 2024
1 parent f9a68ba commit a0128bd
Show file tree
Hide file tree
Showing 11 changed files with 62 additions and 35 deletions.
4 changes: 4 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 18 additions & 16 deletions src/export_templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,43 +22,43 @@ 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',
},
'TextBundle': {
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`
Expand All @@ -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`
},
Expand All @@ -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': {
Expand Down
5 changes: 3 additions & 2 deletions src/exporto0o.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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'
Expand Down
3 changes: 2 additions & 1 deletion src/lang/de-DE.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
3 changes: 2 additions & 1 deletion src/lang/en-US.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
3 changes: 2 additions & 1 deletion src/lang/zh-CN.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: '默认的导出文件夹',
Expand Down
11 changes: 9 additions & 2 deletions src/pandoc.ts
Original file line number Diff line number Diff line change
@@ -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<string, string>): Promise<SemVer> {
export async function getPandocVersion(path?: string, env?: Record<string, string>) {
path = normalizePandocPath(path);
let version = await exec(`${path} --version`, { env });
version = version.substring(0, version.indexOf('\n')).replace('pandoc.exe', '').replace('pandoc', '').trim();
Expand All @@ -15,3 +14,11 @@ export async function getPandocVersion(path?: string, env?: Record<string, strin
}
return semver.parse(version);
}

export const PANDOC_REQUIRED_VERSION = '3.1.7';

export default {
normalizePath: normalizePandocPath,
getVersion: getPandocVersion,
requiredVersion: PANDOC_REQUIRED_VERSION,
};
15 changes: 11 additions & 4 deletions src/ui/SettingTab.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as ct from 'electron';
import process from 'process';
import { PluginSettingTab } from 'obsidian';
import type { SemVer } from 'semver'
import type UniversalExportPlugin from '../main';
import {
CustomExportSetting,
Expand All @@ -16,7 +17,7 @@ import { createStore, produce } from 'solid-js/store';
import { insert, Dynamic } from 'solid-js/web';
import type { Lang } from '../lang';

import { getPandocVersion } from '../pandoc';
import pandoc from '../pandoc';
import Modal from './components/Modal';
import Button from './components/Button';
import Setting, { Text, Toggle, ExtraButton, DropDown, TextArea } from './components/Setting';
Expand All @@ -26,7 +27,7 @@ import export_templates from '../export_templates';
const SettingTab = (props: { lang: Lang, plugin: UniversalExportPlugin }) => {
const { plugin, lang } = props;
const [settings, setSettings0] = createStore(plugin.settings);
const [pandocVersion, setPandocVersion] = createSignal<string>();
const [pandocVersion, setPandocVersion] = createSignal<SemVer>();
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);
Expand Down Expand Up @@ -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>();
Expand Down Expand Up @@ -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);
}
Expand Down
12 changes: 7 additions & 5 deletions src/ui/legacy/setting_tab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions tests/pandocVersion.spec.ts
Original file line number Diff line number Diff line change
@@ -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);
});
3 changes: 2 additions & 1 deletion typings/obsidian.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit a0128bd

Please sign in to comment.