Skip to content

Commit b5234fa

Browse files
committed
Adjust based on PR feedback
1 parent 711800e commit b5234fa

3 files changed

Lines changed: 49 additions & 33 deletions

File tree

‎proxy/bin/checkLinks.ts‎

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as fs from 'fs'
22
import path from 'path'
33
import { fileURLToPath } from 'url'
44
import type { MenuItemsStructure } from '../src/types'
5-
import { parseRawContent } from '../src/utils'
5+
import { isDefined, parseRawContent } from '../src/utils'
66

77
const __filename = fileURLToPath(import.meta.url)
88
const __dirname = path.dirname(__filename)
@@ -28,67 +28,71 @@ interface ParsedDoc {
2828
links: ExtractedLink[]
2929
}
3030

31-
const isRecord = (value: unknown): value is Record<string, unknown> =>
32-
typeof value === 'object' && value !== null
31+
interface MarkdownNode {
32+
type?: string
33+
id?: string
34+
href?: string
35+
text?: string
36+
title?: string
37+
linkUrl?: string | null
38+
imageUrl?: string
39+
tokens?: MarkdownNode[]
40+
items?: MarkdownNode[]
41+
header?: MarkdownNode[]
42+
rows?: MarkdownNode[][]
43+
}
3344

3445
const collectSectionsAndLinks = (
35-
nodes: unknown[],
46+
nodes: MarkdownNode[],
3647
sectionIds: Set<string>,
3748
links: ExtractedLink[],
3849
) => {
3950
for (const node of nodes) {
40-
if (!isRecord(node)) {
41-
continue
42-
}
43-
44-
const type = typeof node.type === 'string' ? node.type : ''
51+
const type = node.type ?? ''
4552

4653
if (type === 'code' || type === 'codespan') {
4754
continue
4855
}
4956

50-
if (type === 'section' && typeof node.id === 'string' && node.id) {
57+
if (type === 'section' && node.id) {
5158
sectionIds.add(node.id)
5259
}
5360

54-
if (type === 'link' && typeof node.href === 'string') {
61+
if (type === 'link' && isDefined(node.href)) {
5562
links.push({
5663
href: node.href,
57-
text: typeof node.text === 'string' ? node.text : '',
64+
text: node.text ?? '',
5865
kind: 'link',
5966
})
6067
}
6168

62-
if (
63-
(type === 'image' || type === 'image-static') &&
64-
typeof node.href === 'string'
65-
) {
69+
if ((type === 'image' || type === 'image-static') && isDefined(node.href)) {
6670
links.push({
6771
href: node.href,
68-
text: typeof node.text === 'string' ? node.text : '',
72+
text: node.text ?? '',
6973
kind: 'image',
7074
})
71-
if (typeof node.linkUrl === 'string' && node.linkUrl) {
75+
if (node.linkUrl) {
7276
links.push({
7377
href: node.linkUrl,
74-
text: typeof node.text === 'string' ? node.text : '',
78+
text: node.text ?? '',
7579
kind: 'link',
7680
})
7781
}
7882
}
7983

8084
if (type === 'project-example') {
81-
if (typeof node.imageUrl === 'string' && node.imageUrl) {
85+
if (node.imageUrl) {
8286
links.push({
8387
href: node.imageUrl,
84-
text: typeof node.title === 'string' ? node.title : '',
88+
text: node.title ?? '',
8589
kind: 'image',
8690
})
8791
}
88-
if (typeof node.linkUrl === 'string' && node.linkUrl) {
92+
if (node.linkUrl) {
8993
links.push({
9094
href: node.linkUrl,
91-
text: typeof node.title === 'string' ? node.title : '',
95+
text: node.title ?? '',
9296
kind: 'link',
9397
})
9498
}
@@ -139,14 +143,13 @@ export const getRouteMap = (structure: MenuItemsStructure) => {
139143
const routeMap = new Map<string, string>()
140144

141145
for (const item of structure) {
142-
if (item.children && item.children.length > 0) {
143-
for (const child of item.children) {
144-
const route = `/${item.path}/${child.path}`
145-
routeMap.set(route, `${item.path}/${child.path}/index.md`)
146-
}
147-
} else {
148-
const route = `/${item.path}`
149-
routeMap.set(route, `${item.path}/index.md`)
146+
const routes =
147+
item.children && item.children.length > 0
148+
? item.children.map((child) => `/${item.path}/${child.path}`)
149+
: [`/${item.path}`]
150+
151+
for (const route of routes) {
152+
routeMap.set(route, `${route.slice(1)}/index.md`)
150153
}
151154
}
152155

@@ -329,7 +332,7 @@ export const checkDocsLinks = ({
329332
continue
330333
}
331334

332-
if (fragment !== undefined) {
335+
if (isDefined(fragment)) {
333336
if (!fragment || fragment.startsWith('#')) {
334337
issues.push({
335338
file: doc.file,

‎proxy/src/utils/helpers.test.ts‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, expect, test } from 'bun:test'
2-
import { chunk, kebabCase } from './helpers'
2+
import { chunk, isDefined, kebabCase } from './helpers'
33

44
describe('kebabCase', () => {
55
test('should convert a string to kebab case', () => {
@@ -20,3 +20,13 @@ describe('chunk', () => {
2020
expect(chunk([], 2)).toEqual([])
2121
})
2222
})
23+
24+
describe('isDefined', () => {
25+
test('should return true for defined values and false for null or undefined', () => {
26+
expect(isDefined('')).toBe(true)
27+
expect(isDefined(0)).toBe(true)
28+
expect(isDefined(false)).toBe(true)
29+
expect(isDefined(null)).toBe(false)
30+
expect(isDefined(undefined)).toBe(false)
31+
})
32+
})

‎proxy/src/utils/helpers.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ export const preferLocalData = ({
3131
export const kebabCase = (str: string) =>
3232
str.replaceAll(/[^a-zA-Z0-9]+/g, '-').toLowerCase()
3333

34+
export const isDefined = <T>(value: T | undefined | null): value is T =>
35+
value !== undefined && value !== null
36+
3437
export const chunk = <T>(array: T[], size: number): T[][] => {
3538
const chunkedArr: T[][] = []
3639
let index = 0

0 commit comments

Comments
 (0)