mirror of
https://github.com/LukeHagar/unicorn-utterances.git
synced 2025-12-06 12:57:44 +00:00
Adds internal and external links
This commit is contained in:
@@ -1,12 +1,26 @@
|
||||
import urljoin from "url-join";
|
||||
|
||||
/**
|
||||
* Matches:
|
||||
* - ftp://
|
||||
* - https://
|
||||
* - //
|
||||
*/
|
||||
export const absolutePathRegex = /^(?:[a-z]+:)?\/\//;
|
||||
|
||||
export const isRelativePath = (str: string) => {
|
||||
return str.startsWith('./') || /^\w/.exec(str);
|
||||
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
|
||||
)
|
||||
}
|
||||
|
||||
export const getFullRelativePath = (slug: string, srcStr: string) => {
|
||||
if (srcStr.startsWith('./')) srcStr = srcStr.slice(2);
|
||||
return isRelativePath(srcStr) ?
|
||||
return isRelativePath(srcStr) && !srcStr.startsWith('/') ?
|
||||
// URLJoin doesn't seem to handle the `./` well
|
||||
urljoin('/posts', slug, srcStr)
|
||||
: srcStr
|
||||
|
||||
Reference in New Issue
Block a user