polish(v2): improve Docusaurus 1 to 2 migration developer experience (#2884)

* improve markdown parsing errors by adding file path to error

* typo commit

* Add default nav item position to right (as v1)

* improve error when sidebar references unexisting document

* parseMarkdownFile: improve errors by providing hint about using "" to avoid parsing errors, if using special characters

* improve subcategory migration error for Unknown sidebar item type

* improve unrecognizedFields error

* typo

* fix inline snapshots

* improve the migration docs

* improve the migration docs

* improve migration doc

* Update migrating-from-v1-to-v2.md

Co-authored-by: Yangshun Tay <tay.yang.shun@gmail.com>
This commit is contained in:
Sébastien Lorber 2020-06-06 05:16:50 +02:00 committed by GitHub
parent 8aa520c314
commit 1003a15d1f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 195 additions and 46 deletions

View file

@ -228,15 +228,14 @@ export function createExcerpt(fileString: string): string | undefined {
return undefined;
}
export function parse(
fileString: string,
): {
type ParsedMarkdown = {
frontMatter: {
[key: string]: any;
};
content: string;
excerpt: string | undefined;
} {
};
export function parseMarkdownString(markdownString: string): ParsedMarkdown {
const options: {} = {
excerpt: (file: matter.GrayMatterFile<string>): void => {
// Hacky way of stripping out import statements from the excerpt
@ -246,8 +245,31 @@ export function parse(
},
};
const {data: frontMatter, content, excerpt} = matter(fileString, options);
return {frontMatter, content, excerpt};
try {
const {data: frontMatter, content, excerpt} = matter(
markdownString,
options,
);
return {frontMatter, content, excerpt};
} catch (e) {
throw new Error(`Error while parsing markdown front matter.
This can happen if you use special characteres like : in frontmatter values (try using "" around that value)
${e.message}`);
}
}
export async function parseMarkdownFile(
source: string,
): Promise<ParsedMarkdown> {
const markdownString = await fs.readFile(source, 'utf-8');
try {
return parseMarkdownString(markdownString);
} catch (e) {
throw new Error(
`Error while parsing markdown file ${source}
${e.message}`,
);
}
}
export function normalizeUrl(rawUrls: string[]): string {