wip markdown process: crash

This commit is contained in:
ozakione 2024-03-25 12:30:00 +01:00
parent 1c825e02d2
commit 9790934ec4
3 changed files with 128 additions and 25 deletions

View file

@ -9,12 +9,21 @@
import fs from 'fs-extra'; import fs from 'fs-extra';
import path from 'path'; import path from 'path';
import {DEFAULT_PLUGIN_ID} from '@docusaurus/utils'; import {
DEFAULT_PLUGIN_ID,
addTrailingPathSeparator,
aliasedSitePath,
docuHash,
} from '@docusaurus/utils';
import Yaml from 'js-yaml'; import Yaml from 'js-yaml';
import {contentAuthorsSchema} from './options'; import {contentAuthorsSchema} from './options';
import type {LoadContext, Plugin} from '@docusaurus/types'; import type {LoadContext, Plugin} from '@docusaurus/types';
import type {PluginOptions, Content} from '@docusaurus/plugin-showcase'; import type {
PluginOptions,
Content,
ShowcaseMetadata,
} from '@docusaurus/plugin-showcase';
// https://stackoverflow.com/a/71166133 // https://stackoverflow.com/a/71166133
const walk = async (dirPath: string): Promise<any[]> => const walk = async (dirPath: string): Promise<any[]> =>
@ -39,6 +48,8 @@ export default function pluginContentShowcase(
); );
const dataDir = path.join(pluginDataDirRoot, options.id ?? DEFAULT_PLUGIN_ID); const dataDir = path.join(pluginDataDirRoot, options.id ?? DEFAULT_PLUGIN_ID);
const showcasePath = path.join(siteDir, options.path);
return { return {
name: 'docusaurus-plugin-showcase', name: 'docusaurus-plugin-showcase',
@ -48,32 +59,33 @@ export default function pluginContentShowcase(
async loadContent(): Promise<Content> { async loadContent(): Promise<Content> {
const files: string[] = await walk(path.join(siteDir, options.path)); const files: string[] = await walk(path.join(siteDir, options.path));
console.log('allFiles:', files.flat(Number.POSITIVE_INFINITY)); const filteredFiles = files
const contentPromises = files
.flat(Number.POSITIVE_INFINITY) .flat(Number.POSITIVE_INFINITY)
.map(async (file) => { .filter((file) => file.endsWith('.yaml'));
const rawYaml = await fs.readFile(path.join(file), 'utf-8');
const yaml = Yaml.load(rawYaml);
const parsedYaml = contentAuthorsSchema.validate(yaml);
if (parsedYaml.error) { const contentPromises = filteredFiles.map(async (file) => {
throw new Error(`Validation failed: ${parsedYaml.error.message}`, { const rawYaml = await fs.readFile(path.join(file), 'utf-8');
cause: parsedYaml.error, const yaml = Yaml.load(rawYaml);
}); const parsedYaml = contentAuthorsSchema.validate(yaml);
}
const {title, description, preview, website, source, tags} = if (parsedYaml.error) {
parsedYaml.value; throw new Error(`Validation failed: ${parsedYaml.error.message}`, {
cause: parsedYaml.error,
});
}
return { const {title, description, preview, website, source, tags} =
title, parsedYaml.value;
description,
preview, return {
website, title,
source, description,
tags, preview,
}; website,
}); source,
tags,
};
});
const content = await Promise.all(contentPromises); const content = await Promise.all(contentPromises);
return { return {
@ -91,7 +103,7 @@ export default function pluginContentShowcase(
await Promise.all( await Promise.all(
content.website.map(async (item) => { content.website.map(async (item) => {
const dataAuthor = await createData( const dataAuthor = await createData(
`${item.title}.json`, `${docuHash(item.title)}.json`,
JSON.stringify(item), JSON.stringify(item),
); );
@ -120,6 +132,68 @@ export default function pluginContentShowcase(
exact: true, exact: true,
}); });
}, },
configureWebpack(_config, isServer, utils, content) {
return {
resolve: {
alias: {
'~blog': pluginDataDirRoot,
},
},
module: {
rules: [
{
test: /\.mdx?$/i,
include: [...showcasePath]
// Trailing slash is important, see https://github.com/facebook/docusaurus/pull/3970
.map(addTrailingPathSeparator),
use: [
{
loader: require.resolve('@docusaurus/mdx-loader'),
options: {
staticDirs: siteConfig.staticDirectories.map((dir) =>
path.resolve(siteDir, dir),
),
siteDir,
metadataPath: (mdxPath: string) => {
// Note that metadataPath must be the same/in-sync as
// the path from createData for each MDX.
const aliasedPath = aliasedSitePath(mdxPath, siteDir);
return path.join(
dataDir,
`${docuHash(aliasedPath)}.json`,
);
},
// For blog posts a title in markdown is always removed
// Blog posts title are rendered separately
removeContentTitle: true,
// Assets allow to convert some relative images paths to
// require() calls
// createAssets: ({
// frontMatter,
// metadata,
// }: {
// frontMatter: Content['website'][number];
// metadata: ShowcaseMetadata;
// }): Assets => ({
// image: frontMatter.preview,
// authorsImageUrls: metadata.frontMatter.preview.map(
// (author) => author.imageURL,
// ),
// }),
markdownConfig: siteConfig.markdown,
},
},
{
loader: path.resolve(__dirname, './markdownLoader.js'),
},
].filter(Boolean),
},
],
},
};
},
}; };
} }

View file

@ -47,4 +47,23 @@ declare module '@docusaurus/plugin-showcase' {
context: LoadContext, context: LoadContext,
options: PluginOptions, options: PluginOptions,
): Promise<Plugin<Content | null>>; ): Promise<Plugin<Content | null>>;
export type ShowcaseMetadata = {
/** Path to the Markdown source, with `@site` alias. */
readonly source: string;
/**
* Used to generate the page h1 heading, tab title, and pagination title.
*/
readonly title: string;
/** Full link including base URL. */
readonly permalink: string;
/**
* Description used in the meta. Could be an empty string (empty content)
*/
readonly description: string;
/** Front matter, as-is. */
readonly frontMatter: Content['website'][number] & {[key: string]: unknown};
/** Tags, normalized. */
readonly tags: TagType[];
};
} }

View file

@ -0,0 +1,10 @@
---
title: Clement;
description: Description from frontmatter
preview: https://github.com/ozakione.png
website: https://github.com/ozakione
source: source
tags: ['favorite', 'opensource']
---
# Hello