fix: errors loading relative path img URLs

This commit is contained in:
Corbin Crutchley
2021-11-30 07:12:02 -08:00
parent 773cf54e50
commit 3b45934490
3 changed files with 17 additions and 13 deletions

15
package-lock.json generated
View File

@@ -2134,6 +2134,13 @@
"ignore": "^5.1.4",
"merge2": "^1.3.0",
"slash": "^3.0.0"
},
"dependencies": {
"slash": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/slash/-/slash-3.0.0.tgz",
"integrity": "sha512-g9Q1haeby36OSStwb4ntCGGGaKsaVSjQ68fBxoQcutl5fS1vuY18H3wSt3jFyFtrkx+Kz0V1G85A4MyAdDMi2Q=="
}
}
},
"got": {
@@ -4797,7 +4804,7 @@
}
},
"rehype-img-size": {
"version": "git+https://github.com/unicorn-utterances/rehype-img-size.git#4080a1807ebf6711427b380fc73493b51bab6ca1",
"version": "git+https://github.com/unicorn-utterances/rehype-img-size.git#9b89df92310514d7cc13f1a3614f557870d0e708",
"from": "git+https://github.com/unicorn-utterances/rehype-img-size.git#relative-path-full",
"requires": {
"image-size": "^1.0.0",
@@ -5295,9 +5302,9 @@
"integrity": "sha512-sDl4qMFpijcGw22U5w63KmD3cZJfBuFlVNbVMKje2keoKML7X2UzWbc4XrmEbDwg0NXJc3yv4/ox7b+JWb57kQ=="
},
"slash": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/slash/-/slash-3.0.0.tgz",
"integrity": "sha512-g9Q1haeby36OSStwb4ntCGGGaKsaVSjQ68fBxoQcutl5fS1vuY18H3wSt3jFyFtrkx+Kz0V1G85A4MyAdDMi2Q=="
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/slash/-/slash-4.0.0.tgz",
"integrity": "sha512-3dOsAHXXUkQTpOYcoAxLIorMTp4gIQr5IW3iVb7A7lFIp0VHhnynm9izx6TssdrIcVIESAlVjtnO2K8bg+Coew=="
},
"smart-buffer": {
"version": "4.2.0",

View File

@@ -33,6 +33,7 @@
"remark-stringify": "^10.0.1",
"remark-unwrap-images": "^3.0.1",
"retext-english": "^4.1.0",
"slash": "^4.0.0",
"ts-util-helpers": "^1.0.0-alpha.3",
"typescript": "^4.4.4",
"unified": "^10.1.0",

View File

@@ -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
}