From dc28b3ac60955ea140873087a65ed76f891535be Mon Sep 17 00:00:00 2001 From: OliverBalfour Date: Thu, 24 Jun 2021 16:35:26 +1000 Subject: [PATCH] temp fix for citations --- global.ts | 3 ++ main.ts | 91 ++++++++++++++++++++++++++++++++++------------------- settings.ts | 14 +++++++++ 3 files changed, 76 insertions(+), 32 deletions(-) diff --git a/global.ts b/global.ts index 5330df9..d1b9451 100644 --- a/global.ts +++ b/global.ts @@ -28,6 +28,8 @@ export interface PandocPluginSettings { outputFolder: string | null, // Extra CLI arguments for Pandoc to support features we don't have a UI for yet extraArguments: string, + // Export from HTML or from markdown? + exportFrom: 'html' | 'md', } export const DEFAULT_SETTINGS: PandocPluginSettings = { @@ -43,6 +45,7 @@ export const DEFAULT_SETTINGS: PandocPluginSettings = { pdflatex: null, outputFolder: null, extraArguments: '', + exportFrom: 'html', } export function replaceFileExtension(file: string, ext: string): string { diff --git a/main.ts b/main.ts index 664b38d..2cda975 100644 --- a/main.ts +++ b/main.ts @@ -91,44 +91,71 @@ export default class PandocPlugin extends Plugin { // Instead of using Pandoc to process the raw Markdown, we use Obsidian's // internal markdown renderer, and process the HTML it generates instead. // This allows us to more easily deal with Obsidian specific Markdown syntax. + // However, we provide an option to use MD instead to use citations + let outputFile: string = replaceFileExtension(inputFile, extension); + if (this.settings.outputFolder) { + outputFile = path.join(this.settings.outputFolder, path.basename(outputFile)); + } + const view = this.app.workspace.getActiveViewOfType(MarkdownView); + try { - const view = this.app.workspace.getActiveViewOfType(MarkdownView); - const adapter = this.app.vault.adapter as FileSystemAdapter; - const { html, metadata } = await render(this, view, inputFile, format); - - let outputFile: string = replaceFileExtension(inputFile, extension); - if (this.settings.outputFolder) { - outputFile = path.join(this.settings.outputFolder, path.basename(outputFile)); + let error, command; + + switch (this.settings.exportFrom) { + case 'html': { + const { html, metadata } = await render(this, view, inputFile, format); + + if (format === 'html') { + // Write to HTML file + await fs.promises.writeFile(outputFile, html); + new Notice('Successfully exported via Pandoc to ' + outputFile); + return; + } else { + // Spawn Pandoc + const metadataFile = temp.path(); + const metadataString = YAML.stringify(metadata); + await fs.promises.writeFile(metadataFile, metadataString); + const result = await pandoc( + { + file: 'STDIN', contents: html, format: 'html', metadataFile, + pandoc: this.settings.pandoc, pdflatex: this.settings.pdflatex + }, + { file: outputFile, format }, + this.settings.extraArguments.split('\n') + ); + error = result.error; + command = result.command; + } + break; + } + case 'md': { + const result = await pandoc( + { + file: inputFile, format: 'markdown', + pandoc: this.settings.pandoc, pdflatex: this.settings.pdflatex + }, + { file: outputFile, format }, + this.settings.extraArguments.split('\n') + ); + error = result.error; + command = result.command; + break; + } } - if (format === 'html') { - // Write to HTML file - await fs.promises.writeFile(outputFile, html); - new Notice('Successfully exported via Pandoc to ' + outputFile); + // Never give warnings for plain-text exports + if (error.length && format !== 'plain') { + new Notice('Exported via Pandoc to ' + outputFile + ' with warnings'); + new Notice('Pandoc warnings:' + error, 10000); } else { - // Spawn Pandoc - const metadataFile = temp.path(); - const metadataString = YAML.stringify(metadata); - await fs.promises.writeFile(metadataFile, metadataString); - const { error, command } = await pandoc( - { file: 'STDIN', contents: html, format: 'html', metadataFile, - pandoc: this.settings.pandoc, pdflatex: this.settings.pdflatex }, - { file: outputFile, format }, - this.settings.extraArguments.split('\n') - ); - // Never give warnings for plain-text exports - if (error.length && format !== 'plain') { - new Notice('Exported via Pandoc to ' + outputFile + ' with warnings'); - new Notice('Pandoc warnings:' + error, 10000); - } else { - new Notice('Successfully exported via Pandoc to ' + outputFile); - } - if (this.settings.showCLICommands) { - new Notice('Pandoc command: ' + command, 10000); - console.log(command); - } + new Notice('Successfully exported via Pandoc to ' + outputFile); } + if (this.settings.showCLICommands) { + new Notice('Pandoc command: ' + command, 10000); + console.log(command); + } + } catch (e) { new Notice('Pandoc export failed: ' + e.toString(), 15000); console.error(e); diff --git a/settings.ts b/settings.ts index 4ff658f..9bf46cf 100644 --- a/settings.ts +++ b/settings.ts @@ -81,6 +81,20 @@ export default class PandocPluginSettingTab extends PluginSettingTab { await this.plugin.saveSettings(); })); + new Setting(containerEl) + .setName("Export files from HTML or markdown?") + .setDesc("Export from markdown, or from the HTML visible in Obsidian? HTML supports fancy plugin features, markdown supports Pandoc features like citations.") + .addDropdown(dropdown => dropdown + .addOptions({ + "html": "HTML", + "md": "Markdown", + }) + .setValue(this.plugin.settings.exportFrom) + .onChange(async (value: string) => { + this.plugin.settings.exportFrom = value as 'html' | 'md'; + await this.plugin.saveSettings(); + })); + new Setting(containerEl) .setName("Export folder") .setDesc("Absolute path to an export folder, like 'C:\Users\Example\Documents' or '/home/user/zettelkasten'. If left blank, files are saved next to where they were exported from.")