dsh-learn-space 1.2.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.
Files changed (4) hide show
  1. package/README.md +1 -1
  2. package/client.js +20 -4
  3. package/index.js +116 -13
  4. package/package.json +1 -1
package/README.md CHANGED
@@ -8,7 +8,7 @@ DeepSeek Harness(dsh)的学习空间插件:AI 辅导的个人学习面板
8
8
  - **学习面板**:侧边栏入口,课程进度、知识节点(点开看教学内容)、错题本、学习记录、答题历史
9
9
  - **公式渲染**:面板内用内置 KaTeX 渲染 LaTeX(行内 `$…$`、块级 `$$…$$`),资产加载失败时降级显示原始 TeX
10
10
  - **数据沉淀**:`curriculum/<主题>/`(plan.md 依赖图 + notes/ 教学笔记 + mistakes.md 错题 + study-log.md)、`journal/YYYY-MM.md` 学习日志
11
- - **导出学习报告**:自包含 HTML(内嵌 mermaid 渲染),含每个节点的学习内容/教学笔记、学习记录、答题历史、错题
11
+ - **导出学习报告**:自包含 HTML(内嵌 mermaid 与 KaTeX 渲染,含字体),含每个节点的学习内容/教学笔记、学习记录、答题历史、错题
12
12
  - **资料支持**:上传资料放 `materials/<主题>/`,或让 AI 上网检索核实后规划
13
13
 
14
14
  ## 安装
package/client.js CHANGED
@@ -405,6 +405,15 @@ window.__ModuleLoader__.load({
405
405
  .ls-upload-fill { height: 100%; background: var(--dsw-alias-brand-primary); transition: width .15s; }
406
406
  .ls-upload-err { font-size: 12px; color: var(--dsw-alias-state-danger-primary, #B5432E); padding: 4px 0; }
407
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; }
408
417
  .ls-picked { margin-top: 6px; font-size: 12px; color: var(--dsw-alias-label-secondary); }
409
418
  .ls-picked-item { display: flex; align-items: center; gap: 6px; padding: 1px 0; word-break: break-all; }
410
419
  .ls-picked-x { cursor: pointer; border: none; background: none; color: var(--dsw-alias-label-secondary);
@@ -1101,6 +1110,7 @@ window.__ModuleLoader__.load({
1101
1110
 
1102
1111
  /** 材料扩展名白名单(与 Host 上传路由一致),供 file input 的 accept */
1103
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 读不出正文'
1104
1114
 
1105
1115
  /** XHR 上传(fetch 无上传进度):FormData + xhr.upload.onprogress 回传百分比 */
1106
1116
  function uploadFiles(topic, fileList, onProgress) {
@@ -1124,8 +1134,8 @@ window.__ModuleLoader__.load({
1124
1134
  })
1125
1135
  }
1126
1136
 
1127
- /** 隐藏 file input + 样式化按钮触发,选中即回调 */
1128
- function FilePickButton({ onFiles, label, disabled }) {
1137
+ /** 隐藏 file input + 样式化按钮触发,选中即回调;tip 走 .ls-tip 悬停气泡 */
1138
+ function FilePickButton({ onFiles, label, disabled, tip, tipAlign }) {
1129
1139
  const ref = useRef(null)
1130
1140
  return h('span', { className: 'ls-file-label' },
1131
1141
  h('input', {
@@ -1133,7 +1143,9 @@ window.__ModuleLoader__.load({
1133
1143
  onChange: e => { if (e.target.files && e.target.files.length) onFiles(e.target.files); e.target.value = '' },
1134
1144
  }),
1135
1145
  h('button', {
1136
- className: 'ls-btn', type: 'button', disabled,
1146
+ className: 'ls-btn' + (tip ? ' ls-tip' + (tipAlign === 'left' ? ' ls-tip-left' : '') : ''),
1147
+ type: 'button', disabled,
1148
+ 'data-tip': tip || null,
1137
1149
  onClick: () => ref.current && ref.current.click(),
1138
1150
  }, label || '导入材料'),
1139
1151
  )
@@ -1173,7 +1185,10 @@ window.__ModuleLoader__.load({
1173
1185
  className: 'ls-h', style: { border: 'none', padding: 0, margin: 0, cursor: 'pointer', userSelect: 'none' },
1174
1186
  onClick: () => setOpen(!open),
1175
1187
  }, `${open ? '▾' : '▸'} 学习材料${materials.length ? `(${materials.length})` : ''}`),
1176
- h(FilePickButton, { onFiles: doUpload, disabled: uploading, label: uploading ? '上传中…' : '导入材料' })),
1188
+ h(FilePickButton, {
1189
+ onFiles: doUpload, disabled: uploading, label: uploading ? '上传中…' : '导入材料',
1190
+ tip: MATERIAL_TIP,
1191
+ })),
1177
1192
  open ? (materials.length === 0
1178
1193
  ? h('div', { className: 'ls-empty' }, '暂无材料')
1179
1194
  : materials.map(m => h('div', { key: m.name, className: 'ls-mat-item' },
@@ -1633,6 +1648,7 @@ window.__ModuleLoader__.load({
1633
1648
  h(FilePickButton, {
1634
1649
  onFiles: fl => setFiles(prev => [...prev, ...Array.from(fl)]),
1635
1650
  disabled: busy, label: files.length ? `+ 追加材料(${files.length})` : '+ 附带材料',
1651
+ tip: MATERIAL_TIP, tipAlign: 'left',
1636
1652
  }),
1637
1653
  h('button', { className: 'ls-btn', type: 'submit', disabled: busy || !name.trim() },
1638
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, '&amp;').replace(/</g, '&lt;')
@@ -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
- const inline = s => s
573
- .replace(/\*\*([^*]+)\*\*/g, '<strong>$1</strong>')
574
- .replace(/`([^`]+)`/g, '<code>$1</code>')
575
- const flushList = () => { if (list && list.length) { out.push(`<ul>${list.map(li => `<li>${inline(li)}</li>`).join('')}</ul>`); list = null } }
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>${inline(esc(hm[2]))}</h4>`); continue }
584
- if (/^\s*[-*]\s+/.test(raw)) { list = list || []; list.push(esc(raw.replace(/^\s*[-*]\s+/, ''))); continue }
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>${inline(esc(raw))}</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>${esc(r.stem)}</td><td>答 ${esc(r.answer)}</td><td>${esc(r.feedback)}</td></tr>`).join('')
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>${esc(m)}</li>`).join('')}</ul>`
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>${esc(e.summary)}</td></tr>`).join('')
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/&lt;节点&gt;.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>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-learn-space",
3
- "version": "1.2.0",
3
+ "version": "1.2.1",
4
4
  "description": "学习空间:AI 辅导的个人学习面板。摸底定位 → 依赖图规划 → 逐节点教学 + quiz 确认,进度/笔记/错题/学习记录沉淀为 Markdown,支持导出自包含 HTML 学习报告。",
5
5
  "keywords": [
6
6
  "dsh",