Skip to content

Commit

Permalink
temp fix for citations
Browse files Browse the repository at this point in the history
  • Loading branch information
OliverBalfour committed Jun 24, 2021
1 parent c913472 commit dc28b3a
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 32 deletions.
3 changes: 3 additions & 0 deletions global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -43,6 +45,7 @@ export const DEFAULT_SETTINGS: PandocPluginSettings = {
pdflatex: null,
outputFolder: null,
extraArguments: '',
exportFrom: 'html',
}

export function replaceFileExtension(file: string, ext: string): string {
Expand Down
91 changes: 59 additions & 32 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
14 changes: 14 additions & 0 deletions settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand Down

0 comments on commit dc28b3a

Please sign in to comment.