forked from shawngraham/dh-tutorial-platform
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.ts
More file actions
133 lines (118 loc) · 4.31 KB
/
vite.config.ts
File metadata and controls
133 lines (118 loc) · 4.31 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
/// <reference types="vitest/config" />
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import tailwindcss from '@tailwindcss/vite'
import matter from 'gray-matter'
import type { Plugin } from 'vite'
// ---------------------------------------------------------------------------
// Helpers (ported from scripts/compile-lessons.mjs)
// ---------------------------------------------------------------------------
function ensureArray(val: unknown): string[] {
if (Array.isArray(val)) return (val as unknown[]).map(String)
if (val == null || val === '') return []
return [String(val)]
}
function extractMeta(block: string, key: string): string | null {
const match = block.match(new RegExp(`^-\\s*${key}:\\s*(.+)`, 'm'))
return match ? match[1].trim() : null
}
function escapeRe(s: string): string {
return s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
}
function extractCodeBlock(text: string, section: string): string | null {
const pat = new RegExp(
`####\\s*${escapeRe(section)}[^\\n]*\\n[\\s\\S]*?\`\`\`[\\w]*\\n([\\s\\S]*?)\`\`\``
)
const m = text.match(pat)
return m ? m[1] : null
}
function extractTextBlock(text: string, section: string): string | null {
const pat = new RegExp(
`####\\s*${escapeRe(section)}[^\\n]*\\n([\\s\\S]*?)(?=####|$)`
)
const m = text.match(pat)
if (!m) return null
const body = m[1].trim()
const code = body.match(/```\w*\n([\s\S]*?)```/)
return code ? code[1].trimEnd() : body
}
function extractNumberedList(text: string, section: string): string[] {
const pat = new RegExp(
`####\\s*${escapeRe(section)}[^\\n]*\\n([\\s\\S]*?)(?=####|$)`
)
const m = text.match(pat)
if (!m) return []
const items: string[] = []
for (const line of m[1].split('\n')) {
const num = line.match(/^\s*\d+\.\s+(.+)/)
if (num) { items.push(num[1].trim()); continue }
const bul = line.match(/^\s*-\s+(.+)/)
if (bul) items.push(bul[1].trim())
}
return items
}
function parseChallenges(raw: string) {
const blocks = raw.split(/(?=^### Challenge:)/m).filter(b => b.trim())
return blocks.map(block => {
const titleMatch = block.match(/^### Challenge:\s*(.+)/m)
if (!titleMatch) throw new Error('Challenge block missing ### Challenge: header')
const title = titleMatch[1].trim()
const id = extractMeta(block, 'id')
if (!id) throw new Error(`Challenge "${title}" missing id`)
return {
id,
title,
language: extractMeta(block, 'language') ?? 'python',
difficulty: extractMeta(block, 'difficulty') ?? 'beginner',
starterCode: extractCodeBlock(block, 'Starter Code') ?? '',
expectedOutput: extractTextBlock(block, 'Expected Output') ?? '',
hints: extractNumberedList(block, 'Hints'),
solution: extractCodeBlock(block, 'Solution') ?? '',
}
})
}
// ---------------------------------------------------------------------------
// Vite plugin: transforms .md lesson files into JS modules
// ---------------------------------------------------------------------------
function lessonMarkdownPlugin(): Plugin {
return {
name: 'lesson-markdown',
transform(code, id) {
if (!id.endsWith('.md')) return null
const sepIdx = code.indexOf('---challenges---')
let mdContent: string
let challengesRaw: string
if (sepIdx !== -1) {
mdContent = code.slice(0, sepIdx).trim()
challengesRaw = code.slice(sepIdx + '---challenges---'.length).trim()
} else {
mdContent = code.trim()
challengesRaw = ''
}
const { data: fm, content } = matter(mdContent)
const challenges = challengesRaw ? parseChallenges(challengesRaw) : []
const lesson = {
id: fm.id,
title: fm.title,
moduleId: fm.moduleId,
prerequisites: ensureArray(fm.prerequisites),
estimatedTimeMinutes: fm.estimatedTimeMinutes ?? 30,
difficulty: fm.difficulty,
learningObjectives: ensureArray(fm.learningObjectives),
keywords: ensureArray(fm.keywords),
content: content.trim(),
challenges,
}
return { code: `export default ${JSON.stringify(lesson)};`, map: null }
},
}
}
export default defineConfig({
plugins: [lessonMarkdownPlugin(), react(), tailwindcss()],
test: {
globals: true,
environment: 'happy-dom',
setupFiles: ['./src/__tests__/setup.ts'],
css: true,
},
})