-
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
1 parent
971b9a7
commit efe3b29
Showing
15 changed files
with
130 additions
and
51 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
File renamed without changes.
6 changes: 3 additions & 3 deletions
6
kastro/hashcache/compression.js → kastro/compiler/hashcache/compression.js
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
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,8 +1,64 @@ | ||
import { readFile } from "node:fs/promises"; | ||
import { SAXParser } from "sax"; | ||
import { optimize } from "svgo"; | ||
import { tagYaz } from "../../util/html"; | ||
import { getByKey } from "./hashcache/buildCache"; | ||
import SvgoInlineConfig from "./svgoInlineConfig"; | ||
|
||
const removeGlobalProps = (props) => { | ||
for (const prop in props) | ||
if (prop.charCodeAt(0) < 91) | ||
delete props[prop]; | ||
} | ||
|
||
const Image = ({ src, inline }) => { | ||
console.log(src, inline); | ||
return <img src={src} data-inline={inline} />; | ||
/** | ||
* We optimize the inline svgs regardless of the build mode. | ||
* | ||
* @param {!Object<string, *>} props | ||
* @returns {!Promise<string>} | ||
*/ | ||
const compileInlineSvg = ({ src, ...props }) => | ||
getByKey("build/" + src, () => | ||
getByKey(src, () => readFile(src, "utf-8")) | ||
.then((svg) => optimize(svg, SvgoInlineConfig).data)) | ||
.then((svg) => { | ||
removeGlobalProps(props); | ||
delete props.inline; | ||
const parser = new SAXParser(true); | ||
let result = ""; | ||
parser.onopentag = ({ name, attributes }) => { | ||
if (name === "svg") | ||
Object.assign(attributes, props); | ||
result += tagYaz(name, attributes, false); | ||
}; | ||
parser.ontext = (text) => { | ||
result += text; | ||
}; | ||
parser.onclosetag = (tagName) => { | ||
result += `</${tagName}>`; | ||
}; | ||
parser.write(svg).close(); | ||
return result; | ||
}); | ||
|
||
|
||
/** | ||
* @param {!Object<string, *>} props | ||
* @return {!Promise<string>} | ||
*/ | ||
const Image = (props) => { | ||
const { inline, src, ...restProps } = props; | ||
if (inline) { | ||
if (!src.endsWith(".svg")) | ||
throw new Error("We only inline svgs; for other formats serving directly is more efficient"); | ||
return compileInlineSvg(props) | ||
} | ||
|
||
for (const prop in props) | ||
if (prop[0] === prop[0].toUpperCase()) | ||
delete props[prop]; | ||
|
||
return tagYaz("img", props, true); | ||
}; | ||
|
||
export { Image }; |
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,7 @@ | ||
|
||
const StyleSheet = ({ src, shared, SharedCss, PageCss }) => { | ||
(shared ? SharedCss : PageCss).add(src); | ||
return; | ||
} | ||
|
||
export { StyleSheet }; |
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