-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: resolve assets in preview (#31)
* chore: add asset resolver * chore: transform src attribute on img tag * feat: show image via img element in preview * chore: resolve assets specified in url function on styles * feat: preview style assets * fix: should not resolve full url of assets
- Loading branch information
Showing
11 changed files
with
302 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { URL } from 'url' | ||
import path from 'path' | ||
|
||
const assetsEndpoint = '/assets' | ||
|
||
// https://tools.ietf.org/html/rfc3986#section-3.1 | ||
const uriRegExp = /^\w[\w\d+-.]*:/ | ||
|
||
export class AssetResolver { | ||
pathToUrl(assetPath: string, basePath: string): string { | ||
// If it is full url, don't try to resolve it | ||
if (uriRegExp.test(assetPath)) { | ||
return assetPath | ||
} | ||
|
||
const resolved = path.resolve(basePath, assetPath) | ||
return assetsEndpoint + '?path=' + encodeURIComponent(resolved) | ||
} | ||
|
||
urlToPath(assetUrl: string): string | null { | ||
const url = new URL(assetUrl, 'file://') | ||
if (url.pathname !== assetsEndpoint) { | ||
return null | ||
} | ||
|
||
const assetPath = url.searchParams.get('path') | ||
return assetPath && decodeURIComponent(assetPath) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.