Skip to content

Commit

Permalink
🐛 Fix utf8 path export failure on windows (#157)
Browse files Browse the repository at this point in the history
  • Loading branch information
mokeyish authored Feb 2, 2024
1 parent 42fdf45 commit 64c1a65
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 13 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ jobs:
node-version: ${{ matrix.node-version }}
- name: Install Pandoc
run: |
wget https://github.com/jgm/pandoc/releases/download/3.1.6.1/pandoc-3.1.6.1-1-amd64.deb
sudo dpkg -i pandoc-3.1.6.1-1-amd64.deb
wget https://github.com/jgm/pandoc/releases/download/3.1.11.1/pandoc-3.1.11.1-1-amd64.deb
sudo dpkg -i pandoc-3.1.11.1-1-amd64.deb
- name: Install Dependencies
run: npm install
- name: Test
Expand Down
6 changes: 4 additions & 2 deletions lua/markdown.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ local url = require('url')
local pandoc=pandoc
local PANDOC_STATE=PANDOC_STATE

PANDOC_VERSION:must_be_at_least '2.17'
PANDOC_VERSION:must_be_at_least '3.1.7'

os.text = pandoc.text

local PATH = pandoc.path
local doc_dir = nil
Expand All @@ -22,7 +24,7 @@ if PANDOC_STATE.output_file then
local output_file = PANDOC_STATE.output_file
doc_dir = PATH.directory(output_file)
if PANDOC_WRITER_OPTIONS.variables["media_dir"] then
media_dir = PANDOC_WRITER_OPTIONS.variables["media_dir"]
media_dir = tostring(PANDOC_WRITER_OPTIONS.variables["media_dir"])
else
media_dir = PATH.split_extension(output_file)
if Mode ~= 'hugo' then
Expand Down
7 changes: 5 additions & 2 deletions lua/polyfill.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ if os.platform == nil then
local libExt = package.cpath:match("%p[\\|/]?\\.%p(%a+)")
if libExt == 'dll' then
os.platform = "Windows"
require"utf8_filenames"
elseif libExt == 'so' then
os.platform = "Linux"
elseif libExt == 'dylib' then
Expand All @@ -15,7 +14,9 @@ end
os.copy = function(src, dest)
if os.platform == "Windows" then
src = string.gsub(src, "/", "\\")
os.execute('copy "' .. src .. '" "' .. dest .. '"')
src = os.text.toencoding(src)
dest = os.text.toencoding(dest)
os.execute('copy "' .. src .. '" "' .. dest .. '" >NUL')
else
os.execute('cp "' .. src .. '" "' .. dest .. '"')
end
Expand All @@ -26,6 +27,7 @@ os.mkdir = function(dir)
return
end
if os.platform == "Windows" then
dir = os.text.toencoding(dir)
os.execute('mkdir "' .. dir .. '"')
else
os.execute('mkdir -p "' .. dir .. '"')
Expand All @@ -35,6 +37,7 @@ end
os.exists = function(path)
if os.platform == "Windows" then
path = string.gsub(path, "/", "\\")
path = os.text.toencoding(path)
local _, _, code = os.execute('if exist "' .. path .. '" (exit 0) else (exit 1)')
return code == 0
else
Expand Down
File renamed without changes.
11 changes: 4 additions & 7 deletions src/exporto0o.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import argsParser from 'yargs-parser';
import { Variables, ExportSetting, extractDefaultExtension as extractExtension, createEnv } from './settings';
import { MessageBox } from './ui/message_box';
import { Notice, TFile } from 'obsidian';
import { exec, renderTemplate, getPlatformValue } from './utils';
import { exec, renderTemplate, getPlatformValue, trimQuotes } from './utils';
import type ExportPlugin from './main';
import pandoc from './pandoc';

Expand Down Expand Up @@ -154,6 +154,7 @@ export async function exportToOo(
let pandocPath = pandoc.normalizePath(getPlatformValue(globalSetting.pandocPath));

if (process.platform === 'win32') {
// https://github.com/mokeyish/obsidian-enhancing-export/issues/153
pandocPath = pandocPath.replaceAll('\\', '/');
const pathKeys: Array<keyof Variables> = [
'pluginDir',
Expand All @@ -174,7 +175,7 @@ export async function exportToOo(

const cmdTpl =
setting.type === 'pandoc'
? `${pandocPath} ${setting.arguments ?? ''} ${setting.customArguments ?? ''} "${currentPath}"`
? `${pandocPath} ${setting.arguments ?? ''} ${setting.customArguments ?? ''} "\${currentPath}"`
: setting.command;

const cmd = renderTemplate(cmdTpl, variables);
Expand All @@ -183,11 +184,7 @@ export async function exportToOo(
output: ['o'],
},
});
const actualOutputPath = path.normalize(
(args.output.startsWith('"') && args.output.endsWith('"')) || (args.output.startsWith("'") && args.output.endsWith("'"))
? args.output.substring(1, args.output.length - 1)
: args.output
);
const actualOutputPath = path.normalize(trimQuotes(args.output));

const actualOutputDir = path.dirname(actualOutputPath);
if (!fs.existsSync(actualOutputDir)) {
Expand Down
9 changes: 9 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,17 @@ export function exec(cmd: string, options?: ExecOptions): Promise<string> {
node_exec(cmd, options, (error, stdout, stderr) => {
if (error) {
reject(error);
console.error(stdout, error);
return;
}
if (stderr && stderr !== '') {
reject(stderr);
console.error(stdout, error);
return;
}
if (stdout?.trim().length === 0 && '1' === localStorage.getItem('debug-plugin')) {
console.log(stdout);
}
resolve(stdout);
});
});
Expand All @@ -71,6 +76,10 @@ export function joinEnvPath(...paths: string[]) {
}
}

export function trimQuotes(s: string) {
return (s.startsWith('"') && s.endsWith('"')) || (s.startsWith("'") && s.endsWith("'")) ? s.substring(1, s.length - 1) : s;
}

/**
* render template
* @example
Expand Down

0 comments on commit 64c1a65

Please sign in to comment.