@a9i5k4/dsh-literature 0.2.8 → 0.2.9

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 (3) hide show
  1. package/lib/client.js +69 -13
  2. package/lib/index.js +53 -15
  3. package/package.json +1 -1
package/lib/index.js CHANGED
@@ -1768,7 +1768,11 @@ var cite_exports = {};
1768
1768
  __export(cite_exports, {
1769
1769
  bibtex: () => bibtex,
1770
1770
  cite: () => cite,
1771
- listStyles: () => listStyles
1771
+ citeDetailed: () => citeDetailed,
1772
+ listStyles: () => listStyles,
1773
+ segmentsToHtml: () => segmentsToHtml,
1774
+ stripItalic: () => stripItalic,
1775
+ toSegments: () => toSegments
1772
1776
  });
1773
1777
  function authorsList(record, { max = 99, ellipsis = ", et al." } = {}) {
1774
1778
  const list = record.authors ?? [];
@@ -1802,7 +1806,7 @@ function apa(record) {
1802
1806
  const title = (record.title ?? "Untitled").trim();
1803
1807
  if (record.itemType === "journalArticle" || record.itemType === "preprint") {
1804
1808
  const journal = record.container ? `*${record.container}*, ` : "";
1805
- const vol = record.volume ? `${record.volume}` : "";
1809
+ const vol = record.volume ? `*${record.volume}*` : "";
1806
1810
  const issue = record.issue ? `(${record.issue})` : "";
1807
1811
  const pages = pagesStr(record) ? `, ${pagesStr(record)}` : "";
1808
1812
  const volPart = vol || issue || pages ? `${vol}${issue}${pages}. ` : "";
@@ -1884,11 +1888,11 @@ function chicago(record) {
1884
1888
  const who = list.length ? list.map((a) => `${a.firstName ?? ""} ${a.lastName ?? ""}`.trim()).join(", ") + ". " : "";
1885
1889
  const title = (record.title ?? "Untitled").trim();
1886
1890
  if (record.itemType === "journalArticle" || record.itemType === "preprint") {
1887
- const journal = record.container ? ` "${record.container}"` : "";
1891
+ const journal = record.container ? ` *${record.container}*` : "";
1888
1892
  const vol = record.volume ? ` ${record.volume}` : "";
1889
1893
  const issue = record.issue ? `, no. ${record.issue}` : "";
1890
1894
  const pages = pagesStr(record) ? `: ${pagesStr(record)}` : "";
1891
- return `${who}"${title}.${journal}" ${vol}${issue} (${yearStr(record)})${pages}.`.trim();
1895
+ return `${who}"${title}."${journal}${vol}${issue} (${yearStr(record)})${pages}.`.replace(/\s+/g, " ").replace(/ ,/g, ",").trim();
1892
1896
  }
1893
1897
  const pub = record.publisher ? ` ${record.publisher},` : "";
1894
1898
  return `${who}*${title}.*${pub} ${yearStr(record)}.`.trim();
@@ -1930,22 +1934,56 @@ function bibtex(record) {
1930
1934
  const body = fields.map(([k, v]) => ` ${k} = {${bibtexEscape(v)}}`).join(",\n");
1931
1935
  return "@" + type + "{" + key + ",\n" + body + "\n}\n";
1932
1936
  }
1937
+ function stripItalic(text) {
1938
+ return String(text ?? "").replace(/\*([^*]+)\*/g, "$1");
1939
+ }
1940
+ function toSegments(text) {
1941
+ const s = String(text ?? "");
1942
+ const out = [];
1943
+ const re = /\*([^*]+)\*/g;
1944
+ let last = 0;
1945
+ let m;
1946
+ while (m = re.exec(s)) {
1947
+ if (m.index > last) out.push({ text: s.slice(last, m.index) });
1948
+ out.push({ text: m[1], italic: true });
1949
+ last = m.index + m[0].length;
1950
+ }
1951
+ if (last < s.length) out.push({ text: s.slice(last) });
1952
+ return out.length ? out : [{ text: s }];
1953
+ }
1954
+ function escapeHtml(text) {
1955
+ return String(text ?? "").replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;");
1956
+ }
1957
+ function segmentsToHtml(segments) {
1958
+ return segments.map((seg) => seg.italic ? `<i>${escapeHtml(seg.text)}</i>` : escapeHtml(seg.text)).join("");
1959
+ }
1933
1960
  function cite(record, opts = {}) {
1961
+ return citeDetailed(record, opts).text;
1962
+ }
1963
+ function citeDetailed(record, opts = {}) {
1934
1964
  const style = STYLES[opts.style] ?? STYLES.apa;
1935
1965
  const mode = opts.mode ?? "reference";
1936
- if (opts.style === "bibtex") return style.format(record);
1966
+ if (opts.style === "bibtex") {
1967
+ const text2 = style.format(record);
1968
+ return { text: text2, segments: [{ text: text2 }], html: escapeHtml(text2), style: opts.style, mode: "reference" };
1969
+ }
1970
+ let raw;
1937
1971
  if (mode === "intext") {
1938
1972
  if (opts.style === "gb") {
1939
- return `\uFF08${authorsShortGb(record)}\uFF0C${yearStr(record)}\uFF09`;
1973
+ raw = `\uFF08${authorsShortGb(record)}\uFF0C${yearStr(record)}\uFF09`;
1974
+ } else {
1975
+ raw = `(${authorsShort(record)}, ${yearStr(record)})`;
1940
1976
  }
1941
- return `(${authorsShort(record)}, ${yearStr(record)})`;
1942
- }
1943
- if (mode === "direct") {
1977
+ } else if (mode === "direct") {
1944
1978
  const pages = opts.pages ? opts.style === "gb" ? `\uFF0C\u7B2C ${opts.pages} \u9875` : `, p. ${opts.pages}` : "";
1945
- if (opts.style === "gb") return `\u201C${(record.title ?? "").trim()}\u201D\uFF08${authorsShortGb(record)}\uFF0C${yearStr(record)}${pages}\uFF09`;
1946
- return `"${(record.title ?? "").trim()}" (${authorsShort(record)}, ${yearStr(record)}${pages})`;
1979
+ if (opts.style === "gb") raw = `\u201C${(record.title ?? "").trim()}\u201D\uFF08${authorsShortGb(record)}\uFF0C${yearStr(record)}${pages}\uFF09`;
1980
+ else raw = `"${(record.title ?? "").trim()}" (${authorsShort(record)}, ${yearStr(record)}${pages})`;
1981
+ } else {
1982
+ raw = style.format(record);
1947
1983
  }
1948
- return style.format(record);
1984
+ const text = stripItalic(raw);
1985
+ const segments = toSegments(raw);
1986
+ return { text, segments, html: segmentsToHtml(segments), style: opts.style ?? "apa", mode };
1949
1987
  }
1950
1988
  function authorsShortGb(record) {
1951
1989
  const list = record.authors ?? [];
@@ -35823,13 +35861,13 @@ async function handler(req, res, ctx) {
35823
35861
  writeJson(res, 404, { error: "\u6761\u76EE\u7F3A\u5C11\u5143\u6570\u636E\uFF0C\u65E0\u6CD5\u751F\u6210\u5F15\u7528", code: "noMetadataCite" });
35824
35862
  return;
35825
35863
  }
35826
- const { cite: cite2 } = await Promise.resolve().then(() => (init_cite(), cite_exports));
35827
- const text = cite2(item.record, {
35864
+ const { citeDetailed: citeDetailed2 } = await Promise.resolve().then(() => (init_cite(), cite_exports));
35865
+ const detail = citeDetailed2(item.record, {
35828
35866
  style: body?.style ?? "apa",
35829
35867
  mode: body?.mode ?? "reference",
35830
35868
  pages: body?.pages
35831
35869
  });
35832
- writeJson(res, 200, { text, style: body?.style ?? "apa", mode: body?.mode ?? "reference" });
35870
+ writeJson(res, 200, { ...detail, style: body?.style ?? "apa", mode: body?.mode ?? "reference" });
35833
35871
  return;
35834
35872
  }
35835
35873
  if (head === "scan-dir" && methodOk(req, "POST")) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@a9i5k4/dsh-literature",
3
- "version": "0.2.8",
3
+ "version": "0.2.9",
4
4
  "description": "DSH Literature 文献侧窗:在 DeepSeek Harness 侧边栏识别 DOI/arXiv/标题、抓取元数据与全文、写入本地文献库或导出目录,并提供内置 PDF 阅读器(缩放/翻页/目录/搜索/高亮/笔记)。",
5
5
  "type": "module",
6
6
  "main": "lib/index.js",