mirror of
https://github.com/facebook/docusaurus.git
synced 2025-06-02 10:52:35 +02:00
feat(v2): various markdown string parsing improvements/fixes (#4590)
* extract createExcerpt code in separate file + add bad test * almost working markdown parsing refactor * complete parseMarkdownString refactor * fix tests * fix blog test issue * fix docusaurus utils imports
This commit is contained in:
parent
b743edf5fb
commit
4efe6824b3
15 changed files with 895 additions and 563 deletions
|
@ -198,9 +198,7 @@ Object {
|
|||
\\"slug\\": \\"/headingAsTitle\\",
|
||||
\\"permalink\\": \\"/docs/headingAsTitle\\",
|
||||
\\"version\\": \\"current\\",
|
||||
\\"frontMatter\\": {
|
||||
\\"title\\": \\"My heading as title\\"
|
||||
}
|
||||
\\"frontMatter\\": {}
|
||||
}",
|
||||
"site-docs-hello-md-9df.json": "{
|
||||
\\"unversionedId\\": \\"hello\\",
|
||||
|
|
|
@ -0,0 +1,33 @@
|
|||
/**
|
||||
* Copyright (c) Facebook, Inc. and its affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
import {Joi} from '@docusaurus/utils-validation';
|
||||
|
||||
// TODO complete this frontmatter + add unit tests
|
||||
type DocFrontMatter = {
|
||||
id?: string;
|
||||
title?: string;
|
||||
description?: string;
|
||||
slug?: string;
|
||||
sidebar_label?: string;
|
||||
custom_edit_url?: string;
|
||||
};
|
||||
|
||||
const DocFrontMatterSchema = Joi.object<DocFrontMatter>({
|
||||
id: Joi.string(),
|
||||
title: Joi.string(),
|
||||
description: Joi.string(),
|
||||
slug: Joi.string(),
|
||||
sidebar_label: Joi.string(),
|
||||
custom_edit_url: Joi.string().allow(null),
|
||||
}).unknown();
|
||||
|
||||
export function assertDocFrontMatter(
|
||||
frontMatter: Record<string, unknown>,
|
||||
): asserts frontMatter is DocFrontMatter {
|
||||
Joi.attempt(frontMatter, DocFrontMatterSchema);
|
||||
}
|
|
@ -30,6 +30,7 @@ import getSlug from './slug';
|
|||
import {CURRENT_VERSION_NAME} from './constants';
|
||||
import globby from 'globby';
|
||||
import {getDocsDirPaths} from './versions';
|
||||
import {assertDocFrontMatter} from './docFrontMatter';
|
||||
|
||||
type LastUpdateOptions = Pick<
|
||||
PluginOptions,
|
||||
|
@ -115,11 +116,15 @@ export function processDocMetadata({
|
|||
const {homePageId} = options;
|
||||
const {siteDir, i18n} = context;
|
||||
|
||||
const {frontMatter, contentTitle, excerpt} = parseMarkdownString(content, {
|
||||
source,
|
||||
});
|
||||
assertDocFrontMatter(frontMatter);
|
||||
|
||||
// ex: api/myDoc -> api
|
||||
// ex: myDoc -> .
|
||||
const docsFileDirName = path.dirname(source);
|
||||
|
||||
const {frontMatter = {}, excerpt} = parseMarkdownString(content, source);
|
||||
const {
|
||||
sidebar_label: sidebarLabel,
|
||||
custom_edit_url: customEditURL,
|
||||
|
@ -165,9 +170,9 @@ export function processDocMetadata({
|
|||
});
|
||||
|
||||
// Default title is the id.
|
||||
const title: string = frontMatter.title || baseID;
|
||||
const title: string = frontMatter.title ?? contentTitle ?? baseID;
|
||||
|
||||
const description: string = frontMatter.description || excerpt;
|
||||
const description: string = frontMatter.description ?? excerpt ?? '';
|
||||
|
||||
const permalink = normalizeUrl([versionMetadata.versionPath, docSlug]);
|
||||
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
|
||||
import {DocsMarkdownOption} from '../types';
|
||||
import {getDocsDirPaths} from '../versions';
|
||||
import {replaceMarkdownLinks} from '@docusaurus/utils/lib/markdownLinks';
|
||||
import {replaceMarkdownLinks} from '@docusaurus/utils';
|
||||
|
||||
function getVersion(filePath: string, options: DocsMarkdownOption) {
|
||||
const versionFound = options.versionsMetadata.find((version) =>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue