-
-
Notifications
You must be signed in to change notification settings - Fork 320
Expand file tree
/
Copy pathdocuments.server.ts
More file actions
655 lines (560 loc) · 17.6 KB
/
documents.server.ts
File metadata and controls
655 lines (560 loc) · 17.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
import fs from 'node:fs'
import fsp from 'node:fs/promises'
import path from 'node:path'
import * as graymatter from 'gray-matter'
import { fetchCached } from '~/utils/cache.server'
import { multiSortBy, removeLeadingSlash } from './utils'
import { env } from './env'
export type Doc = {
filepath: string
}
export type DocFrontMatter = {
title: string
published?: string
exerpt?: string
}
/**
* Return text content of file from remote location
*/
async function fetchRemote(
owner: string,
repo: string,
ref: string,
filepath: string,
) {
const href = new URL(
`${owner}/${repo}/${ref}/${filepath}`,
'https://raw.githubusercontent.com/',
).href
const response = await fetch(href, {
headers: { 'User-Agent': `docs:${owner}/${repo}` },
})
if (!response.ok) {
return null
}
return await response.text()
}
/**
* Validate that a filepath doesn't attempt path traversal
*/
function isValidFilepath(filepath: string): boolean {
const normalized = path.normalize(filepath)
return (
!normalized.startsWith('..') &&
!normalized.includes('/../') &&
!path.isAbsolute(normalized)
)
}
/**
* Return text content of file from local file system
*/
async function fetchFs(repo: string, filepath: string) {
if (!isValidFilepath(filepath)) {
console.warn(`[fetchFs] Invalid filepath rejected: ${filepath}\n`)
return ''
}
const dirname = import.meta.url.split('://').at(-1)!
const baseDir = path.resolve(dirname, `../../../../${repo}`)
const localFilePath = path.resolve(baseDir, filepath)
if (!localFilePath.startsWith(baseDir)) {
console.warn(
`[fetchFs] Path traversal attempt blocked: ${filepath} resolved to ${localFilePath}\n`,
)
return ''
}
const exists = fs.existsSync(localFilePath)
if (!exists) {
console.warn(
`[fetchFs] Tried to read file that does not exist: ${localFilePath}\n`,
)
return ''
}
const file = await fsp.readFile(localFilePath)
return file.toString()
}
/**
* Perform global string replace in text for given key-value map
*/
function replaceContent(
text: string,
frontmatter: graymatter.GrayMatterFile<string>,
) {
let result = text
const replace = frontmatter.data.replace as Record<string, string> | undefined
if (replace) {
Object.entries(replace).forEach(([key, value]) => {
result = result.replace(new RegExp(key, 'g'), value)
})
}
return result
}
/**
* Perform tokenized sections replace in text.
* - Discover sections based on token marker via RegExp in origin file.
* - Discover sections based on token marker via RegExp in target file.
* - replace sections in target file staring from the end, with sections defined in origin file
* @param text File content
* @param frontmatter Referencing file front-matter
* @returns File content with replaced sections
*/
function replaceSections(
text: string,
frontmatter: graymatter.GrayMatterFile<string>,
) {
let result = text
// RegExp defining token pair to dicover sections in the document
// [//]: # (<Section Token>)
const sectionMarkerRegex = /\[\/\/\]: # '([a-zA-Z\d]*)'/g
const sectionRegex =
/\[\/\/\]: # '([a-zA-Z\d]*)'[\S\s]*?\[\/\/\]: # '([a-zA-Z\d]*)'/g
// Find all sections in origin file
const substitutes = new Map<string, RegExpMatchArray>()
for (const match of frontmatter.content.matchAll(sectionRegex)) {
if (match[1] !== match[2]) {
console.error(
`Origin section '${match[1]}' does not have matching closing token (found '${match[2]}'). Please make sure that each section has corresponsing closing token and that sections are not nested.`,
)
}
substitutes.set(match[1], match)
}
// Find all sections in target file
const sections = new Map<string, RegExpMatchArray>()
for (const match of result.matchAll(sectionRegex)) {
if (match[1] !== match[2]) {
console.error(
`Target section '${match[1]}' does not have matching closing token (found '${match[2]}'). Please make sure that each section has corresponsing closing token and that sections are not nested.`,
)
}
sections.set(match[1], match)
}
Array.from(substitutes.entries())
.reverse()
.forEach(([key, value]) => {
const sectionMatch = sections.get(key)
if (sectionMatch) {
result =
result.slice(0, sectionMatch.index!) +
value[0] +
result.slice(
sectionMatch.index! + sectionMatch[0].length,
result.length,
)
}
})
// Remove all section markers from the result
result = result.replaceAll(sectionMarkerRegex, '')
return result
}
/**
* Perform image src replacement in text for given repo pair and ref.
* - Find all instances of markdown inline images
* - Find all instances of markdown html images
* - Replace image src's for given repo pair and ref if matched
* @param text Markdown file content
* @param repoPair Repo pair e.g. "TanStack/router"
* @param ref Branch ref e.g. "main"
* @returns Markdown file content with replaced image src's for given repo pair and ref
*/
function replaceProjectImageBranch(
text: string,
repoPair: string,
ref: string,
) {
const handleReplaceImageSrc = (src: string): string => {
const srcLowered = src.toLowerCase()
const isHttps = srcLowered.startsWith('https://')
const testOrigin = isHttps
? 'https://raw.githubusercontent.com/'
: 'http://raw.githubusercontent.com/'
let validSrcOrigin: string | undefined
if (srcLowered.startsWith(testOrigin)) {
validSrcOrigin = testOrigin
}
// If the image src does not start with a known origin, return the src as is
if (!validSrcOrigin) {
return src
}
// If the image src does not contain the repo pair after the origin, return the src as is
const repoIndex = srcLowered.indexOf(repoPair.toLowerCase())
if (
repoIndex === -1 ||
src.indexOf(validSrcOrigin) + validSrcOrigin.length !== repoIndex
) {
return src
}
// If the branch ref is the same as the target ref, return the src as is
const refIndex = srcLowered.indexOf(ref.toLowerCase())
if (refIndex !== -1 && refIndex === repoIndex + repoPair.length + 1) {
return src
}
// We should only replace the branch ref if it is present and only immediately after the repo pair
// It should NOT be replaced if it is further down the path.
// Example: If the ref is "main" and the src is "https://github.com/TanStack/router/beta/docs/assets/beta.png"
// then the replaced src should be "https://github.com/TanStack/router/main/docs/assets/beta.png"
const branchIndex = repoIndex + repoPair.length + 1
const nextSlashIndex = src.indexOf('/', branchIndex)
const oldRef = src.slice(branchIndex, nextSlashIndex)
const newSrc = src.replace(oldRef, ref)
return newSrc
}
// find all instances of markdown inline images
const markdownInlineImageRegex = /\!(\[([^\]]+)\]\(([^)]+)\))/g
const inlineMarkdownImageMatches = text.matchAll(markdownInlineImageRegex)
for (const match of inlineMarkdownImageMatches) {
const [fullMatch, _, __, src] = match
const newSrc = handleReplaceImageSrc(src)
// No need to replace the src if it is the same as the original
if (newSrc === src) {
continue
}
const replacement = fullMatch.replace(src, newSrc)
text = text.replace(fullMatch, replacement)
}
// find all instances of markdown html images
const markdownImageHtmlTagRegex = /<img[^>]+>/g
const htmlImageTagMatches = text.matchAll(markdownImageHtmlTagRegex)
for (const match of htmlImageTagMatches) {
const [fullMatch] = match
// Match the src attribute on the img tag
// The src could be wrapped with single or double quotes
const src =
fullMatch.match(/src='([^']+)'/)?.[1] ||
fullMatch.match(/src="([^"]+)"/)?.[1]
if (!src) {
continue
}
const newSrc = handleReplaceImageSrc(src)
// No need to replace the src if it is the same as the original
if (newSrc === src) {
continue
}
const replacement = fullMatch.replace(src, newSrc)
text = text.replace(fullMatch, replacement)
}
return text
}
export async function fetchRepoFile(
repoPair: string,
ref: string,
filepath: string,
) {
const key = `${repoPair}:${ref}:${filepath}`
const [owner, repo] = repoPair.split('/')
const ttl = process.env.NODE_ENV === 'development' ? 1 : 10 * 60 * 1000 // 10 minutes
const file = await fetchCached({
key,
ttl,
fn: async () => {
const maxDepth = 4
let currentDepth = 1
let originFrontmatter: graymatter.GrayMatterFile<string> | undefined
while (maxDepth > currentDepth) {
let text: string | null
// Read file contents
try {
if (process.env.NODE_ENV === 'development') {
text = await fetchFs(repo, filepath)
} else {
text = await fetchRemote(owner, repo, ref, filepath)
}
} catch (err) {
console.error(err)
return null
}
if (text === null) {
return null
}
try {
const frontmatter = extractFrontMatter(text)
// If file does not have a ref in front-matter, replace necessary content
if (!frontmatter.data.ref) {
if (originFrontmatter) {
text = replaceContent(text, originFrontmatter)
text = replaceSections(text, originFrontmatter)
}
text = replaceProjectImageBranch(text, repoPair, ref)
return Promise.resolve(text)
}
// If file has a ref to another file, cache current front-matter and load referenced file
filepath = frontmatter.data.ref
originFrontmatter = frontmatter
} catch (error) {
return Promise.resolve(text)
}
currentDepth++
}
return null
},
})
return file
}
export function extractFrontMatter(content: string) {
const result = graymatter.default(content, {
excerpt: (file: any) => (file.excerpt = createRichExcerpt(file.content)),
})
const redirectFrom = normalizeRedirectFrom(result.data.redirect_from)
return {
...result,
data: {
...result.data,
description: createExcerpt(result.content),
redirect_from: redirectFrom,
redirectFrom,
} as { [key: string]: any } & {
description: string
redirect_from?: Array<string>
redirectFrom?: Array<string>
},
}
}
export function normalizeRedirectFrom(value: unknown) {
if (!Array.isArray(value)) {
return undefined
}
const normalizedPaths = Array.from(
new Set(
value.flatMap((item) => {
if (typeof item !== 'string') {
return []
}
const trimmedItem = item.trim()
if (!trimmedItem) {
return []
}
return [trimmedItem.startsWith('/') ? trimmedItem : `/${trimmedItem}`]
}),
),
)
return normalizedPaths.length > 0 ? normalizedPaths : undefined
}
function createExcerpt(text: string, maxLength = 200) {
// Remove Markdown formatting using a basic regex
let cleanText = text
.replace(/!\[.*?\]\(.*?\)/g, '') // Remove images
.replace(/\[.*?\]\(.*?\)/g, '') // Remove links
.replace(/[`*_~>]/g, '') // Remove Markdown special characters
.replace(/#+\s/g, '') // Remove headers
.replace(/-\s/g, '') // Remove list markers
.replace(/\r?\n|\r/g, ' ') // Convert line breaks to spaces
.replace(/\s+/g, ' ') // Collapse multiple spaces
.trim()
// Truncate the text to the desired length, preserving whole words
if (cleanText.length > maxLength) {
cleanText = cleanText.slice(0, maxLength).trim() + '...'
}
return cleanText
}
function createRichExcerpt(text: string, maxLength = 200) {
let cleanText = createExcerpt(text, maxLength)
const imageText = extractFirstImage(text)
if (imageText) {
cleanText = `${imageText}<div style="height:1rem;"></div>${cleanText}`
}
return cleanText
}
function extractFirstImage(markdown: string) {
// Regex to match Markdown image syntax: 
const imageRegex = /!\[(.*?)\]\((.*?)\)/
const match = markdown.match(imageRegex)
return match?.[0]
}
export interface GitHubFile {
name: string
path: string
// sha: string
// size: number
// url: string
// html_url: string
// git_url: string
// download_url: string
type: string
_links: {
self: string
// git: string
// html: string
}
}
export interface GitHubFileNode extends GitHubFile {
children?: Array<GitHubFileNode>
depth: number
parentPath?: string
}
const API_CONTENTS_MAX_DEPTH = 3
export function fetchApiContents(
repoPair: string,
branch: string,
startingPath: string,
) {
const isDev = process.env.NODE_ENV === 'development'
return fetchCached({
key: `${repoPair}:${branch}:${startingPath}`,
ttl: isDev ? 1 : 10 * 60 * 1000, // 10 minute
fn: () => {
return isDev
? fetchApiContentsFs(repoPair, startingPath)
: fetchApiContentsRemote(repoPair, branch, startingPath)
},
})
}
function sortApiContents(contents: Array<GitHubFile>): Array<GitHubFile> {
return multiSortBy(contents, [
(node) => (node.type === 'dir' ? -1 : 1),
(node) => (node.name.startsWith('.') ? -1 : 1),
(node) => node.name,
])
}
async function fetchApiContentsFs(
repoPair: string,
startingPath: string,
): Promise<Array<GitHubFileNode> | null> {
const [_, repo] = repoPair.split('/')
const dirname = import.meta.url.split('://').at(-1)!
const base = path.resolve(dirname, `../../../../${repo}`)
const fsStartPath = path.join(base, removeLeadingSlash(startingPath))
const dirsAndFilesToIgnore = [
'node_modules',
'.git',
'dist',
'test-results',
'.output',
'.netlify',
'.vercel',
'.DS_Store',
'.nitro',
'.tanstack-start/build',
]
async function getContentsForPath(
filePath: string,
): Promise<Array<GitHubFile>> {
try {
const list = await fsp.readdir(filePath, { withFileTypes: true })
return list
.filter((item) => !dirsAndFilesToIgnore.includes(item.name))
.map((item) => {
return {
name: item.name,
path: path.join(filePath, item.name),
type: item.isDirectory() ? 'dir' : 'file',
_links: {
self: path.join(filePath, item.name),
},
}
})
} catch (error) {
if (
error instanceof Error &&
'code' in error &&
error.code === 'ENOENT'
) {
return []
}
throw error
}
}
const data = await getContentsForPath(fsStartPath)
if (data.length === 0) {
return null
}
async function buildFileTree(
nodes: Array<GitHubFile> | undefined,
depth: number,
parentPath: string,
) {
const result: Array<GitHubFileNode> = []
const sortedNodes = sortApiContents(nodes ?? [])
for (const node of sortedNodes) {
const file: GitHubFileNode = {
...node,
depth,
parentPath,
}
if (file.type === 'dir' && depth <= API_CONTENTS_MAX_DEPTH) {
const directoryFiles = await getContentsForPath(file._links.self)
file.children = await buildFileTree(
directoryFiles,
depth + 1,
`${parentPath}${file.path}/`,
)
}
// This replacement is only being done to more accurately mock the GitHub API response
file.path = removeLeadingSlash(file.path.replace(base, ''))
file._links.self = removeLeadingSlash(file._links.self.replace(base, ''))
result.push(file)
}
return result
}
const fileTree = await buildFileTree(data, 0, '')
return fileTree
}
async function fetchApiContentsRemote(
repo: string,
branch: string,
startingPath: string,
): Promise<Array<GitHubFileNode> | null> {
const fetchOptions: RequestInit = {
headers: {
'X-GitHub-Api-Version': '2022-11-28',
Authorization: `token ${env.GITHUB_AUTH_TOKEN}`,
},
}
const res = await fetch(
`https://api.github.com/repos/${repo}/contents/${startingPath}?ref=${branch}`,
fetchOptions,
)
if (!res.ok) {
if (res.status === 404) {
return null
}
throw new Error(
`Failed to fetch repo contents for ${repo}/${branch}/${startingPath}: Status is ${res.statusText} - ${res.status}`,
)
}
const data = (await res.json()) as Array<GitHubFile> | null
if (!Array.isArray(data)) {
console.warn(
'Expected an array of files from GitHub API, but received:\n',
JSON.stringify(data),
)
return null
}
async function buildFileTree(
nodes: Array<GitHubFile> | undefined,
depth: number,
parentPath: string,
) {
const result: Array<GitHubFileNode> = []
const sortedNodes = sortApiContents(nodes ?? [])
for (const node of sortedNodes) {
const file: GitHubFileNode = {
...node,
depth,
parentPath,
}
if (file.type === 'dir' && depth <= API_CONTENTS_MAX_DEPTH) {
const directoryFilesResponse = await fetch(
file._links.self,
fetchOptions,
)
const directoryFiles =
(await directoryFilesResponse.json()) as Array<GitHubFile>
if (!Array.isArray(directoryFiles)) {
console.warn(
`Expected an array of files from GitHub API for directory ${file.path}, but received:\n`,
JSON.stringify(directoryFiles),
)
// Leave file.children undefined
} else {
file.children = await buildFileTree(
directoryFiles,
depth + 1,
`${parentPath}${file.path}/`,
)
}
}
result.push(file)
}
return result
}
const fileTree = await buildFileTree(data, 0, '')
return fileTree
}