mirror of
https://github.com/LukeHagar/unicorn-utterances.git
synced 2025-12-06 12:57:44 +00:00
fix: errors loading relative path img URLs
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
import urljoin from "url-join";
|
||||
import {join} from "path";
|
||||
import slash from "slash";
|
||||
|
||||
/**
|
||||
* Matches:
|
||||
@@ -11,17 +13,11 @@ export const absolutePathRegex = /^(?:[a-z]+:)?\/\//;
|
||||
export const isRelativePath = (str: string) => {
|
||||
const isAbsolute = absolutePathRegex.exec(str);
|
||||
if (isAbsolute) return false;
|
||||
// Matches simple `test.png` links
|
||||
const simpleRelativePath = /^\w/.exec(str);
|
||||
return str.startsWith('./') || str.startsWith('/') || (
|
||||
simpleRelativePath
|
||||
)
|
||||
return true;
|
||||
}
|
||||
|
||||
export const getFullRelativePath = (slug: string, srcStr: string) => {
|
||||
if (srcStr.startsWith('./')) srcStr = srcStr.slice(2);
|
||||
return isRelativePath(srcStr) && !srcStr.startsWith('/') ?
|
||||
// URLJoin doesn't seem to handle the `./` well
|
||||
urljoin('/posts', slug, srcStr)
|
||||
return isRelativePath(srcStr) ?
|
||||
slash(join('/posts', slug, srcStr))
|
||||
: srcStr
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user