mirror of
https://github.com/facebook/docusaurus.git
synced 2025-07-26 04:57:50 +02:00
chore(v2): ability to test the migration cli easily (#3113)
* ability to test the migration cli easily * add node scripts to help test migration cli (locally + CI) * add test for frontmatter quotify * more tests for shouldQuotifyFrontMatter * typo * updated yarn lock
This commit is contained in:
parent
6aec331963
commit
a0ef8939a1
7 changed files with 69 additions and 20 deletions
|
@ -0,0 +1,27 @@
|
|||
/**
|
||||
* 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 {shouldQuotifyFrontMatter} from '../frontMatter';
|
||||
|
||||
describe('frontMatter', () => {
|
||||
test('shouldQuotifyFrontMatter', () => {
|
||||
expect(shouldQuotifyFrontMatter(['id', 'value'])).toEqual(false);
|
||||
expect(
|
||||
shouldQuotifyFrontMatter([
|
||||
'title',
|
||||
"Some title front matter with allowed special chars like sàáâãäåçèéêëìíîïðòóôõöùúûüýÿ!;,=+-_?'`&#()[]§%€$",
|
||||
]),
|
||||
).toEqual(false);
|
||||
|
||||
expect(shouldQuotifyFrontMatter(['title', 'Special char :'])).toEqual(true);
|
||||
|
||||
expect(shouldQuotifyFrontMatter(['title', 'value!'])).toEqual(false);
|
||||
expect(shouldQuotifyFrontMatter(['title', '!value'])).toEqual(true);
|
||||
|
||||
expect(shouldQuotifyFrontMatter(['tags', '[tag1, tag2]'])).toEqual(false);
|
||||
});
|
||||
});
|
|
@ -50,3 +50,27 @@ export default function extractMetadata(content: string): Data {
|
|||
}
|
||||
return {metadata, rawContent: both.content};
|
||||
}
|
||||
|
||||
// The new frontmatter parser need some special chars to
|
||||
export function shouldQuotifyFrontMatter([key, value]: [
|
||||
string,
|
||||
string,
|
||||
]): boolean {
|
||||
if (key === 'tags') {
|
||||
return false;
|
||||
}
|
||||
if (String(value).match(/^("|').+("|')$/)) {
|
||||
return false;
|
||||
}
|
||||
// TODO weird graymatter case
|
||||
// title: !something need quotes
|
||||
// but not title: something!
|
||||
if (!String(value).trim().match(/^\w.*/)) {
|
||||
return true;
|
||||
}
|
||||
// TODO this is not ideal to have to maintain such a list of allowed chars
|
||||
// maybe we should quotify if graymatter throws instead?
|
||||
return !String(value).match(
|
||||
/^([\w .\-sàáâãäåçèéêëìíîïðòóôõöùúûüýÿ!;,=+_?'`&#()[\]§%€$])+$/,
|
||||
);
|
||||
}
|
||||
|
|
|
@ -17,7 +17,7 @@ import {
|
|||
ClassicPresetEntries,
|
||||
SidebarEntries,
|
||||
} from './types';
|
||||
import extractMetadata from './frontMatter';
|
||||
import extractMetadata, {shouldQuotifyFrontMatter} from './frontMatter';
|
||||
import migratePage from './transform';
|
||||
import sanitizeMD from './sanitizeMD';
|
||||
import path from 'path';
|
||||
|
@ -46,13 +46,9 @@ function sanitizedFileContent(
|
|||
): string {
|
||||
const extractedData = extractMetadata(content);
|
||||
const extractedMetaData = Object.entries(extractedData.metadata).reduce(
|
||||
(metaData, value) => {
|
||||
return `${metaData}\n${value[0]}: ${
|
||||
value[0] === 'tags' ||
|
||||
!!String(value[1]).match(/^(\w| |\.|-)+$/m) ||
|
||||
String(value[1]).match(/^("|').+("|')$/)
|
||||
? value[1]
|
||||
: `"${value[1]}"`
|
||||
(metaData, [key, value]) => {
|
||||
return `${metaData}\n${key}: ${
|
||||
shouldQuotifyFrontMatter([key, value]) ? `"${value}"` : value
|
||||
}`;
|
||||
},
|
||||
'',
|
||||
|
@ -377,7 +373,7 @@ function createPages(newDir: string, siteDir: string): void {
|
|||
function createDefaultLandingPage(newDir: string) {
|
||||
const indexPage = `import Layout from "@theme/Layout";
|
||||
import React from "react";
|
||||
|
||||
|
||||
export default () => {
|
||||
return <Layout />;
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue