Skip to content

Commit

Permalink
Added gfm support and relative path support for images
Browse files Browse the repository at this point in the history
  • Loading branch information
JDMcE committed Jul 9, 2022
1 parent 8229273 commit 4eb7e64
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 23 deletions.
6 changes: 4 additions & 2 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ export default class PandocPlugin extends Plugin {
const result = await pandoc(
{
file: 'STDIN', contents: html, format: 'html', metadataFile,
pandoc: this.settings.pandoc, pdflatex: this.settings.pdflatex
pandoc: this.settings.pandoc, pdflatex: this.settings.pdflatex,
directory: path.dirname(inputFile)
},
{ file: outputFile, format },
this.settings.extraArguments.split('\n')
Expand All @@ -133,7 +134,8 @@ export default class PandocPlugin extends Plugin {
const result = await pandoc(
{
file: inputFile, format: 'markdown',
pandoc: this.settings.pandoc, pdflatex: this.settings.pdflatex
pandoc: this.settings.pandoc, pdflatex: this.settings.pdflatex,
directory: path.dirname(inputFile)
},
{ file: outputFile, format },
this.settings.extraArguments.split('\n')
Expand Down
23 changes: 12 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,20 @@
"author": "Oliver Balfour",
"license": "MIT",
"devDependencies": {
"@rollup/plugin-commonjs": "^15.1.0",
"@rollup/plugin-node-resolve": "^9.0.0",
"@rollup/plugin-typescript": "^6.0.0",
"@types/node": "^14.14.2",
"@codemirror/view": "^6.0.0",
"@rollup/plugin-commonjs": "15.1.0",
"@rollup/plugin-node-resolve": "9.0.0",
"@rollup/plugin-typescript": "6.0.0",
"@types/node": "14.14.2",
"obsidian": "https://github.com/obsidianmd/obsidian-api/tarball/master",
"rollup": "^2.32.1",
"tslib": "^2.0.3",
"typescript": "^4.0.3"
"rollup": "2.32.1",
"tslib": "2.0.3",
"typescript": "4.0.3"
},
"dependencies": {
"@types/temp": "^0.9.0",
"lookpath": "^1.2.0",
"temp": "^0.9.4",
"yaml": "^2.0.0-4"
"@types/temp": "0.9.0",
"lookpath": "1.2.0",
"temp": "0.9.4",
"yaml": "2.0.0-4"
}
}
6 changes: 4 additions & 2 deletions pandoc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,15 @@ export const inputExtensions = ['md', 'docx', 'csv', 'html', 'tex', 'odt'];
// Subset of output formats, will add more later
// Note: you need a `-o -` in the command to output odt, docx, epub or pdf output (presumably as they are binary formats or something)
export type OutputFormat = 'asciidoc' | 'beamer' | 'commonmark_x' | 'docx' | 'epub'
| 'html' | 'pdf' | 'json' | 'latex' | 'odt' | 'pptx' | 'revealjs'
| 'html' | 'gfm' | 'pdf' | 'json' | 'latex' | 'odt' | 'pptx' | 'revealjs'
| 'beamer' | 'rtf' | 'docuwiki' | 'mediawiki';

// List of [pretty name, pandoc format name, file extension, shortened pretty name]
export const outputFormats = [
['AsciiDoc (adoc)', 'asciidoc', 'adoc', 'AsciiDoc'],
['Word Document (docx)', 'docx', 'docx', 'Word'],
['Pandoc Markdown', 'markdown', 'pandoc.md', 'markdown'], // X.md -> X.pandoc.md to avoid conflict
['GitHub Markdown', 'gfm', 'gfm.md', 'gfmarkdown'], // X.md -> X.gfm.md to avoid conflict
['HTML (without Pandoc)','html','html', 'HTML'],
['LaTeX', 'latex', 'tex', 'LaTeX'],
['OpenDocument (odt)', 'odt', 'odt', 'OpenDocument'],
Expand All @@ -64,6 +65,7 @@ export interface PandocInput {
metadataFile?: string, // path to YAML file
pandoc?: string, // optional path to Pandoc if it's not in the current PATH variable
pdflatex?: string, // ditto for pdflatex
directory: AbsoluteFilePath, // The directory the source file is in
}

export interface PandocOutput {
Expand Down Expand Up @@ -158,7 +160,7 @@ export const pandoc = async (input: PandocInput, output: PandocOutput, extraPara
env.PATH += ":";
env.PATH += path.dirname(input.pdflatex);
}
pandoc = spawn(input.pandoc || 'pandoc', args, { env: process.env });
pandoc = spawn(input.pandoc || 'pandoc', args, { env: process.env, cwd: input.directory });

if (stdin) {
// TODO: strip some unicode characters but not others
Expand Down
16 changes: 8 additions & 8 deletions renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,14 +227,14 @@ async function postProcessRenderedHTML(plugin: PandocPlugin, inputFile: string,
// Fix <img src="app://obsidian.md/image.png">
// Note: this will throw errors when Obsidian tries to load images with a (now invalid) src
// These errors can be safely ignored
if (outputFormat !== 'html') {
for (let img of Array.from(wrapper.querySelectorAll('img'))) {
if (img.src.startsWith(prefix) && img.getAttribute('data-touched') !== 'true') {
img.src = adapter.getFullPath(img.src.substring(prefix.length));
img.setAttribute('data-touched', 'true');
}
}
}
// if (outputFormat !== 'html') {
// for (let img of Array.from(wrapper.querySelectorAll('img'))) {
// if (img.src.startsWith(prefix) && img.getAttribute('data-touched') !== 'true') {
// img.src = adapter.getFullPath(img.src.substring(prefix.length));
// img.setAttribute('data-touched', 'true');
// }
// }
// }
// Remove YAML frontmatter from the output if desired
if (!settings.displayYAMLFrontmatter) {
Array.from(wrapper.querySelectorAll('.frontmatter, .frontmatter-container'))
Expand Down

0 comments on commit 4eb7e64

Please sign in to comment.