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

feat: handle simple mdx rendering #11633

Merged
merged 21 commits into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
fix invalidation of the module when there's no store available
  • Loading branch information
ematipico committed Aug 8, 2024
commit ed8b7b0ea1b2b309a7391062c524ed626e1afb51
23 changes: 11 additions & 12 deletions packages/astro/src/content/data-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { MarkdownHeading } from '@astrojs/markdown-remark';
import * as devalue from 'devalue';
import { imageSrcToImportId, importIdToSymbolName } from '../assets/utils/resolveImports.js';
import { AstroError, AstroErrorData } from '../core/errors/index.js';
import {DEFERRED_MODULE} from './consts.js';
import {CONTENT_MODULE_FLAG, DEFERRED_MODULE} from './consts.js';

const SAVE_DEBOUNCE_MS = 500;

Expand Down Expand Up @@ -139,8 +139,8 @@ export class DataStore {
assets.forEach((asset) => this.addAssetImport(asset, filePath));
}

addModuleImport(fileName: string, specifier: string) {
const id = contentModuleToId(fileName, specifier);
addModuleImport(fileName: string) {
const id = contentModuleToId(fileName);
if (id) {
this.#moduleImports.set(fileName, id);
// We debounce the writes to disk because addAssetImport is called for every image in every file,
Expand Down Expand Up @@ -221,6 +221,7 @@ export default new Map([${exports.join(', ')}]);
export default new Map([\n${lines.join(',\n')}]);
`;
try {
console.log("writing to disk now")
await fs.writeFile(filePath, code);
} catch (err) {
throw new AstroError({
Expand Down Expand Up @@ -323,10 +324,8 @@ export default new Map([\n${lines.join(',\n')}]);
this.addAssetImport(assetImport, fileName),
addAssetImports: (assets: Array<string>, fileName: string) =>
this.addAssetImports(assets, fileName),
addModuleImport: (fileName: string, specifier: string) =>
this.addModuleImport(fileName, specifier),
// addModuleImports: (modules: Array<string>, fileName: string) =>
// this.addModuleImports(modules, fileName),
addModuleImport: (fileName: string) =>
this.addModuleImport(fileName),
};
}
/**
Expand Down Expand Up @@ -441,7 +440,7 @@ export interface ScopedDataStore {
* @param specifier
* @returns
*/
addModuleImport: (fileName: string, specifier: string) => void;
addModuleImport: (fileName: string) => void;
}

/**
Expand Down Expand Up @@ -471,11 +470,11 @@ function dataStoreSingleton() {
}

// TODO: find a better place to put this image
export function contentModuleToId(fileName: string, specifier: string) {
const params = new URLSearchParams(specifier);
export function contentModuleToId(fileName: string) {
const params = new URLSearchParams(DEFERRED_MODULE);
params.set('fileName', fileName);
params.set(DEFERRED_MODULE, 'true');
return `${specifier}?${params.toString()}`;
params.set(CONTENT_MODULE_FLAG, 'true');
return `${DEFERRED_MODULE}?${params.toString()}`;
}

/** @internal */
Expand Down
4 changes: 2 additions & 2 deletions packages/astro/src/content/loaders/glob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export function glob(globOptions: GlobOptions): Loader {

if (existingEntry && existingEntry.digest === digest && existingEntry.filePath) {
if (entryType.extensions.includes('.mdx')) {
store.addModuleImport(existingEntry.filePath, 'astro:content-layer-deferred-module');
store.addModuleImport(existingEntry.filePath);
}

if (existingEntry.rendered?.metadata?.imagePaths?.length) {
Expand All @@ -130,7 +130,7 @@ export function glob(globOptions: GlobOptions): Loader {
});

if (entryType.extensions.includes('.mdx')) {
store.addModuleImport(relativePath, 'astro:content-layer-deferred-module');
store.addModuleImport(relativePath);
ematipico marked this conversation as resolved.
Show resolved Hide resolved
store.set({ id, data: parsedData, body, filePath: relativePath, digest, deferredRender: true });
} else if (entryType.getRenderFunction) {
let render = renderFunctionByContentType.get(entryType);
Expand Down
2 changes: 1 addition & 1 deletion packages/astro/src/content/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ const CONTENT_LAYER_IMAGE_REGEX = /__ASTRO_IMAGE_="([^"]+)"/g;
async function updateImageReferencesInBody(html: string, fileName: string) {
// @ts-expect-error Virtual module
const { default: imageAssetMap } = await import('astro:asset-imports');

const imageObjects = new Map<string, GetImageResult>();

// @ts-expect-error Virtual module resolved at runtime
Expand Down
5 changes: 2 additions & 3 deletions packages/astro/src/content/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ import { isYAMLException } from '../core/errors/utils.js';
import type { Logger } from '../core/logger/core.js';
import {
CONTENT_FLAGS,
CONTENT_LAYER_TYPE,
DEFERRED_MODULE,
CONTENT_LAYER_TYPE, CONTENT_MODULE_FLAG,
IMAGE_IMPORT_PREFIX,
PROPAGATED_ASSET_FLAG,
} from './consts.js';
Expand Down Expand Up @@ -477,7 +476,7 @@ export function hasContentFlag(viteId: string, flag: (typeof CONTENT_FLAGS)[numb

export function isDeferredModule(viteId: string): boolean {
const flags = new URLSearchParams(viteId.split('?')[1] ?? '');
return flags.has(DEFERRED_MODULE);
return flags.has(CONTENT_MODULE_FLAG);
}

async function loadContentConfig({
Expand Down
13 changes: 11 additions & 2 deletions packages/astro/src/content/vite-plugin-content-virtual-mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,20 @@ export function astroContentVirtualModPlugin({
}

if (id === ASSET_IMPORTS_RESOLVED_STUB_ID) {
return 'export default new Map()';
const assetImportsFile = new URL(ASSET_IMPORTS_FILE, settings.dotAstroDir);
if (!fs.existsSync(assetImportsFile)) {
return 'export default new Map()';
}
return fs.readFileSync(assetImportsFile, 'utf-8');

}

if (id === MODULES_MJS_VIRTUAL_ID) {
return 'export default new Map()';
const modules = new URL(MODULES_IMPORTS_FILE, settings.dotAstroDir);
if (!fs.existsSync(modules)) {
return 'export default new Map()';
}
return fs.readFileSync(modules, 'utf-8');
}
},
renderChunk(code, chunk) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ import H2 from "../src/components/H2.astro";
<H2>Iguana</H2>

This is a rendered entry

![file](./shuttle.jpg)
Loading