Skip to content

Improve LLM Texts #248

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 15, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 70 additions & 7 deletions zerops-llm-script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import * as path from 'node:path'
import * as globModule from 'glob'

const frontmatterRegex = /^\n*---(\n.+)*?\n---\n/
const mdxComponentRegex = /<[^>]+>/g
const imageRegex = /!\[.*?\]\(.*?\)/g
const importRegex = /^import\s+.*?from\s+['"].*?['"];?/gm

const contentDir = path.resolve('apps/docs/content')

Expand All @@ -11,7 +14,13 @@ const sliceExt = (file: string) => {
}

const extractLabel = (file: string) => {
return sliceExt(file.split('/').pop() || '')
const pathWithoutExt = sliceExt(file)
const parts = pathWithoutExt.split('/')
return parts.map(part =>
part.split('-')
.map(word => word.charAt(0).toUpperCase() + word.slice(1))
.join(' ')
).join(' > ')
}

function capitalizeDelimiter(str: string): string {
Expand All @@ -21,23 +30,77 @@ function capitalizeDelimiter(str: string): string {
.join('-')
}

function cleanMarkdownContent(content: string): string {
let cleaned = content.replace(frontmatterRegex, '')

cleaned = cleaned.replace(/<FAQItem\s+question="([^"]+)"\s*>([\s\S]*?)<\/FAQItem>/g, (match, question, answer) => {
return `Question: ${question}\nAnswer: ${answer}\n`
})
cleaned = cleaned.replace(/<FAQ>([\s\S]*?)<\/FAQ>/g, (match, content) => {
return content
})

const sections = cleaned.split(/(\|.*\|\n\|.*\|\n(\|.*\|\n)*)/)
let processedContent = ''

for (let i = 0; i < sections.length; i++) {
const section = sections[i]

if (section.trim().startsWith('|')) {
processedContent += section
} else {
let processedSection = section
processedSection = processedSection.replace(mdxComponentRegex, '')
processedSection = processedSection.replace(imageRegex, '')
processedSection = processedSection.replace(importRegex, '')
processedContent += processedSection
}
}

const lines = processedContent.split('\n')
const processedLines: string[] = []
let lastLineWasEmpty = false

for (const line of lines) {
const trimmedLine = line.trim()

if (trimmedLine === '' && lastLineWasEmpty) {
continue
}

processedLines.push(line)
lastLineWasEmpty = trimmedLine === ''
}

return processedLines.join('\n')
}

async function generateContent(
files: string[],
contentDir: string,
header: string
): Promise<string> {
let content = header + '# Start of Zerops documentation\n'
for (const file of files) {
let content = header + '# Start of Zerops documentation\n\n'

for (let i = 0; i < files.length; i++) {
const file = files[i]
console.log(`> Writing '${file}' `)
const fileContent = fs.readFileSync(
path.resolve(contentDir, file),
'utf-8'
)
const contentWithoutFrontmatter = fileContent.replace(frontmatterRegex, '')
const lines = contentWithoutFrontmatter.split('\n')
const filteredLines = lines.filter(line => !line.trim().startsWith('import '))
content += filteredLines.join('\n') + '\n\n'

const cleanedContent = cleanMarkdownContent(fileContent)
const title = extractLabel(file)

if (i === 0) {
content += '-'.repeat(40) + '\n\n'
}

content += `# ${title}\n\n${cleanedContent}\n\n`
content += '-'.repeat(40) + '\n\n'
}

return content
}

Expand Down