dsh-learn-space 1.1.0 → 1.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -1
- package/assets/katex/LICENSE +21 -0
- package/assets/katex/fonts/KaTeX_AMS-Regular.woff2 +0 -0
- package/assets/katex/fonts/KaTeX_Caligraphic-Bold.woff2 +0 -0
- package/assets/katex/fonts/KaTeX_Caligraphic-Regular.woff2 +0 -0
- package/assets/katex/fonts/KaTeX_Fraktur-Bold.woff2 +0 -0
- package/assets/katex/fonts/KaTeX_Fraktur-Regular.woff2 +0 -0
- package/assets/katex/fonts/KaTeX_Main-Bold.woff2 +0 -0
- package/assets/katex/fonts/KaTeX_Main-BoldItalic.woff2 +0 -0
- package/assets/katex/fonts/KaTeX_Main-Italic.woff2 +0 -0
- package/assets/katex/fonts/KaTeX_Main-Regular.woff2 +0 -0
- package/assets/katex/fonts/KaTeX_Math-BoldItalic.woff2 +0 -0
- package/assets/katex/fonts/KaTeX_Math-Italic.woff2 +0 -0
- package/assets/katex/fonts/KaTeX_SansSerif-Bold.woff2 +0 -0
- package/assets/katex/fonts/KaTeX_SansSerif-Italic.woff2 +0 -0
- package/assets/katex/fonts/KaTeX_SansSerif-Regular.woff2 +0 -0
- package/assets/katex/fonts/KaTeX_Script-Regular.woff2 +0 -0
- package/assets/katex/fonts/KaTeX_Size1-Regular.woff2 +0 -0
- package/assets/katex/fonts/KaTeX_Size2-Regular.woff2 +0 -0
- package/assets/katex/fonts/KaTeX_Size3-Regular.woff2 +0 -0
- package/assets/katex/fonts/KaTeX_Size4-Regular.woff2 +0 -0
- package/assets/katex/fonts/KaTeX_Typewriter-Regular.woff2 +0 -0
- package/assets/katex/katex.min.css +1 -0
- package/assets/katex/katex.min.js +1 -0
- package/client.js +118 -16
- package/index.js +131 -18
- package/package.json +2 -2
package/client.js
CHANGED
|
@@ -328,6 +328,11 @@ window.__ModuleLoader__.load({
|
|
|
328
328
|
.ls-md-pre { margin: 4px 0; padding: 8px 10px; background: var(--dsw-alias-bg-layer-1);
|
|
329
329
|
border: 1px solid var(--dsw-alias-border-l1); border-radius: 6px; overflow-x: auto; }
|
|
330
330
|
.ls-md-pre code { font-family: Consolas, monospace; font-size: 12px; white-space: pre; }
|
|
331
|
+
.ls-math { white-space: normal; }
|
|
332
|
+
.ls-math-block { display: block; margin: 6px 0; padding: 2px 0; overflow-x: auto; overflow-y: hidden; text-align: center; }
|
|
333
|
+
.ls-math-block .katex-display { margin: 0; }
|
|
334
|
+
.ls-math-fallback { font-family: Consolas, monospace; font-size: 12px; white-space: pre-wrap;
|
|
335
|
+
color: var(--dsw-alias-state-warning-primary, #b7791f); }
|
|
331
336
|
.ls-tool { align-self: flex-start; font-size: 12px; color: var(--dsw-alias-label-secondary); }
|
|
332
337
|
.ls-chat-err { font-size: 12px; color: var(--dsw-alias-state-danger-primary, #B5432E); padding: 4px 0; }
|
|
333
338
|
.ls-chat-row { display: flex; gap: 8px; margin-top: 8px; align-items: flex-end; }
|
|
@@ -400,6 +405,15 @@ window.__ModuleLoader__.load({
|
|
|
400
405
|
.ls-upload-fill { height: 100%; background: var(--dsw-alias-brand-primary); transition: width .15s; }
|
|
401
406
|
.ls-upload-err { font-size: 12px; color: var(--dsw-alias-state-danger-primary, #B5432E); padding: 4px 0; }
|
|
402
407
|
.ls-file-label { display: inline-flex; align-items: center; gap: 6px; }
|
|
408
|
+
/* 自绘悬停气泡:原生 title 在面板环境不浮现,改用 data-tip + ::after */
|
|
409
|
+
.ls-tip { position: relative; }
|
|
410
|
+
.ls-tip::after { content: attr(data-tip); position: absolute; right: 0; top: calc(100% + 6px); z-index: 40;
|
|
411
|
+
width: max-content; max-width: 220px; padding: 5px 8px; border-radius: 6px; text-align: left;
|
|
412
|
+
background: var(--dsw-alias-bg-layer-2); color: var(--dsw-alias-label-primary);
|
|
413
|
+
border: 1px solid var(--dsw-alias-border-l2); font-size: 11px; font-weight: 400; line-height: 1.5;
|
|
414
|
+
white-space: normal; opacity: 0; pointer-events: none; transition: opacity .12s ease-out; }
|
|
415
|
+
.ls-tip:hover::after, .ls-tip:focus-visible::after { opacity: 1; transition-delay: .3s; }
|
|
416
|
+
.ls-tip-left::after { right: auto; left: 0; }
|
|
403
417
|
.ls-picked { margin-top: 6px; font-size: 12px; color: var(--dsw-alias-label-secondary); }
|
|
404
418
|
.ls-picked-item { display: flex; align-items: center; gap: 6px; padding: 1px 0; word-break: break-all; }
|
|
405
419
|
.ls-picked-x { cursor: pointer; border: none; background: none; color: var(--dsw-alias-label-secondary);
|
|
@@ -692,29 +706,75 @@ window.__ModuleLoader__.load({
|
|
|
692
706
|
)
|
|
693
707
|
}
|
|
694
708
|
|
|
695
|
-
/**
|
|
709
|
+
/** KaTeX 模块级 Promise 单例:注入样式表 + UMD,失败记 katexFailed 不再重试 */
|
|
710
|
+
let katexPromise = null
|
|
711
|
+
let katexFailed = false
|
|
712
|
+
function loadKatex() {
|
|
713
|
+
if (katexFailed) return Promise.reject(new Error('katex asset failed'))
|
|
714
|
+
if (!katexPromise) {
|
|
715
|
+
katexPromise = new Promise((resolve, reject) => {
|
|
716
|
+
const link = document.createElement('link')
|
|
717
|
+
link.rel = 'stylesheet'
|
|
718
|
+
link.href = '/api/learning-space/assets/katex/katex.min.css'
|
|
719
|
+
document.head.appendChild(link)
|
|
720
|
+
const s = document.createElement('script')
|
|
721
|
+
s.src = '/api/learning-space/assets/katex/katex.min.js'
|
|
722
|
+
s.onload = () => {
|
|
723
|
+
if (window.katex) resolve(window.katex)
|
|
724
|
+
else { katexFailed = true; katexPromise = null; reject(new Error('katex 未挂载')) }
|
|
725
|
+
}
|
|
726
|
+
s.onerror = () => { katexFailed = true; katexPromise = null; reject(new Error('katex 资产加载失败')) }
|
|
727
|
+
document.head.appendChild(s)
|
|
728
|
+
})
|
|
729
|
+
}
|
|
730
|
+
return katexPromise
|
|
731
|
+
}
|
|
732
|
+
|
|
733
|
+
/** LaTeX 公式:katex.render 写进 ref 元素(不拼用户文本进 innerHTML),失败降级显示原始 TeX */
|
|
734
|
+
function MathNode({ tex, block }) {
|
|
735
|
+
const ref = useRef(null)
|
|
736
|
+
const [failed, setFailed] = useState(false)
|
|
737
|
+
useEffect(() => {
|
|
738
|
+
let alive = true
|
|
739
|
+
loadKatex().then(k => {
|
|
740
|
+
if (!alive || !ref.current) return
|
|
741
|
+
k.render(tex, ref.current, { displayMode: !!block, throwOnError: false, strict: false })
|
|
742
|
+
}).catch(() => { if (alive) setFailed(true) })
|
|
743
|
+
return () => { alive = false }
|
|
744
|
+
}, [tex, block])
|
|
745
|
+
if (failed) return h('code', { className: 'ls-math-fallback' }, block ? `$$${tex}$$` : `$${tex}$`)
|
|
746
|
+
return h(block ? 'div' : 'span', { className: block ? 'ls-math ls-math-block' : 'ls-math', ref })
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
/** 行内 markdown:**加粗**、`行内码`、$$…$$ / $…$ / \(…\) 公式,其余按原文本 */
|
|
696
750
|
function inlineNodes(text, keyBase) {
|
|
751
|
+
const src = String(text == null ? '' : text)
|
|
697
752
|
const out = []
|
|
698
|
-
const re = /(\*\*[^*]+\*\*|`[^`]
|
|
753
|
+
const re = /(\*\*[^*]+\*\*|`[^`]+`|\$\$[^$\n]+?\$\$|(?<!\\)\$(?![\s$])[^$\n]+?(?<![\s\\])\$(?!\d)|\\\((?:[^\\\n]|\\.)+?\\\)|\\\[(?:[^\\\n]|\\.)+?\\\])/g
|
|
699
754
|
let last = 0, m, i = 0
|
|
700
|
-
while ((m = re.exec(
|
|
701
|
-
if (m.index > last) out.push(
|
|
755
|
+
while ((m = re.exec(src))) {
|
|
756
|
+
if (m.index > last) out.push(src.slice(last, m.index))
|
|
702
757
|
const t = m[0]
|
|
703
758
|
if (t.startsWith('**')) out.push(h('strong', { key: `${keyBase}b${i}` }, t.slice(2, -2)))
|
|
704
|
-
else out.push(h('code', { key: `${keyBase}c${i}`, className: 'ls-md-code' }, t.slice(1, -1)))
|
|
759
|
+
else if (t.startsWith('`')) out.push(h('code', { key: `${keyBase}c${i}`, className: 'ls-md-code' }, t.slice(1, -1)))
|
|
760
|
+
else if (t.startsWith('$$')) out.push(h(MathNode, { key: `${keyBase}m${i}`, tex: t.slice(2, -2), block: false }))
|
|
761
|
+
else if (t.startsWith('$')) out.push(h(MathNode, { key: `${keyBase}m${i}`, tex: t.slice(1, -1), block: false }))
|
|
762
|
+
else out.push(h(MathNode, { key: `${keyBase}m${i}`, tex: t.slice(2, -2), block: false }))
|
|
705
763
|
last = m.index + t.length
|
|
706
764
|
i++
|
|
707
765
|
}
|
|
708
|
-
if (last <
|
|
766
|
+
if (last < src.length) out.push(src.slice(last))
|
|
709
767
|
return out
|
|
710
768
|
}
|
|
711
769
|
|
|
712
|
-
/** 极简 markdown → React
|
|
770
|
+
/** 极简 markdown → React 元素数组(代码块/块级公式/标题/列表/加粗/行内码/行内公式),无法识别的行原样输出 */
|
|
713
771
|
function markdownLite(text) {
|
|
714
772
|
const lines = String(text || '').split(/\r?\n/)
|
|
715
773
|
const els = []
|
|
716
774
|
let code = null
|
|
717
775
|
let list = null
|
|
776
|
+
let math = null
|
|
777
|
+
let mathClose = null
|
|
718
778
|
let k = 0
|
|
719
779
|
const flushCode = () => {
|
|
720
780
|
if (code) { els.push(h('pre', { key: `pre${k++}`, className: 'ls-md-pre' }, h('code', null, code.join('\n')))); code = null }
|
|
@@ -722,6 +782,13 @@ window.__ModuleLoader__.load({
|
|
|
722
782
|
const flushList = () => {
|
|
723
783
|
if (list && list.length) { els.push(h('ul', { key: `ul${k++}`, className: 'ls-md-ul' }, list.map((li, i) => h('li', { key: i }, li)))); list = null }
|
|
724
784
|
}
|
|
785
|
+
const flushMath = () => {
|
|
786
|
+
if (!math) return
|
|
787
|
+
const tex = math.join('\n').trim()
|
|
788
|
+
math = null; mathClose = null
|
|
789
|
+
if (tex) els.push(h(MathNode, { key: `m${k++}`, tex, block: true }))
|
|
790
|
+
}
|
|
791
|
+
const pushMath = (tex) => { els.push(h(MathNode, { key: `m${k++}`, tex, block: true })) }
|
|
725
792
|
for (const line of lines) {
|
|
726
793
|
if (/^\s*```/.test(line)) {
|
|
727
794
|
if (code) flushCode()
|
|
@@ -729,6 +796,33 @@ window.__ModuleLoader__.load({
|
|
|
729
796
|
continue
|
|
730
797
|
}
|
|
731
798
|
if (code) { code.push(line); continue }
|
|
799
|
+
if (math) {
|
|
800
|
+
const closeAt = line.search(mathClose)
|
|
801
|
+
if (closeAt >= 0) {
|
|
802
|
+
const before = line.slice(0, closeAt)
|
|
803
|
+
if (before.trim()) math.push(before)
|
|
804
|
+
flushMath()
|
|
805
|
+
} else math.push(line)
|
|
806
|
+
continue
|
|
807
|
+
}
|
|
808
|
+
// 块级公式:$$…$$ 或 \[…\],同行闭合且后面没正文才当块,否则交给行内处理(免得吞掉尾部文字)
|
|
809
|
+
const openAt = /^\s*(?:\$\$|\\\[)/.test(line) ? (/^\s*\$\$/.test(line) ? line.indexOf('$$') : line.indexOf('\\[')) : -1
|
|
810
|
+
if (openAt >= 0) {
|
|
811
|
+
const dollar = line[openAt] === '$'
|
|
812
|
+
const body = line.slice(openAt + 2)
|
|
813
|
+
const closeRe = dollar ? /\$\$/ : /\\\]/
|
|
814
|
+
const closeAt = body.search(closeRe)
|
|
815
|
+
const after = closeAt >= 0 ? body.slice(closeAt + 2) : ''
|
|
816
|
+
if (closeAt < 0 || !after.trim()) {
|
|
817
|
+
flushList()
|
|
818
|
+
if (closeAt < 0) { math = []; mathClose = closeRe; if (body.trim()) math.push(body) }
|
|
819
|
+
else {
|
|
820
|
+
const tex = body.slice(0, closeAt).trim()
|
|
821
|
+
if (tex) pushMath(tex)
|
|
822
|
+
}
|
|
823
|
+
continue
|
|
824
|
+
}
|
|
825
|
+
}
|
|
732
826
|
const hm = /^(#{1,3})\s+(.*)$/.exec(line)
|
|
733
827
|
if (hm) {
|
|
734
828
|
flushList()
|
|
@@ -745,6 +839,7 @@ window.__ModuleLoader__.load({
|
|
|
745
839
|
}
|
|
746
840
|
flushCode()
|
|
747
841
|
flushList()
|
|
842
|
+
flushMath()
|
|
748
843
|
return els
|
|
749
844
|
}
|
|
750
845
|
|
|
@@ -761,7 +856,7 @@ window.__ModuleLoader__.load({
|
|
|
761
856
|
quiz.state === 'graded'
|
|
762
857
|
? h('span', { className: 'ls-quiz-badge' + (quiz.correct ? ' ok' : ' bad') }, quiz.correct ? '✅ 答对' : '❌ 答错')
|
|
763
858
|
: answered ? h('span', { className: 'ls-quiz-badge wait' }, '已作答,等待反馈') : null),
|
|
764
|
-
quiz.stem ? h('div', { className: 'ls-quiz-stem' }, quiz.stem) : null,
|
|
859
|
+
quiz.stem ? h('div', { className: 'ls-quiz-stem' }, inlineNodes(quiz.stem, 'qs')) : null,
|
|
765
860
|
h('div', { className: 'ls-quiz-opts' },
|
|
766
861
|
(quiz.options || []).map(o =>
|
|
767
862
|
h('button', {
|
|
@@ -772,7 +867,7 @@ window.__ModuleLoader__.load({
|
|
|
772
867
|
onClick: () => { Promise.resolve(onAnswer(o.key)).then(ok => setLocalAnswer(ok ? o.key : null)) },
|
|
773
868
|
},
|
|
774
869
|
h('span', { className: 'ls-quiz-opt-key' }, o.key),
|
|
775
|
-
h('span', null, o.text)))),
|
|
870
|
+
h('span', null, inlineNodes(o.text, `qo${o.key}`))))),
|
|
776
871
|
)
|
|
777
872
|
}
|
|
778
873
|
|
|
@@ -801,9 +896,9 @@ window.__ModuleLoader__.load({
|
|
|
801
896
|
r.node ? h('span', { className: 'ls-qh-node' }, r.node) : null,
|
|
802
897
|
h('span', { className: 'ls-qh-ans' }, `答 ${r.answer}`)),
|
|
803
898
|
openSeq === r.fbSeq ? h('div', { className: 'ls-qh-detail' },
|
|
804
|
-
h('div', { className: 'ls-qh-line' },
|
|
805
|
-
h('div', { className: 'ls-qh-line' },
|
|
806
|
-
h('div', { className: 'ls-qh-line' },
|
|
899
|
+
h('div', { className: 'ls-qh-line' }, '题面:', inlineNodes(r.stem, `qhs${r.fbSeq}`)),
|
|
900
|
+
h('div', { className: 'ls-qh-line' }, '选项:', inlineNodes(r.options, `qho${r.fbSeq}`)),
|
|
901
|
+
h('div', { className: 'ls-qh-line' }, '反馈:', inlineNodes(r.feedback, `qhf${r.fbSeq}`))) : null))),
|
|
807
902
|
)
|
|
808
903
|
}
|
|
809
904
|
|
|
@@ -1015,6 +1110,7 @@ window.__ModuleLoader__.load({
|
|
|
1015
1110
|
|
|
1016
1111
|
/** 材料扩展名白名单(与 Host 上传路由一致),供 file input 的 accept */
|
|
1017
1112
|
const ACCEPT = '.pdf,.md,.txt,.docx,.pptx,.xlsx,.csv,.json,.epub,.png,.jpg,.jpeg,.gif,.webp,.zip'
|
|
1113
|
+
const MATERIAL_TIP = '优先用 md/txt/pdf/图片;docx/pptx/xlsx/epub/zip 仅存储,AI 读不出正文'
|
|
1018
1114
|
|
|
1019
1115
|
/** XHR 上传(fetch 无上传进度):FormData + xhr.upload.onprogress 回传百分比 */
|
|
1020
1116
|
function uploadFiles(topic, fileList, onProgress) {
|
|
@@ -1038,8 +1134,8 @@ window.__ModuleLoader__.load({
|
|
|
1038
1134
|
})
|
|
1039
1135
|
}
|
|
1040
1136
|
|
|
1041
|
-
/** 隐藏 file input +
|
|
1042
|
-
function FilePickButton({ onFiles, label, disabled }) {
|
|
1137
|
+
/** 隐藏 file input + 样式化按钮触发,选中即回调;tip 走 .ls-tip 悬停气泡 */
|
|
1138
|
+
function FilePickButton({ onFiles, label, disabled, tip, tipAlign }) {
|
|
1043
1139
|
const ref = useRef(null)
|
|
1044
1140
|
return h('span', { className: 'ls-file-label' },
|
|
1045
1141
|
h('input', {
|
|
@@ -1047,7 +1143,9 @@ window.__ModuleLoader__.load({
|
|
|
1047
1143
|
onChange: e => { if (e.target.files && e.target.files.length) onFiles(e.target.files); e.target.value = '' },
|
|
1048
1144
|
}),
|
|
1049
1145
|
h('button', {
|
|
1050
|
-
className: 'ls-btn'
|
|
1146
|
+
className: 'ls-btn' + (tip ? ' ls-tip' + (tipAlign === 'left' ? ' ls-tip-left' : '') : ''),
|
|
1147
|
+
type: 'button', disabled,
|
|
1148
|
+
'data-tip': tip || null,
|
|
1051
1149
|
onClick: () => ref.current && ref.current.click(),
|
|
1052
1150
|
}, label || '导入材料'),
|
|
1053
1151
|
)
|
|
@@ -1087,7 +1185,10 @@ window.__ModuleLoader__.load({
|
|
|
1087
1185
|
className: 'ls-h', style: { border: 'none', padding: 0, margin: 0, cursor: 'pointer', userSelect: 'none' },
|
|
1088
1186
|
onClick: () => setOpen(!open),
|
|
1089
1187
|
}, `${open ? '▾' : '▸'} 学习材料${materials.length ? `(${materials.length})` : ''}`),
|
|
1090
|
-
h(FilePickButton, {
|
|
1188
|
+
h(FilePickButton, {
|
|
1189
|
+
onFiles: doUpload, disabled: uploading, label: uploading ? '上传中…' : '导入材料',
|
|
1190
|
+
tip: MATERIAL_TIP,
|
|
1191
|
+
})),
|
|
1091
1192
|
open ? (materials.length === 0
|
|
1092
1193
|
? h('div', { className: 'ls-empty' }, '暂无材料')
|
|
1093
1194
|
: materials.map(m => h('div', { key: m.name, className: 'ls-mat-item' },
|
|
@@ -1547,6 +1648,7 @@ window.__ModuleLoader__.load({
|
|
|
1547
1648
|
h(FilePickButton, {
|
|
1548
1649
|
onFiles: fl => setFiles(prev => [...prev, ...Array.from(fl)]),
|
|
1549
1650
|
disabled: busy, label: files.length ? `+ 追加材料(${files.length})` : '+ 附带材料',
|
|
1651
|
+
tip: MATERIAL_TIP, tipAlign: 'left',
|
|
1550
1652
|
}),
|
|
1551
1653
|
h('button', { className: 'ls-btn', type: 'submit', disabled: busy || !name.trim() },
|
|
1552
1654
|
busy ? '创建中…' : '+ 新建课程'),
|
package/index.js
CHANGED
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
* - tools: learn_guide / learn_status / learn_update(AI 教学时读方法、写进度)
|
|
5
5
|
*/
|
|
6
6
|
import { readFile, writeFile, mkdir, readdir, stat, rm } from 'node:fs/promises'
|
|
7
|
+
import { readFileSync } from 'node:fs'
|
|
7
8
|
import { fileURLToPath } from 'node:url'
|
|
8
9
|
import path from 'node:path'
|
|
9
10
|
import { randomUUID as cryptoRandomUUID } from 'node:crypto'
|
|
@@ -539,6 +540,64 @@ function nodePlanDesc(n) {
|
|
|
539
540
|
/** 插件自身目录(定位 assets/,不依赖 cwd) */
|
|
540
541
|
const PLUGIN_DIR = path.dirname(fileURLToPath(import.meta.url))
|
|
541
542
|
|
|
543
|
+
/** Host 侧 KaTeX:导出报告时直接把公式渲染成 HTML(报告离线单文件,不带 katex.min.js) */
|
|
544
|
+
let katexHostState = 0 // 0 未加载 / 1 可用 / 2 缺失
|
|
545
|
+
let katexHost = null
|
|
546
|
+
function getKatexHost() {
|
|
547
|
+
if (!katexHostState) {
|
|
548
|
+
try {
|
|
549
|
+
// 本包 type:module,require 会把 UMD 当 ESM 解析失败;读源码按 CJS 沙箱求值拿 module.exports
|
|
550
|
+
const code = readFileSync(path.join(PLUGIN_DIR, 'assets', 'katex', 'katex.min.js'), 'utf8')
|
|
551
|
+
const mod = { exports: {} }
|
|
552
|
+
new Function('module', 'exports', code)(mod, mod.exports)
|
|
553
|
+
if (typeof mod.exports.renderToString !== 'function') throw new Error('katex UMD 求值后无 renderToString')
|
|
554
|
+
katexHost = mod.exports
|
|
555
|
+
katexHostState = 1
|
|
556
|
+
} catch { katexHostState = 2 }
|
|
557
|
+
}
|
|
558
|
+
return katexHostState === 1 ? katexHost : null
|
|
559
|
+
}
|
|
560
|
+
let katexCssCache = null
|
|
561
|
+
/** katex.min.css 的 woff2 内联成 data URI(单文件报告没法相对引字体),读过一次即缓存 */
|
|
562
|
+
function getKatexCss() {
|
|
563
|
+
if (katexCssCache) return katexCssCache
|
|
564
|
+
try {
|
|
565
|
+
let css = readFileSync(path.join(PLUGIN_DIR, 'assets', 'katex', 'katex.min.css'), 'utf8')
|
|
566
|
+
css = css.replace(/url\(fonts\/([\w-]+\.woff2)\)/g, (m, name) => {
|
|
567
|
+
const b64 = readFileSync(path.join(PLUGIN_DIR, 'assets', 'katex', 'fonts', name)).toString('base64')
|
|
568
|
+
return `url(data:font/woff2;base64,${b64})`
|
|
569
|
+
})
|
|
570
|
+
katexCssCache = css
|
|
571
|
+
} catch { katexCssCache = '' }
|
|
572
|
+
return katexCssCache
|
|
573
|
+
}
|
|
574
|
+
/** 渲染一条公式;资产缺失记 ctx.wanted 走降级,成功记 ctx.used 供 head 决定是否内嵌样式 */
|
|
575
|
+
function renderTexHost(tex, block, ctx) {
|
|
576
|
+
const k = getKatexHost()
|
|
577
|
+
if (!k || !getKatexCss()) { ctx.wanted = true; return null }
|
|
578
|
+
ctx.used = true
|
|
579
|
+
return k.renderToString(tex, { displayMode: block, throwOnError: false, strict: false })
|
|
580
|
+
}
|
|
581
|
+
/** 报告行内片段 → HTML:非公式/非标记部分转义,公式走 KaTeX,**加粗** 与 `行内码` 同客户端规则 */
|
|
582
|
+
function reportInlineHtml(text, ctx) {
|
|
583
|
+
const src = String(text ?? '')
|
|
584
|
+
const re = /(\*\*[^*]+\*\*|`[^`]+`|\$\$[^$\n]+?\$\$|(?<!\\)\$(?![\s$])[^$\n]+?(?<![\s\\])\$(?!\d)|\\\((?:[^\\\n]|\\.)+?\\\)|\\\[(?:[^\\\n]|\\.)+?\\\])/g
|
|
585
|
+
let out = ''
|
|
586
|
+
let last = 0, m
|
|
587
|
+
while ((m = re.exec(src))) {
|
|
588
|
+
out += escapeHtml(src.slice(last, m.index))
|
|
589
|
+
const t = m[0]
|
|
590
|
+
if (t.startsWith('**')) out += `<strong>${escapeHtml(t.slice(2, -2))}</strong>`
|
|
591
|
+
else if (t.startsWith('`')) out += `<code>${escapeHtml(t.slice(1, -1))}</code>`
|
|
592
|
+
else {
|
|
593
|
+
const tex = t.startsWith('$') && !t.startsWith('$$') ? t.slice(1, -1) : t.slice(2, -2)
|
|
594
|
+
out += renderTexHost(tex, false, ctx) ?? escapeHtml(t)
|
|
595
|
+
}
|
|
596
|
+
last = m.index + t.length
|
|
597
|
+
}
|
|
598
|
+
return out + escapeHtml(src.slice(last))
|
|
599
|
+
}
|
|
600
|
+
|
|
542
601
|
/** HTML 转义:& < > " ' 五字符 */
|
|
543
602
|
function escapeHtml(s) {
|
|
544
603
|
return String(s ?? '').replace(/&/g, '&').replace(/</g, '<')
|
|
@@ -562,36 +621,74 @@ const REPORT_STATUS_COLOR = { done: '#3E6B4F', doing: '#3D5A80', warn: '#B5432E'
|
|
|
562
621
|
* 自包含 HTML 报告(方案 A):mermaid.min.js 全文内嵌,浏览器打开时 startOnLoad 渲染。
|
|
563
622
|
* mermaidJs 为 null 时降级:不嵌脚本,.mermaid 块直接显示源码文本 + 顶部提示。
|
|
564
623
|
*/
|
|
565
|
-
/** 导出报告用极简 markdown → HTML
|
|
566
|
-
function mdToHtmlHost(text) {
|
|
624
|
+
/** 导出报告用极简 markdown → HTML:代码块/块级公式/标题/列表/加粗/行内码/行内公式;无法识别的行按段落(先整体转义防注入) */
|
|
625
|
+
function mdToHtmlHost(text, ctx) {
|
|
567
626
|
const esc = escapeHtml
|
|
568
627
|
const lines = String(text || '').split(/\r?\n/)
|
|
569
628
|
const out = []
|
|
570
629
|
let code = null
|
|
571
630
|
let list = null
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
const
|
|
631
|
+
let math = null
|
|
632
|
+
let mathClose = null
|
|
633
|
+
const flushList = () => { if (list && list.length) { out.push(`<ul>${list.map(li => `<li>${reportInlineHtml(li, ctx)}</li>`).join('')}</ul>`); list = null } }
|
|
634
|
+
const pushBlock = tex => {
|
|
635
|
+
const html = renderTexHost(tex, true, ctx)
|
|
636
|
+
out.push(html ? `<div class="math-block">${html}</div>` : `<pre><code>${esc(`$$${tex}$$`)}</code></pre>`)
|
|
637
|
+
}
|
|
638
|
+
const flushMath = () => {
|
|
639
|
+
if (!math) return
|
|
640
|
+
const tex = math.join('\n').trim()
|
|
641
|
+
math = null; mathClose = null
|
|
642
|
+
if (tex) pushBlock(tex)
|
|
643
|
+
}
|
|
576
644
|
for (const raw of lines) {
|
|
577
645
|
if (/^\s*```/.test(raw)) {
|
|
578
646
|
if (code) { out.push(`<pre><code>${esc(code.join('\n'))}</code></pre>`); code = null } else { flushList(); code = [] }
|
|
579
647
|
continue
|
|
580
648
|
}
|
|
581
649
|
if (code) { code.push(raw); continue }
|
|
650
|
+
if (math) {
|
|
651
|
+
const at = raw.search(mathClose)
|
|
652
|
+
if (at >= 0) {
|
|
653
|
+
const before = raw.slice(0, at)
|
|
654
|
+
if (before.trim()) math.push(before)
|
|
655
|
+
flushMath()
|
|
656
|
+
} else math.push(raw)
|
|
657
|
+
continue
|
|
658
|
+
}
|
|
659
|
+
// 块级公式:$$…$$ 或 \[…\],同行闭合且后面没正文才当块,否则交给行内处理
|
|
660
|
+
const openAt = /^\s*(?:\$\$|\\\[)/.test(raw) ? (/^\s*\$\$/.test(raw) ? raw.indexOf('$$') : raw.indexOf('\\[')) : -1
|
|
661
|
+
if (openAt >= 0) {
|
|
662
|
+
const dollar = raw[openAt] === '$'
|
|
663
|
+
const body = raw.slice(openAt + 2)
|
|
664
|
+
const closeRe = dollar ? /\$\$/ : /\\\]/
|
|
665
|
+
const at = body.search(closeRe)
|
|
666
|
+
const after = at >= 0 ? body.slice(at + 2) : ''
|
|
667
|
+
if (at < 0 || !after.trim()) {
|
|
668
|
+
flushList()
|
|
669
|
+
if (at < 0) { math = []; mathClose = closeRe; if (body.trim()) math.push(body) }
|
|
670
|
+
else {
|
|
671
|
+
const tex = body.slice(0, at).trim()
|
|
672
|
+
if (tex) pushBlock(tex)
|
|
673
|
+
}
|
|
674
|
+
continue
|
|
675
|
+
}
|
|
676
|
+
}
|
|
582
677
|
const hm = /^(#{1,4})\s+(.*)$/.exec(raw)
|
|
583
|
-
if (hm) { flushList(); out.push(`<h4>${
|
|
584
|
-
if (/^\s*[-*]\s+/.test(raw)) { list = list || []; list.push(
|
|
678
|
+
if (hm) { flushList(); out.push(`<h4>${reportInlineHtml(hm[2], ctx)}</h4>`); continue }
|
|
679
|
+
if (/^\s*[-*]\s+/.test(raw)) { list = list || []; list.push(raw.replace(/^\s*[-*]\s+/, '')); continue }
|
|
585
680
|
flushList()
|
|
586
|
-
if (raw.trim()) out.push(`<p>${
|
|
681
|
+
if (raw.trim()) out.push(`<p>${reportInlineHtml(raw, ctx)}</p>`)
|
|
587
682
|
}
|
|
588
683
|
if (code) out.push(`<pre><code>${esc(code.join('\n'))}</code></pre>`)
|
|
589
684
|
flushList()
|
|
685
|
+
flushMath()
|
|
590
686
|
return out.join('\n')
|
|
591
687
|
}
|
|
592
688
|
|
|
593
689
|
function buildReportHtml(topicsData, { all, mermaidJs }) {
|
|
594
690
|
const esc = escapeHtml
|
|
691
|
+
const mctx = { used: false, wanted: false }
|
|
595
692
|
const date = new Date().toISOString().slice(0, 10)
|
|
596
693
|
const title = all ? `学习报告 · 全部课程(${date})` : `学习报告 · ${topicsData[0] ? topicsData[0].name : ''}(${date})`
|
|
597
694
|
const sections = topicsData.map(t => {
|
|
@@ -604,23 +701,23 @@ function buildReportHtml(topicsData, { all, mermaidJs }) {
|
|
|
604
701
|
const nodeRows = nodes.map(n =>
|
|
605
702
|
`<tr><td>${esc(reportLabel(n.label))}</td><td><span class="badge" style="background:${REPORT_STATUS_COLOR[n.status] || REPORT_STATUS_COLOR.todo}">${REPORT_STATUS_LABEL[n.status] || n.status}</span></td></tr>`).join('')
|
|
606
703
|
const quizRows = quizRecords.length ? quizRecords.map(r =>
|
|
607
|
-
`<tr><td>${esc(r.date)}</td><td>第 ${esc(r.index)} / ${esc(r.total)} 题</td><td>${r.correct ? '✅ 答对' : '❌ 答错'}</td><td>${
|
|
704
|
+
`<tr><td>${esc(r.date)}</td><td>第 ${esc(r.index)} / ${esc(r.total)} 题</td><td>${r.correct ? '✅ 答对' : '❌ 答错'}</td><td>${reportInlineHtml(r.stem, mctx)}</td><td>答 ${esc(r.answer)}</td><td>${reportInlineHtml(r.feedback, mctx)}</td></tr>`).join('')
|
|
608
705
|
: '<tr><td colspan="6" class="muted">暂无答题记录</td></tr>'
|
|
609
706
|
const mistakeItemsHtml = mistakes.length
|
|
610
|
-
? `<ul>${mistakes.map(m => `<li>${
|
|
707
|
+
? `<ul>${mistakes.map(m => `<li>${reportInlineHtml(m, mctx)}</li>`).join('')}</ul>`
|
|
611
708
|
: '<p class="muted">暂无错题</p>'
|
|
612
709
|
const journalHtml = journal.length
|
|
613
710
|
? `<ul>${journal.map(l => `<li>${esc(l)}</li>`).join('')}</ul>`
|
|
614
711
|
: '<p class="muted">暂无日志</p>'
|
|
615
712
|
const studyLog = t.studyLog || []
|
|
616
713
|
const studyRows = studyLog.length ? studyLog.map(e =>
|
|
617
|
-
`<tr><td>${esc(e.date)}</td><td>${esc(e.node)}</td><td>${esc(e.mastery || '—')}</td><td>${
|
|
714
|
+
`<tr><td>${esc(e.date)}</td><td>${esc(e.node)}</td><td>${esc(e.mastery || '—')}</td><td>${reportInlineHtml(e.summary, mctx)}</td></tr>`).join('')
|
|
618
715
|
: '<tr><td colspan="4" class="muted">暂无学习记录</td></tr>'
|
|
619
716
|
const nodeNotes = t.nodeNotes || []
|
|
620
717
|
const notesHtml = nodeNotes.length
|
|
621
718
|
? nodeNotes.map(nn => {
|
|
622
719
|
const body = nn.content
|
|
623
|
-
? mdToHtmlHost(nn.content)
|
|
720
|
+
? mdToHtmlHost(nn.content, mctx)
|
|
624
721
|
: nn.desc
|
|
625
722
|
? `<p>${esc(nn.desc)}</p><p class="muted">以上为该节点计划中的学习内容,教学笔记将在学习确认后写入。</p>`
|
|
626
723
|
: '<p class="muted">暂无教学笔记(学到这里时 AI 会写入 notes/<节点>.md)</p>'
|
|
@@ -654,6 +751,8 @@ function buildReportHtml(topicsData, { all, mermaidJs }) {
|
|
|
654
751
|
mermaid.initialize({ startOnLoad: true, theme: 'neutral', securityLevel: 'loose' })
|
|
655
752
|
</script>`
|
|
656
753
|
: `<p class="warn">mermaid 资产缺失,依赖图以源码文本显示。</p>`
|
|
754
|
+
const katexCss = mctx.used ? getKatexCss() : ''
|
|
755
|
+
const katexWarn = mctx.wanted ? '<p class="warn">KaTeX 资产缺失,公式以原始 TeX 显示。</p>' : ''
|
|
657
756
|
return `<!DOCTYPE html>
|
|
658
757
|
<html lang="zh-CN">
|
|
659
758
|
<head>
|
|
@@ -686,13 +785,17 @@ th { background: #f1eee7; }
|
|
|
686
785
|
ul { padding-left: 20px; } li { margin: 3px 0; font-size: 13px; }
|
|
687
786
|
.muted { color: #9a968c; font-size: 13px; }
|
|
688
787
|
.warn { background: #fdf3e7; border: 1px solid #e8c89a; border-radius: 8px; padding: 8px 12px; font-size: 13px; }
|
|
788
|
+
.math-block { overflow-x: auto; overflow-y: hidden; margin: 8px 0; }
|
|
789
|
+
.math-block .katex-display { margin: 0; }
|
|
689
790
|
footer { color: #9a968c; font-size: 12px; text-align: center; margin-top: 12px; }
|
|
690
791
|
</style>
|
|
792
|
+
${katexCss ? `<style>${katexCss}</style>` : ''}
|
|
691
793
|
</head>
|
|
692
794
|
<body>
|
|
693
795
|
<h1>${esc(title)}</h1>
|
|
694
796
|
${all && topicsData.length > 1 ? `<p class="muted">共 ${esc(topicsData.length)} 门课程</p>` : ''}
|
|
695
797
|
${mermaidBoot}
|
|
798
|
+
${katexWarn}
|
|
696
799
|
${sections}
|
|
697
800
|
<footer>由学习空间面板导出 · ${esc(date)}</footer>
|
|
698
801
|
</body>
|
|
@@ -1053,13 +1156,23 @@ export function apply(ctx, config) {
|
|
|
1053
1156
|
send(200, { ok: true })
|
|
1054
1157
|
return
|
|
1055
1158
|
}
|
|
1056
|
-
// 静态资产:mermaid
|
|
1057
|
-
if (req.method === 'GET' && url.pathname
|
|
1159
|
+
// 静态资产:mermaid / KaTeX(文件不可变,缓存一天;缺失 404 JSON 让客户端走降级)
|
|
1160
|
+
if (req.method === 'GET' && url.pathname.startsWith('/api/learning-space/assets/')) {
|
|
1161
|
+
const assetsDir = path.join(PLUGIN_DIR, 'assets')
|
|
1162
|
+
const rel = url.pathname.slice('/api/learning-space/assets/'.length)
|
|
1163
|
+
// 资产名全是 ASCII,用白名单字符挡住路径逃逸,免得 decodeURIComponent 抛错变 500
|
|
1164
|
+
const file = /^[\w./-]+$/.test(rel) ? path.normalize(path.join(assetsDir, rel)) : ''
|
|
1165
|
+
if (!file || !file.startsWith(assetsDir + path.sep)) { send(404, { ok: false, error: 'asset not found' }); return }
|
|
1058
1166
|
let buf
|
|
1059
|
-
try { buf = await readFile(
|
|
1060
|
-
catch { send(404, { ok: false, error: '
|
|
1167
|
+
try { buf = await readFile(file) }
|
|
1168
|
+
catch { send(404, { ok: false, error: 'asset missing' }); return }
|
|
1169
|
+
const ext = path.extname(file).toLowerCase()
|
|
1170
|
+
const type = ext === '.js' ? 'application/javascript; charset=utf-8'
|
|
1171
|
+
: ext === '.css' ? 'text/css; charset=utf-8'
|
|
1172
|
+
: ext === '.woff2' ? 'font/woff2'
|
|
1173
|
+
: 'application/octet-stream'
|
|
1061
1174
|
res.writeHead(200, {
|
|
1062
|
-
'content-type':
|
|
1175
|
+
'content-type': type,
|
|
1063
1176
|
'cache-control': 'public, max-age=86400',
|
|
1064
1177
|
})
|
|
1065
1178
|
res.end(buf)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-learn-space",
|
|
3
|
-
"version": "1.1
|
|
3
|
+
"version": "1.2.1",
|
|
4
4
|
"description": "学习空间:AI 辅导的个人学习面板。摸底定位 → 依赖图规划 → 逐节点教学 + quiz 确认,进度/笔记/错题/学习记录沉淀为 Markdown,支持导出自包含 HTML 学习报告。",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"dsh",
|
|
@@ -34,7 +34,7 @@
|
|
|
34
34
|
"cordis.patch.yml",
|
|
35
35
|
"locale/*.json",
|
|
36
36
|
"icon.svg",
|
|
37
|
-
"assets
|
|
37
|
+
"assets",
|
|
38
38
|
"skills",
|
|
39
39
|
"LICENSE",
|
|
40
40
|
"README.md"
|