mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-12 08:37:25 +02:00
fix(v2): move metadata export after compiling MDX to avoid weird MDX parsing error. (#2105)
* fix(v2): move metadata export to mdx-loader to prevent any weird mdx parsing * refactor * nits * nits * nits
This commit is contained in:
parent
1f0eb37e19
commit
ace93c5a14
7 changed files with 64 additions and 65 deletions
|
@ -9,7 +9,12 @@ import _ from 'lodash';
|
|||
import globby from 'globby';
|
||||
import fs from 'fs-extra';
|
||||
import path from 'path';
|
||||
import {normalizeUrl, docuHash, objectWithKeySorted} from '@docusaurus/utils';
|
||||
import {
|
||||
normalizeUrl,
|
||||
docuHash,
|
||||
objectWithKeySorted,
|
||||
aliasedSitePath,
|
||||
} from '@docusaurus/utils';
|
||||
import {LoadContext, Plugin, RouteConfig} from '@docusaurus/types';
|
||||
|
||||
import createOrder from './order';
|
||||
|
@ -285,7 +290,7 @@ export default function pluginContentDocs(
|
|||
const routes = await Promise.all(
|
||||
metadataItems.map(async metadataItem => {
|
||||
await createData(
|
||||
// Note that this created data path must be in sync with markdown/index.ts metadataPath
|
||||
// Note that this created data path must be in sync with metadataPath provided to mdx-loader
|
||||
`${docuHash(metadataItem.source)}.json`,
|
||||
JSON.stringify(metadataItem, null, 2),
|
||||
);
|
||||
|
@ -404,13 +409,20 @@ export default function pluginContentDocs(
|
|||
options: {
|
||||
remarkPlugins,
|
||||
rehypePlugins,
|
||||
metadataPath: (mdxPath: string) => {
|
||||
// Note that metadataPath must be the same/ in-sync as the path from createData for each MDX
|
||||
const aliasedSource = aliasedSitePath(mdxPath, siteDir);
|
||||
return path.join(
|
||||
dataDir,
|
||||
`${docuHash(aliasedSource)}.json`,
|
||||
);
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
loader: path.resolve(__dirname, './markdown/index.js'),
|
||||
options: {
|
||||
siteDir,
|
||||
dataDir,
|
||||
docsDir,
|
||||
sourceToPermalink: sourceToPermalink,
|
||||
versionedDir,
|
||||
|
|
|
@ -5,46 +5,25 @@
|
|||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
import path from 'path';
|
||||
import {readFile} from 'fs-extra';
|
||||
import {getOptions} from 'loader-utils';
|
||||
import {loader} from 'webpack';
|
||||
import linkify from './linkify';
|
||||
import {docuHash, aliasedSitePath} from '@docusaurus/utils';
|
||||
|
||||
export = function(fileString: string) {
|
||||
const callback = this.async();
|
||||
const {
|
||||
dataDir,
|
||||
docsDir,
|
||||
siteDir,
|
||||
versionedDir,
|
||||
sourceToPermalink,
|
||||
} = getOptions(this);
|
||||
|
||||
// Replace all markdown linking to correct url
|
||||
const linkifiedStr = linkify(
|
||||
fileString,
|
||||
this.resourcePath,
|
||||
docsDir,
|
||||
siteDir,
|
||||
sourceToPermalink,
|
||||
versionedDir,
|
||||
const {docsDir, siteDir, versionedDir, sourceToPermalink} = getOptions(this);
|
||||
return (
|
||||
callback &&
|
||||
callback(
|
||||
null,
|
||||
linkify(
|
||||
fileString,
|
||||
this.resourcePath,
|
||||
docsDir,
|
||||
siteDir,
|
||||
sourceToPermalink,
|
||||
versionedDir,
|
||||
),
|
||||
)
|
||||
);
|
||||
|
||||
// Read metadata & then embed it to this markdown content
|
||||
// Note that metadataPath must be the same/ in-sync as the path from createData
|
||||
const aliasedSource = aliasedSitePath(this.resourcePath, siteDir);
|
||||
const metadataPath = path.join(dataDir, `${docuHash(aliasedSource)}.json`);
|
||||
|
||||
// Add metadataPath as dependency of this loader result so that we can recompile if metadata is changed
|
||||
this.addDependency(metadataPath);
|
||||
|
||||
readFile(metadataPath, 'utf8', function(err, metadata) {
|
||||
if (err) return callback && callback(err);
|
||||
|
||||
const metadataStr = `export const metadata = ${metadata}`;
|
||||
// We need to add two lines break so that mdx won't mistake it as part of previous paragraph
|
||||
callback && callback(null, linkifiedStr + '\n\n' + metadataStr);
|
||||
});
|
||||
} as loader.Loader;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue