Skip to content

Commit 64aedf1

Browse files
authored
Join Url paths with forward slashes instead of OS (#934)
* Join web paths with forward slashes instead of OS - On Windows, the contents images had back slashes in paths intended for web pages and messed up the path processing. * Use joinUrlPath consistently * Normalize parts
1 parent b862bf4 commit 64aedf1

File tree

5 files changed

+24
-13
lines changed

5 files changed

+24
-13
lines changed

convert/convertBooks.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@ import { freeze, postQueries, queries } from '../sab-proskomma-tools';
88
import { SABProskomma } from '../src/lib/sab-proskomma';
99
import type { ConfigTaskOutput } from './convertConfig';
1010
import { convertMarkdownsToMilestones } from './convertMarkdown';
11-
import { createHashedFile, createOutputDir, getHashedNameFromContents } from './fileUtils';
11+
import {
12+
createHashedFile,
13+
createOutputDir,
14+
getHashedNameFromContents,
15+
joinUrlPath
16+
} from './fileUtils';
1217
import { hasAudioExtension, hasImageExtension } from './stringUtils';
1318
import { Promisable, Task, TaskOutput } from './Task';
1419
import { verifyGlossaryEntries } from './verifyGlossaryEntries';
@@ -246,11 +251,11 @@ function updateImgTags(
246251
} else {
247252
const imagePath = createHashedFile(
248253
context.dataDir,
249-
`illustrations/${fileName}`,
254+
joinUrlPath('illustrations', fileName),
250255
context.verbose
251256
);
252257

253-
return match.replace(/src=["'][^"']*["']/, `src="${base}/${imagePath}"`);
258+
return match.replace(/src=["'][^"']*["']/, `src="${joinUrlPath(base, imagePath)}"`);
254259
}
255260
}
256261
);

convert/convertContents.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import path from 'path';
33
import { ScriptureConfig } from '$config';
44
import jsdom from 'jsdom';
55
import { ConfigTaskOutput, parseLangAttribute } from './convertConfig';
6-
import { createHashedFile, createOutputDir, deleteOutputDir } from './fileUtils';
6+
import { createHashedFile, createOutputDir, deleteOutputDir, joinUrlPath } from './fileUtils';
77
import { Task, TaskOutput } from './Task';
88

99
type ContentItem = {
@@ -215,12 +215,12 @@ export function convertContents(
215215
scriptureConfig.bookCollections?.some((collection) => {
216216
if (verbose) console.log(`Searching for ${linkTarget} in ${collection.id}`);
217217
const book = collection.books.find((x) => x.id === linkTarget);
218-
if (book && book.type) {
218+
if (book && book.type && linkTarget) {
219219
// We found a book and the book.type is not default (i.e. undefined)
220220
if (verbose)
221221
console.log(`Found ${linkTarget} in ${collection.id} as ${book.type}`);
222222
linkType = book.type;
223-
linkLocation = `${linkType}/${collection.id}/${linkTarget}`;
223+
linkLocation = joinUrlPath(linkType, collection.id, linkTarget);
224224
return true;
225225
}
226226
});

convert/convertManifest.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'fs';
22
import path from 'path';
3-
import { createHashedFile, createHashedFileFromContents } from './fileUtils';
3+
import { createHashedFile, createHashedFileFromContents, joinUrlPath } from './fileUtils';
44
import { Task, TaskOutput } from './Task';
55

66
export interface ManifestTaskOutput extends TaskOutput {
@@ -55,7 +55,7 @@ export function convertManifest(dataDir: string, verbose: number) {
5555
throw new Error(`Required icon file ${iconPath} does not exist!`);
5656
}
5757

58-
line = line.replace(srcMatch[0], `"src": "./${finalName}"`);
58+
line = line.replace(srcMatch[0], `"src": "${joinUrlPath('.', finalName)}"`);
5959
}
6060
return line;
6161
})

convert/convertStyles.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { copyFileSync, existsSync, readdirSync, readFileSync, writeFileSync } from 'fs';
22
import path from 'path';
33
import { ConfigTaskOutput } from 'convertConfig';
4-
import { createOutputDir } from './fileUtils';
4+
import { createOutputDir, joinUrlPath } from './fileUtils';
55
import { compareVersions } from './stringUtils';
66
import { Task, TaskOutput } from './Task';
77

@@ -99,7 +99,7 @@ export function convertStyles(dataDir: string, configData: ConfigTaskOutput, ver
9999
);
100100
}
101101
}
102-
line = line.replace('/fonts', `$assets/${finalPath}`);
102+
line = line.replace('/fonts', joinUrlPath('$assets', finalPath));
103103
}
104104
return line;
105105
})

convert/fileUtils.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { createHash } from 'crypto';
22
import { copyFileSync, existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from 'fs';
3-
import { basename, extname, join } from 'path';
3+
import { basename, extname, join, posix } from 'path';
44

55
export function getHashedName(dataDir: string, src: string) {
66
const fullPath = join(dataDir, src);
@@ -31,7 +31,7 @@ export function getHashedNameFromContents(contents: string, src: string) {
3131
export function createHashedFile(dataDir: string, src: string, verbose: number, destPrefix = '') {
3232
const fullPath = join(dataDir, src);
3333

34-
const hashedPath = join(destPrefix, getHashedName(dataDir, src));
34+
const hashedPath = joinUrlPath(destPrefix, getHashedName(dataDir, src));
3535
const dest = join('static', hashedPath);
3636

3737
if (hashedPath && !existsSync(dest)) {
@@ -50,7 +50,7 @@ export function createHashedFileFromContents(
5050
verbose: number,
5151
destPrefix = ''
5252
) {
53-
const hashedPath = join(destPrefix, getHashedNameFromContents(contents, src));
53+
const hashedPath = joinUrlPath(destPrefix, getHashedNameFromContents(contents, src));
5454
const dest = join('static', hashedPath);
5555

5656
if (!existsSync(dest)) {
@@ -75,3 +75,9 @@ export function deleteOutputDir(dirPath: string) {
7575
rmSync(dirPath, { recursive: true });
7676
}
7777
}
78+
79+
export function joinUrlPath(...parts: string[]) {
80+
// Always use posix style paths for URLs
81+
const normalized = parts.map((part) => part.replace(/\\/g, '/'));
82+
return posix.join(...normalized);
83+
}

0 commit comments

Comments
 (0)