Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement RFC "A core story for images" #6344

Merged
merged 113 commits into from
Mar 7, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
113 commits
Select commit Hold shift + click to select a range
d714a7a
feat(assets): Add Vite plugin
Princesseuh Jan 31, 2023
1f7c4a9
feat(images): Set up Image component
Princesseuh Feb 3, 2023
21416d0
Merge branch 'main' into feat/images
Princesseuh Feb 3, 2023
af1bc13
Merge branch 'main' into feat/images
Princesseuh Feb 3, 2023
063aa27
fix(types): Attempt to fix type generation
Princesseuh Feb 3, 2023
25fc671
Revert "fix(types): Attempt to fix type generation"
Princesseuh Feb 3, 2023
bf0dfff
fix(image): Fix image types causing build to fail
Princesseuh Feb 6, 2023
f2a69bd
feat(image): Implement client side part
Princesseuh Feb 10, 2023
e5ca26e
feat(services): Allow arbitrary transforms parameters
Princesseuh Feb 13, 2023
1a9d939
fix(image): Fix paths and types
Princesseuh Feb 13, 2023
848d326
config(types): Update config types to provide completions for availab…
Princesseuh Feb 13, 2023
02295be
feat(image): Add serving in dev
Princesseuh Feb 14, 2023
bca6ebe
feat(image): Improve type error messages
Princesseuh Feb 14, 2023
b0f4dd0
refactor(image): Move sharp's parseParams to baseService
Princesseuh Feb 14, 2023
7e95c5b
refactor(image): Skip work in dev for remote servies
Princesseuh Feb 14, 2023
633a4d8
feat(image): Add support for remote images
Princesseuh Feb 15, 2023
110c56a
feat(image): Add squoosh service
Princesseuh Feb 16, 2023
cf0bc4e
chore: update export map
Princesseuh Feb 16, 2023
07f2b2e
refactor(image): Abstract attributes handling by services
Princesseuh Feb 16, 2023
092c348
config(vercel): Remove test image service
Princesseuh Feb 16, 2023
3b0d49c
feat(image): Support for relative images in Markdown (WIP)
Princesseuh Feb 16, 2023
1a0ea86
feat(images): Add support for relative images in Markdown
Princesseuh Feb 17, 2023
308a850
feat(image): Update with RFC feedback
Princesseuh Feb 23, 2023
23ac7a4
fix(image): Fix alt error on getImage
Princesseuh Feb 23, 2023
3f85ccd
feat(image): Add support for assets validation through content collec…
Princesseuh Feb 23, 2023
aac6bfc
feat(image): Remove validateTransform
Princesseuh Feb 23, 2023
ef92cca
feat(image): Move to assets folder
Princesseuh Feb 23, 2023
2614cfa
fix(image): Fix package exports
Princesseuh Feb 23, 2023
6e8c795
feat(image): Add static imports references to virtual moduel
Princesseuh Feb 23, 2023
b75cd2f
fix(image): Fix images from content collections not working when embe…
Princesseuh Feb 23, 2023
e872d8d
Merge branch 'main' into feat/images
Princesseuh Feb 23, 2023
3753445
chore: lockfile
Princesseuh Feb 23, 2023
544d6ce
fix(markdown): Fix type
Princesseuh Feb 23, 2023
b7d6f68
fix(images): Flag enhanced images behing an experimental flag
Princesseuh Feb 23, 2023
b15b666
Merge branch 'main' into feat/images
Princesseuh Feb 23, 2023
4f0cf02
config(example): Update images example conifg
Princesseuh Feb 23, 2023
08c4240
Merge branch 'main' into feat/images
Princesseuh Feb 24, 2023
52a4388
fix(image): Fix types
Princesseuh Feb 24, 2023
cf2da10
fix(image): Fix asset type for strict, allow arbritary input and outp…
Princesseuh Feb 27, 2023
87fbfc7
chore: fix example check
Princesseuh Feb 27, 2023
5f0463b
feat(image): Emit assets for ESM imported images
Princesseuh Feb 27, 2023
33238d4
Merge branch 'main' into feat/images
Princesseuh Feb 28, 2023
406eff6
Add initial core image tests (#6381)
matthewp Feb 28, 2023
4c92818
feat(images): Make frontmatter extraction more generic than images fo…
Princesseuh Feb 28, 2023
7eb51b4
feat(image): Add support for building
Princesseuh Feb 28, 2023
d1da8e2
fix(image): Fix types
Princesseuh Feb 28, 2023
0cdf5d7
fix(images): Fix compatibility with image integration
Princesseuh Feb 28, 2023
9dc6a56
feat(images): Cuter generation stats
Princesseuh Feb 28, 2023
6bb2e34
fix(images): Globals are unsafe, it turns out
Princesseuh Feb 28, 2023
e908862
fix(images): Only generate images if flag is enabled
Princesseuh Feb 28, 2023
57ae057
fix(images): Only create `addStaticImage` in build
Princesseuh Feb 28, 2023
dc87411
feat(images): Add SSR endpoint
Princesseuh Feb 28, 2023
3932c73
fix(images): Only inject route in SSR
Princesseuh Feb 28, 2023
2aec7ef
Add tests for SSR
matthewp Feb 28, 2023
baa44ae
Remove console.log
matthewp Feb 28, 2023
9526d1c
Updated lockfile
matthewp Feb 28, 2023
57a8e0e
rename to satisfy the link gods
matthewp Feb 28, 2023
8446fb3
skip build tests for now
matthewp Feb 28, 2023
4256bb5
fix(images): Fix WASM files not being copied in dev
Princesseuh Mar 1, 2023
6108458
Merge branch 'main' into feat/images
Princesseuh Mar 1, 2023
46e319b
feat(images): Add quality presets
Princesseuh Mar 1, 2023
acd56b1
fix build tests running
matthewp Mar 1, 2023
49da47c
Remove console.log
matthewp Mar 1, 2023
fab176b
Add tests for getImage
matthewp Mar 1, 2023
46d5ae2
Test local services
matthewp Mar 1, 2023
051798e
Test the content collections API
matthewp Mar 1, 2023
cdc77e2
Add tests for quality
matthewp Mar 1, 2023
caa2900
Skipping content collections test
matthewp Mar 1, 2023
b86c995
feat(image): Add support for `~/assets` alias
Princesseuh Mar 2, 2023
f29be13
test(image): Add tests for aliases in dev
Princesseuh Mar 2, 2023
f919012
Fix windows + content collections
matthewp Mar 2, 2023
15538fe
test(image): Add tests for aliased images and images in Markdown
Princesseuh Mar 2, 2023
eb20571
Fix markdown images being built
matthewp Mar 2, 2023
7ef6b27
Should be posix join
matthewp Mar 2, 2023
1ed8652
Use the optimized image
matthewp Mar 2, 2023
abb72a0
fix test
matthewp Mar 2, 2023
309dcee
Fixes windows smoke
matthewp Mar 2, 2023
87a0d6a
fix(image): Nits
Princesseuh Mar 3, 2023
4db291a
Merge branch 'main' into feat/images
Princesseuh Mar 3, 2023
edc007d
feat(images): Add automatic update for `env.d.ts` when experimental i…
Princesseuh Mar 3, 2023
f67e6c4
fix(images): Revert env.d.ts change if the user opted-out of the expe…
Princesseuh Mar 3, 2023
6bc4ce9
chore: remove bad image example project
Princesseuh Mar 3, 2023
b620671
feat(image): Rename `experimental.images` to `experimental.assets`
Princesseuh Mar 3, 2023
c88a998
fix(images): Remove unused code in MDX integration
Princesseuh Mar 3, 2023
e606a5f
chore: Remove unrelated change
Princesseuh Mar 3, 2023
3ba14ab
fix(images): Remove export from astro/components
Princesseuh Mar 3, 2023
e0fa179
Fix, esm import on Win
matthewp Mar 3, 2023
252cea8
test(images): Add test for format
Princesseuh Mar 3, 2023
d5560e2
Merge branch 'main' into feat/images
Princesseuh Mar 3, 2023
ad701ad
fix(images): Add `client-image.d.ts` to export map
Princesseuh Mar 3, 2023
aacfe61
chore: changeset
Princesseuh Mar 3, 2023
a87c7a7
fix(images): Adjust with feedback, no more automatic refine, asset() …
Princesseuh Mar 6, 2023
13a9974
fix(images): Fix types
Princesseuh Mar 6, 2023
c57a47d
fix(images): Remove unnecessary spread
Princesseuh Mar 6, 2023
0414d49
fix(images): Better types for parseUrl and transform
Princesseuh Mar 6, 2023
be5d6e5
fix(images): Fix types
Princesseuh Mar 6, 2023
7fd3eda
Merge branch 'main' into feat/images
Princesseuh Mar 6, 2023
2d54346
fix(images): Adjust from feedback
Princesseuh Mar 7, 2023
ad7a15e
fix(images): Pass width and height through getHTMLAttributes even if …
Princesseuh Mar 7, 2023
2f38e34
Merge branch 'main' into feat/images
Princesseuh Mar 7, 2023
a124a71
fix(images): Recusirsively extract frontmatter assets
Princesseuh Mar 7, 2023
84c4997
fix(images): Use a reduce instead
Princesseuh Mar 7, 2023
c1299d3
feat(images): Add support for data: URIs
Princesseuh Mar 7, 2023
8c13a6b
chore: changeset
Princesseuh Mar 7, 2023
1f7d910
docs(images): Misc docs fixes
Princesseuh Mar 7, 2023
9b53c92
Update .changeset/gold-rocks-cry.md
Princesseuh Mar 7, 2023
97f0cdd
Update .changeset/gold-rocks-cry.md
Princesseuh Mar 7, 2023
93acb22
Update packages/astro/src/@types/astro.ts
Princesseuh Mar 7, 2023
516493b
Update packages/astro/src/assets/services/service.ts
Princesseuh Mar 7, 2023
7404291
Update packages/astro/src/assets/services/service.ts
Princesseuh Mar 7, 2023
b7f9f9c
Update packages/astro/src/assets/services/service.ts
Princesseuh Mar 7, 2023
5f5a06c
Update packages/astro/src/assets/types.ts
Princesseuh Mar 7, 2023
c912ffb
Update packages/astro/src/assets/types.ts
Princesseuh Mar 7, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat(image): Support for relative images in Markdown (WIP)
  • Loading branch information
Princesseuh committed Feb 16, 2023
commit 3b0d49c04c980302e76d7a4ab3340b1d8e64efcc
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions examples/images/src/content/blog/hello.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Hello

This is my website! This is a Markdown page with a relative image!

![Elsa and Anna from Frozen](./_elsa-and-anna.jpg)
1 change: 1 addition & 0 deletions examples/images/src/env.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/// <reference path="../.astro/types.d.ts" />
/// <reference types="astro/client-image" />
18 changes: 18 additions & 0 deletions examples/images/src/pages/hello.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
import { getEntryBySlug } from 'astro:content';

const entry = await getEntryBySlug('blog', 'hello');
const { Content, headings } = await entry.render();
---

<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<Content />
</body>
</html>
2 changes: 1 addition & 1 deletion packages/astro/src/image/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export function isESMImportedImage(src: ImageMetadata | string): src is ImageMet
return typeof src === 'object';
}

