refactor: enable a few TS flags (#6852)

* refactor: enable a few TS flags

* refactor

* revert to working version

* fix

* better

* change
This commit is contained in:
Joshua Chen 2022-03-06 13:09:10 +08:00 committed by GitHub
parent 9f925a42bf
commit 4db0c620de
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
71 changed files with 210 additions and 174 deletions

View file

@ -6,11 +6,11 @@
*/
export const NODE_MAJOR_VERSION = parseInt(
process.versions.node.split('.')[0],
process.versions.node.split('.')[0]!,
10,
);
export const NODE_MINOR_VERSION = parseInt(
process.versions.node.split('.')[1],
process.versions.node.split('.')[1]!,
10,
);

View file

@ -48,12 +48,13 @@ export function replaceMarkdownLinks<T extends ContentPaths>({
let lastCodeFence = '';
const lines = fileString.split('\n').map((line) => {
if (line.trim().startsWith('```')) {
const codeFence = line.trim().match(/^`+/)![0]!;
if (!fencedBlock) {
fencedBlock = true;
[lastCodeFence] = line.trim().match(/^`+/)!;
lastCodeFence = codeFence;
// If we are in a ````-fenced block, all ``` would be plain text instead
// of fences
} else if (line.trim().match(/^`+/)![0].length >= lastCodeFence.length) {
} else if (codeFence.length >= lastCodeFence.length) {
fencedBlock = false;
}
}
@ -71,7 +72,7 @@ export function replaceMarkdownLinks<T extends ContentPaths>({
let mdMatch = mdRegex.exec(modifiedLine);
while (mdMatch !== null) {
// Replace it to correct html link.
const mdLink = mdMatch.groups!.filename;
const mdLink = mdMatch.groups!.filename!;
const sourcesToTry = [
path.resolve(path.dirname(filePath), decodeURIComponent(mdLink)),

View file

@ -18,8 +18,8 @@ export function parseMarkdownHeadingId(heading: string): {
const matches = customHeadingIdRegex.exec(heading);
if (matches) {
return {
text: matches.groups!.text,
id: matches.groups!.id,
text: matches.groups!.text!,
id: matches.groups!.id!,
};
}
return {text: heading, id: undefined};
@ -51,14 +51,13 @@ export function createExcerpt(fileString: string): string | undefined {
// Skip code block line.
if (fileLine.trim().startsWith('```')) {
const codeFence = fileLine.trim().match(/^`+/)![0]!;
if (!inCode) {
inCode = true;
[lastCodeFence] = fileLine.trim().match(/^`+/)!;
lastCodeFence = codeFence;
// If we are in a ````-fenced block, all ``` would be plain text instead
// of fences
} else if (
fileLine.trim().match(/^`+/)![0].length >= lastCodeFence.length
) {
} else if (codeFence.length >= lastCodeFence.length) {
inCode = false;
}
continue;

View file

@ -80,13 +80,13 @@ export function groupTaggedItems<Item>(
// TODO: it's not really clear what should be the behavior if 2 items have
// the same tag but the permalink is different for each
// For now, the first tag found wins
result[tag.permalink] = result[tag.permalink] ?? {
result[tag.permalink] ??= {
tag,
items: [],
};
// Add item to group
result[tag.permalink].items.push(item);
result[tag.permalink]!.items.push(item);
}
items.forEach((item) => {

View file

@ -15,10 +15,17 @@ export function normalizeUrl(rawUrls: string[]): string {
let hasStartingSlash = false;
let hasEndingSlash = false;
const isNonEmptyArray = (arr: string[]): arr is [string, ...string[]] =>
arr.length > 0;
if (!isNonEmptyArray(urls)) {
return '';
}
// If the first part is a plain protocol, we combine it with the next part.
if (urls[0].match(/^[^/:]+:\/*$/) && urls.length > 1) {
const first = urls.shift();
if (first!.startsWith('file:') && urls[0].startsWith('/')) {
const first = urls.shift()!;
if (first.startsWith('file:') && urls[0].startsWith('/')) {
// Force a double slash here, else we lose the information that the next
// segment is an absolute path
urls[0] = `${first}//${urls[0]}`;