refactor: pass file contents
This commit is contained in:
parent
e4d38c3498
commit
e17e8735be
4 changed files with 9 additions and 9 deletions
|
@ -980,7 +980,7 @@ export interface AstroConfig extends z.output<typeof AstroConfigSchema> {
|
||||||
|
|
||||||
export interface ContentEntryType {
|
export interface ContentEntryType {
|
||||||
extensions: string[];
|
extensions: string[];
|
||||||
getEntryInfo(params: { fileUrl: URL }): Promise<{
|
getEntryInfo(params: { fileUrl: URL; contents: string }): Promise<{
|
||||||
data: Record<string, unknown>;
|
data: Record<string, unknown>;
|
||||||
/**
|
/**
|
||||||
* Used for error hints to point to correct line and location
|
* Used for error hints to point to correct line and location
|
||||||
|
|
|
@ -79,7 +79,10 @@ export function astroContentImportPlugin({
|
||||||
unvalidatedSlug: string,
|
unvalidatedSlug: string,
|
||||||
rawData: string;
|
rawData: string;
|
||||||
if (contentEntryType) {
|
if (contentEntryType) {
|
||||||
const info = await contentEntryType.getEntryInfo({ fileUrl: pathToFileURL(fileId) });
|
const info = await contentEntryType.getEntryInfo({
|
||||||
|
fileUrl: pathToFileURL(fileId),
|
||||||
|
contents: rawContents,
|
||||||
|
});
|
||||||
body = info.body;
|
body = info.body;
|
||||||
unvalidatedData = info.data;
|
unvalidatedData = info.data;
|
||||||
unvalidatedSlug = info.slug;
|
unvalidatedSlug = info.slug;
|
||||||
|
|
|
@ -1,15 +1,13 @@
|
||||||
import type { AstroIntegration } from 'astro';
|
import type { AstroIntegration } from 'astro';
|
||||||
import type { InlineConfig } from 'vite';
|
import type { InlineConfig } from 'vite';
|
||||||
import _Markdoc from '@markdoc/markdoc';
|
import _Markdoc from '@markdoc/markdoc';
|
||||||
import fs from 'node:fs';
|
|
||||||
import { parseFrontmatter } from './utils.js';
|
import { parseFrontmatter } from './utils.js';
|
||||||
import { fileURLToPath } from 'node:url';
|
import { fileURLToPath } from 'node:url';
|
||||||
|
|
||||||
const contentEntryType = {
|
const contentEntryType = {
|
||||||
extensions: ['.mdoc'],
|
extensions: ['.mdoc'],
|
||||||
async getEntryInfo({ fileUrl }: { fileUrl: URL }) {
|
async getEntryInfo({ fileUrl, contents }: { fileUrl: URL; contents: string }) {
|
||||||
const rawContents = await fs.promises.readFile(fileUrl, 'utf-8');
|
const parsed = parseFrontmatter(contents, fileURLToPath(fileUrl));
|
||||||
const parsed = parseFrontmatter(rawContents, fileURLToPath(fileUrl));
|
|
||||||
return {
|
return {
|
||||||
data: parsed.data,
|
data: parsed.data,
|
||||||
body: parsed.content,
|
body: parsed.content,
|
||||||
|
|
|
@ -25,9 +25,8 @@ export type MdxOptions = Omit<typeof markdownConfigDefaults, 'remarkPlugins' | '
|
||||||
|
|
||||||
const contentEntryType = {
|
const contentEntryType = {
|
||||||
extensions: ['.mdx'],
|
extensions: ['.mdx'],
|
||||||
async getEntryInfo({ fileUrl }: { fileUrl: URL }) {
|
async getEntryInfo({ fileUrl, contents }: { fileUrl: URL; contents: string }) {
|
||||||
const rawContents = await fs.readFile(fileUrl, 'utf-8');
|
const parsed = parseFrontmatter(contents, fileURLToPath(fileUrl));
|
||||||
const parsed = parseFrontmatter(rawContents, fileURLToPath(fileUrl));
|
|
||||||
return {
|
return {
|
||||||
data: parsed.data,
|
data: parsed.data,
|
||||||
body: parsed.content,
|
body: parsed.content,
|
||||||
|
|
Loading…
Reference in a new issue