async function getConfiguredService(): Promise<ImageService> {
export async function getConfiguredService(): Promise<ImageService> {
if (!globalThis.astroImageService) {
const { default: service }: { default: ImageService } = await import(
// @ts-ignore
Expand Down
28 changes: 24 additions & 4 deletions packages/astro/src/vite-plugin-markdown/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { renderMarkdown } from '@astrojs/markdown-remark';
import {
InvalidAstroDataError,
safelyGetAstroData,
safelyGetAstroData
} from '@astrojs/markdown-remark/dist/internal.js';
import fs from 'fs';
import matter from 'gray-matter';
Expand All @@ -14,6 +14,7 @@ import { AstroError, AstroErrorData, MarkdownError } from '../core/errors/index.
import type { LogOptions } from '../core/logger/core.js';
import { warn } from '../core/logger/core.js';
import { isMarkdownFile } from '../core/util.js';
import { getConfiguredService as getConfiguredImageService } from '../image/internal.js';
import type { PluginMetadata } from '../vite-plugin-astro/types.js';
import { escapeViteEnvReferences, getFileInfo } from '../vite-plugin-utils/index.js';

Expand Down Expand Up @@ -68,15 +69,19 @@ export default function markdown({ settings, logging }: AstroPluginOptions): Plu
const { fileId, fileUrl } = getFileInfo(id, settings.config);
const rawFile = await fs.promises.readFile(fileId, 'utf-8');
const raw = safeMatter(rawFile, id);

const imageService = getConfiguredImageService();
const renderResult = await renderMarkdown(raw.content, {
...settings.config.markdown,
fileURL: new URL(`file://${fileId}`),
contentDir: getContentPaths(settings.config).contentDir,
frontmatter: raw.data,
imageService,
});

const html = renderResult.code;
let html = renderResult.code;
const { headings } = renderResult.metadata;
const { relImagePaths } = renderResult.vfile.data as { relImagePaths: string[] };
const astroData = safelyGetAstroData(renderResult.vfile.data);
if (astroData instanceof InvalidAstroDataError) {
throw new AstroError(AstroErrorData.InvalidFrontmatterInjectionError);
Expand All @@ -96,8 +101,23 @@ export default function markdown({ settings, logging }: AstroPluginOptions): Plu
const code = escapeViteEnvReferences(`
import { Fragment, jsx as h } from ${JSON.stringify(astroJsxRuntimeModulePath)};
${layout ? `import Layout from ${JSON.stringify(layout)};` : ''}
import { getImage } from "astro:assets";

const images = {
${relImagePaths.map(
(entry) =>
`'${entry}': await getImage({ src: await (await import('${entry}')).default, alt: ''})`
)}
}

const html = () => {
const rawHTML = ${JSON.stringify(html)}
}

const html = ${JSON.stringify(html)};
${JSON.stringify(html).replaceAll(
/\$AstroImage\$(.*)\$AstroImage\$/gm,
'${images["$1"].src}'
)}\`;

export const frontmatter = ${JSON.stringify(frontmatter)};
export const file = ${JSON.stringify(fileId)};
Expand All @@ -106,7 +126,7 @@ export default function markdown({ settings, logging }: AstroPluginOptions): Plu
return ${JSON.stringify(raw.content)};
}
export function compiledContent() {
return html;
return html();
}
export function getHeadings() {
return ${JSON.stringify(headings)};
Expand Down
2 changes: 1 addition & 1 deletion packages/integrations/mdx/src/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ export async function getRemarkPlugins(
}

// Apply last in case user plugins resolve relative image paths
remarkPlugins.push(toRemarkContentRelImageError(config));
// remarkPlugins.push(toRemarkContentRelImageError(config));

return remarkPlugins;
}
Expand Down
6 changes: 4 additions & 2 deletions packages/markdown/remark/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type {
import { toRemarkInitializeAstroData } from './frontmatter-injection.js';
import { loadPlugins } from './load-plugins.js';
import { rehypeHeadingIds } from './rehype-collect-headings.js';
import toRemarkContentRelImageError from './remark-content-rel-image-error.js';
import toRemarkCollectRelativeImages from './remark-collect-rel-images.js';
import remarkPrism from './remark-prism.js';
import scopedStyles from './remark-scoped-styles.js';
import remarkShiki from './remark-shiki.js';
Expand All @@ -21,6 +21,7 @@ import markdownToHtml from 'remark-rehype';
import remarkSmartypants from 'remark-smartypants';
import { unified } from 'unified';
import { VFile } from 'vfile';
import { rehypeRelativeImages } from './rehype-rel-images.js';

export { rehypeHeadingIds } from './rehype-collect-headings.js';
export * from './types.js';
Expand Down Expand Up @@ -90,7 +91,7 @@ export async function renderMarkdown(
}

// Apply later in case user plugins resolve relative image paths
parser.use([toRemarkContentRelImageError({ contentDir })]);
parser.use([toRemarkCollectRelativeImages()]);

parser.use([
[
Expand All @@ -107,6 +108,7 @@ export async function renderMarkdown(
parser.use([[plugin, pluginOpts]]);
});

parser.use(rehypeRelativeImages(await opts.imageService));
parser.use([rehypeHeadingIds, rehypeRaw]).use(rehypeStringify, { allowDangerousHtml: true });

let vfile: MarkdownVFile;
Expand Down
43 changes: 43 additions & 0 deletions packages/markdown/remark/src/rehype-rel-images.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { visit } from 'unist-util-visit';
import type { MarkdownVFile } from './types.js';

export function rehypeRelativeImages(imageService: any) {
return () =>
function (tree: any, file: MarkdownVFile) {
visit(tree, (node) => {
if (node.type !== 'element') return;
if (node.tagName !== 'img') return;

if (node.properties?.src) {
if (isRelativePath(node.properties.src.toString())) {
node.properties.$injectURL = node.properties.src;

if (!node.properties.width) {
node.properties.$injectWidth = true;
}

if (!node.properties.height) {
node.properties.$injectHeight = true;
}
}
}
});
};
}

function isRelativePath(path: string) {
return startsWithDotDotSlash(path) || startsWithDotSlash(path);
}

function startsWithDotDotSlash(path: string) {
const c1 = path[0];
const c2 = path[1];
const c3 = path[2];
return c1 === '.' && c2 === '.' && c3 === '/';
}

function startsWithDotSlash(path: string) {
const c1 = path[0];
const c2 = path[1];
return c1 === '.' && c2 === '/';
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,28 @@ import { visit } from 'unist-util-visit';
import { pathToFileURL } from 'url';
import type { VFile } from 'vfile';

export default function toRemarkCollectRelativeimages() {
return () =>
function (tree: any, vfile: VFile) {
if (typeof vfile?.path !== 'string') return;

const relImagePaths = new Set<string>();
visit(tree, 'image', function raiseError(node: Image) {
if (isRelativePath(node.url)) {
relImagePaths.add(node.url);
}
});
if (relImagePaths.size === 0) return;

vfile.data.relImagePaths = Array.from(relImagePaths);
};
}

/**
* `src/content/` does not support relative image paths.
* This plugin throws an error if any are found
*/
export default function toRemarkContentRelImageError({ contentDir }: { contentDir: URL }) {
export function toRemarkContentRelImageError({ contentDir }: { contentDir: URL }) {
return function remarkContentRelImageError() {
return (tree: any, vfile: VFile) => {
if (typeof vfile?.path !== 'string') return;
Expand Down
1 change: 1 addition & 0 deletions packages/markdown/remark/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export interface MarkdownRenderingOptions extends AstroMarkdownOptions {
contentDir: URL;
/** Used for frontmatter injection plugins */
frontmatter?: Record<string, any>;
imageService?: any;
}

export interface MarkdownHeading {
Expand Down