docusaurus/packages/docusaurus-migrate/src/frontMatter.ts
Nick Schonning 521eb119a7
chore: add cSpell for spell checking (#6456)
* chore: Add cSpell for spell checking

* chore: exclude map files and remove dups

* chore: exclude more binary files

* chore: remove MD headings

* Update .cspell.json

* fix a few spellings

* fix more

* fix

Signed-off-by: Joshua Chen <sidachen2003@gmail.com>

* fix a few

* oops

Co-authored-by: Joshua Chen <sidachen2003@gmail.com>
2022-01-25 09:40:02 +08:00

74 lines
2.1 KiB
TypeScript

/**
* 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 type {RawData, Data} from './types';
function splitHeader(content: string): RawData {
// New line characters need to handle all operating systems.
const lines = content.split(/\r?\n/);
if (lines[0] !== '---') {
return {};
}
let i = 1;
for (; i < lines.length - 1; i = 1 + i) {
if (lines[i] === '---') {
break;
}
}
return {
header: lines.slice(1, i + 1).join('\n'),
content: lines.slice(i + 1).join('\n'),
};
}
export default function extractMetadata(content: string): Data {
const metadata: {[key: string]: string} = {};
const both = splitHeader(content);
if (!both.content) {
if (!both.header) {
return {metadata, rawContent: content};
}
return {metadata, rawContent: both.header};
}
// New line characters => to handle all operating systems.
const lines = (both.header ?? '').split(/\r?\n/);
for (let i = 0; i < lines.length - 1; i += 1) {
const keyValue = lines[i].split(':');
const key = keyValue[0].trim();
let value = keyValue.slice(1).join(':').trim();
try {
value = JSON.parse(value);
} catch (err) {
// Ignore the error as it means it's not a JSON value.
}
metadata[key] = value;
}
return {metadata, rawContent: both.content};
}
// The new front matter 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;
}
// title: !something needs quotes because otherwise it's a YAML tag.
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 gray-matter throws instead?
return !String(value).match(
/^([\w .\-sàáâãäåçèéêëìíîïðòóôõöùúûüýÿ!;,=+_?'`&#()[\]§%€$])+$/,
);
}