-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
28 changed files
with
297 additions
and
35 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
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,48 @@ | ||
# Asset Import | ||
|
||
By default, imgit is set up to detect and transform Markdown syntax in the source content. This works best for simple documentation and blog websites, but may not be flexible enough for more complex apps authored with frameworks like React. | ||
|
||
To better fit component-based apps, imgit allows importing media assets with `import` statement to manually author the desired HTML. | ||
|
||
Use `imgit:` namespace when importing a media asset to make imgit optimize it and return sources of the generated assets. For example, consider following [Astro](https://astro.build) page: | ||
|
||
```astro | ||
--- | ||
import psd from "imgit:https://example.com/photo.psd"; | ||
import mkv from "imgit:/assets/video.mkv"; | ||
--- | ||
<img src={psd.content.encoded} | ||
height={psd.info.height} | ||
loading="lazy"/> | ||
<video src={mkv.content.encoded} | ||
poster={mkv.content.cover} | ||
height={mkv.info.height} | ||
autoplay loop/> | ||
``` | ||
|
||
Imported asset returns following default export: | ||
|
||
```ts | ||
type AssetImport = { | ||
content: { | ||
encoded: string, | ||
dense?: string, | ||
cover?: string, | ||
safe?: string | ||
}, | ||
info: { | ||
type: string, | ||
height: number, | ||
width: number, | ||
alpha: boolean | ||
} | ||
}; | ||
``` | ||
|
||
— where `content` are the sources of the generated optimized files, which you can assign to the various `src` attributes of the built HTML. Additional `info` object contains metadata describing the imported asset, such its dimensions and MIME type, which may be helpful when building the host component. | ||
|
||
::: tip | ||
When using TypeScript, add `/// <reference types="imgit/client" />` to a `.d.ts` file anywhere under project source directory to correctly resolve virtual asset imports. | ||
::: |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
/// <reference types="astro/client" /> | ||
/// <reference types="imgit/client" /> |
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,31 @@ | ||
--- | ||
import psd from "imgit:https://github.com/elringus/imgit/raw/main/samples/assets/psd.psd"; | ||
import mkv from "imgit:https://github.com/elringus/imgit/raw/main/samples/assets/mkv.mkv"; | ||
--- | ||
|
||
<html lang="en"> | ||
|
||
<head> | ||
<title>Astro Import Sample</title> | ||
<meta charset="utf-8"> | ||
<link rel="icon" href="data:,"> | ||
<style is:global> | ||
body { background: #222; } | ||
img, video { max-width: 100%; height: auto; } | ||
</style> | ||
</head> | ||
|
||
<body> | ||
|
||
<img src={psd.content.encoded} | ||
height={psd.info.height} | ||
loading="lazy"/> | ||
|
||
<video src={mkv.content.encoded} | ||
poster={mkv.content.cover} | ||
height={mkv.info.height} | ||
autoplay loop/> | ||
|
||
</body> | ||
|
||
</html> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
rm -rf dist | ||
tsc --build src | ||
cp src/client.d.ts dist | ||
cp src/client/styles.css dist/client | ||
cp src/plugin/youtube/styles.css dist/plugin/youtube |
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,5 @@ | ||
/* v8 ignore start */ | ||
declare module "imgit:*" { | ||
const asset: import("./server/import.js").AssetImport; | ||
export default asset; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { stages } from "./transform/index.js"; | ||
import { EncodedContent, ContentInfo, BuiltAsset } from "./asset.js"; | ||
|
||
/** Result of importing asset via imgit. */ | ||
export type AssetImport = { | ||
/** Sources of the asset content. */ | ||
content: EncodedContent; | ||
/** Content metadata. */ | ||
info: ContentInfo; | ||
} | ||
|
||
/** Whether specified import identifier is an imgit asset import. */ | ||
export function isImgitAssetImport(importId: string): boolean { | ||
return importId.startsWith("imgit:"); | ||
} | ||
|
||
/** Resolves result (source code) of importing an imgit asset. */ | ||
export async function importImgitAsset(importId: string): Promise<string> { | ||
const url = importId.substring(6); | ||
const asset = <BuiltAsset>{ syntax: { text: "", index: -1, url } }; | ||
stages.resolve.asset(asset); | ||
await stages.fetch.asset(asset); | ||
await stages.probe.asset(asset); | ||
await stages.encode.asset(asset); | ||
const size = stages.build.size(asset); | ||
return `export default { | ||
content: { | ||
encoded: ${buildSrc(asset.content.encoded)}, | ||
dense: ${buildSrc(asset.content.dense)}, | ||
cover: ${buildSrc(asset.content.cover)}, | ||
safe: ${buildSrc(asset.content.safe)} | ||
}, | ||
info: { | ||
type: "${asset.content.info.type}", | ||
height: ${size.height}, | ||
width: ${size.width}, | ||
alpha: ${asset.content.info.alpha} | ||
} | ||
}`; | ||
} | ||
|
||
function buildSrc(path?: string) { | ||
if (path === undefined) return "undefined"; | ||
const src = stages.build.source(path); | ||
return `"${src}"`; | ||
} |
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.