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:
Endi 2019-12-11 16:33:47 +07:00 committed by GitHub
parent 1f0eb37e19
commit ace93c5a14
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 64 additions and 65 deletions

View file

@ -8,7 +8,7 @@
import fs from 'fs-extra';
import _ from 'lodash';
import path from 'path';
import {normalizeUrl, docuHash} from '@docusaurus/utils';
import {normalizeUrl, docuHash, aliasedSitePath} from '@docusaurus/utils';
import {
PluginOptions,
@ -69,9 +69,10 @@ export default function pluginContentBlog(
opts: Partial<PluginOptions>,
): Plugin<BlogContent | null> {
const options: PluginOptions = {...DEFAULT_OPTIONS, ...opts};
const contentPath = path.resolve(context.siteDir, options.path);
const {siteDir, generatedFilesDir} = context;
const contentPath = path.resolve(siteDir, options.path);
const dataDir = path.join(
context.generatedFilesDir,
generatedFilesDir,
'docusaurus-plugin-content-blog',
);
@ -231,7 +232,7 @@ export default function pluginContentBlog(
blogPosts.map(async blogPost => {
const {id, metadata} = blogPost;
await createData(
// Note that this created data path must be in sync with markdownLoader.ts metadataPath
// Note that this created data path must be in sync with metadataPath provided to mdx-loader
`${docuHash(metadata.source)}.json`,
JSON.stringify(metadata, null, 2),
);
@ -373,13 +374,19 @@ export default function pluginContentBlog(
options: {
remarkPlugins,
rehypePlugins,
// Note that metadataPath must be the same/ in-sync as the path from createData for each MDX
metadataPath: (mdxPath: string) => {
const aliasedSource = aliasedSitePath(mdxPath, siteDir);
return path.join(
dataDir,
`${docuHash(aliasedSource)}.json`,
);
},
},
},
{
loader: path.resolve(__dirname, './markdownLoader.js'),
options: {
dataDir,
siteDir: context.siteDir,
truncateMarker,
},
},

View file

@ -8,14 +8,11 @@
const {parseQuery, getOptions} = require('loader-utils');
import {loader} from 'webpack';
import {truncate} from './blogUtils';
import path from 'path';
import {readFile} from 'fs-extra';
import {aliasedSitePath, docuHash} from '@docusaurus/utils';
export = function(fileString: string) {
const callback = this.async();
const {truncateMarker, siteDir, dataDir} = getOptions(this);
const {truncateMarker}: {truncateMarker: RegExp | string} = getOptions(this);
let finalContent = fileString;
@ -24,20 +21,5 @@ export = function(fileString: string) {
if (truncated) {
finalContent = truncate(fileString, truncateMarker);
}
// 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, finalContent + '\n\n' + metadataStr);
});
return callback && callback(null, finalContent);
} as loader.Loader;