diff --git a/packages/lib/models/Note.test.ts b/packages/lib/models/Note.test.ts index 7a57a0c8a6..bcbbf067f1 100644 --- a/packages/lib/models/Note.test.ts +++ b/packages/lib/models/Note.test.ts @@ -9,6 +9,7 @@ import Tag from './Tag'; import ItemChange from './ItemChange'; import Resource from './Resource'; import { ResourceEntity } from '../services/database/types'; +import { toForwardSlashes } from '../path-utils'; const ArrayUtils = require('../ArrayUtils.js'); async function allItems() { @@ -259,7 +260,8 @@ describe('models/Note', function() { const t1 = r1.updated_time; const t2 = r2.updated_time; - const resourceDirE = markdownUtils.escapeLinkUrl(resourceDir); + const resourceDirE = markdownUtils.escapeLinkUrl(toForwardSlashes(resourceDir)); + const fileProtocol = `file://${process.platform === 'win32' ? '/' : ''}`; const testCases = [ [ @@ -285,17 +287,17 @@ describe('models/Note', function() { [ true, `![](:/${r1.id})`, - `![](file://${resourceDirE}/${r1.id}.jpg?t=${t1})`, + `![](${fileProtocol}${resourceDirE}/${r1.id}.jpg?t=${t1})`, ], [ true, `![](:/${r1.id}) ![](:/${r1.id}) ![](:/${r2.id})`, - `![](file://${resourceDirE}/${r1.id}.jpg?t=${t1}) ![](file://${resourceDirE}/${r1.id}.jpg?t=${t1}) ![](file://${resourceDirE}/${r2.id}.jpg?t=${t2})`, + `![](${fileProtocol}${resourceDirE}/${r1.id}.jpg?t=${t1}) ![](${fileProtocol}${resourceDirE}/${r1.id}.jpg?t=${t1}) ![](${fileProtocol}${resourceDirE}/${r2.id}.jpg?t=${t2})`, ], [ true, `![](:/${r3.id})`, - `![](file://${resourceDirE}/${r3.id}.pdf)`, + `![](${fileProtocol}${resourceDirE}/${r3.id}.pdf)`, ], ]; diff --git a/packages/lib/models/Note.ts b/packages/lib/models/Note.ts index b8a6eaf466..fa226b2141 100644 --- a/packages/lib/models/Note.ts +++ b/packages/lib/models/Note.ts @@ -11,6 +11,7 @@ import Tag from './Tag'; const { sprintf } = require('sprintf-js'); import Resource from './Resource'; import syncDebugLog from '../services/synchronizer/syncDebugLog'; +import { toFileProtocolPath, toForwardSlashes } from '../path-utils'; const { pregQuote, substrWithEllipsis } = require('../string-utils.js'); const { _ } = require('../locale'); const ArrayUtils = require('../ArrayUtils.js'); @@ -167,7 +168,7 @@ export default class Note extends BaseItem { // change, the preview is updated inside the note. This is not // needed for other resources since they are simple links. const timestampParam = isImage ? `?t=${resource.updated_time}` : ''; - const resourcePath = options.useAbsolutePaths ? `file://${Resource.fullPath(resource)}${timestampParam}` : Resource.relativePath(resource); + const resourcePath = options.useAbsolutePaths ? toFileProtocolPath(Resource.fullPath(resource)) + timestampParam : Resource.relativePath(resource); body = body.replace(new RegExp(`:/${id}`, 'gi'), markdownUtils.escapeLinkUrl(resourcePath)); } @@ -181,10 +182,14 @@ export default class Note extends BaseItem { useAbsolutePaths: false, }, options); + const resourceDir = toForwardSlashes(Setting.value('resourceDir')); + let pathsToTry = []; if (options.useAbsolutePaths) { - pathsToTry.push(`file://${Setting.value('resourceDir')}`); - pathsToTry.push(`file://${shim.pathRelativeToCwd(Setting.value('resourceDir'))}`); + pathsToTry.push(`file://${resourceDir}`); + pathsToTry.push(`file:///${resourceDir}`); + pathsToTry.push(`file://${shim.pathRelativeToCwd(resourceDir)}`); + pathsToTry.push(`file:///${shim.pathRelativeToCwd(resourceDir)}`); } else { pathsToTry.push(Resource.baseRelativeDirectoryPath()); } diff --git a/packages/lib/path-utils.ts b/packages/lib/path-utils.ts index 2a90911911..a7c71c943a 100644 --- a/packages/lib/path-utils.ts +++ b/packages/lib/path-utils.ts @@ -146,6 +146,10 @@ export function toSystemSlashes(path: string, os: string = null) { return path.replace(/\\/g, '/'); } +export function toForwardSlashes(path: string) { + return toSystemSlashes(path, 'linux'); +} + export function rtrimSlashes(path: string) { return path.replace(/[\/\\]+$/, ''); }