Skip to content

Commit 4c457ac

Browse files
committed
xmldoc comment fixes
1 parent 5ebcc74 commit 4c457ac

3 files changed

Lines changed: 97 additions & 3 deletions

File tree

‎docs/content/_template.html‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ <H1 style="font-size: 24px;">{{fsdocs-collection-name}}</H1>
110110
</div>
111111
</dialog>
112112
<script type="module" src="{{root}}content/fsdocs-tips.js"></script>
113+
<script type="module" src="{{root}}content/fsdocs-xmldoc-tips.js"></script>
113114
<script type="module" src="{{root}}content/fsdocs-theme-toggle.js"></script>
114115
<script type="module" src="{{root}}content/fsdocs-theme.js"></script>
115116
<script type="module" src="{{root}}content/fsdocs-search.js"></script>

‎docs/content/content/fsdocs-custom.css‎

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ blockquote {
345345

346346
#content pre.fssnip code {
347347
font: 0.86rem 'Fira Code Fixed', monospace;
348-
font-weight: 600;
348+
font-weight: 500;
349349
}
350350

351351
#content table.pre {
@@ -553,7 +553,7 @@ blockquote {
553553
}
554554
/* keywords */
555555
#content span.k {
556-
color: #1f4ee9;
556+
color: #4878f0;
557557
}
558558
/* comment */
559559
#content span.c {
@@ -597,7 +597,6 @@ div.fsdocs-tip {
597597
border-radius: 4px;
598598
font: 0.85rem 'Fira Code Fixed', monospace;
599599
padding: 6px 8px 6px 8px;
600-
display: none;
601600
color: #d1d1d1;
602601
pointer-events: none;
603602
}
@@ -607,6 +606,31 @@ div.fsdocs-tip {
607606
font: 0.85rem 'Fira Code Fixed', monospace;
608607
}
609608

609+
/* XML doc in a tool tip, rendered by fsdocs-xmldoc-tips.js */
610+
div.fsdocs-tip-doc {
611+
margin-top: 6px;
612+
padding-top: 6px;
613+
border-top: 1px solid #5f7478;
614+
white-space: normal;
615+
}
616+
617+
div.fsdocs-tip-doc-summary {
618+
color: #f0d98c;
619+
}
620+
621+
div.fsdocs-tip-doc-remarks,
622+
div.fsdocs-tip-doc-param {
623+
margin-top: 4px;
624+
}
625+
626+
span.fsdocs-tip-doc-name {
627+
color: #9cdcfe;
628+
}
629+
630+
div.fsdocs-tip-doc code {
631+
color: #ffffff;
632+
}
633+
610634
#content code {
611635
color: #8c938c
612636
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// fsdocs puts a member's XML doc into its tooltip as raw text, tags included.
2+
// This renders the tags: the summary in its own colour, one line per parameter, the return value, and
3+
// <see cref="..."/> / <paramref name="..."/> as the name they refer to.
4+
5+
const docTag = /<\/?(summary|param|returns|remarks|exception|typeparam|see|paramref|typeparamref|para|c|code)\b/;
6+
7+
function refName(el) {
8+
const name = el.getAttribute("cref") || el.getAttribute("name") || el.getAttribute("langword") || el.textContent;
9+
return name.replace(/^[A-Z]:/, "");
10+
}
11+
12+
function inline(node, target) {
13+
for (const child of node.childNodes) {
14+
if (child.nodeType === Node.TEXT_NODE) {
15+
target.append(child.textContent.replace(/\s+/g, " "));
16+
} else if (child.nodeType === Node.ELEMENT_NODE) {
17+
const tag = child.localName;
18+
if (tag === "see" || tag === "seealso" || tag === "paramref" || tag === "typeparamref" || tag === "c" || tag === "code") {
19+
const code = document.createElement("code");
20+
code.textContent = child.textContent.trim() || refName(child);
21+
target.append(code);
22+
} else {
23+
inline(child, target);
24+
}
25+
}
26+
}
27+
}
28+
29+
function line(cls, label, el) {
30+
const body = document.createDocumentFragment();
31+
inline(el, body);
32+
body.normalize();
33+
if (body.firstChild?.nodeType === Node.TEXT_NODE) body.firstChild.textContent = body.firstChild.textContent.trimStart();
34+
if (body.lastChild?.nodeType === Node.TEXT_NODE) body.lastChild.textContent = body.lastChild.textContent.trimEnd();
35+
const div = document.createElement("div");
36+
div.className = cls;
37+
if (label) {
38+
const name = document.createElement("span");
39+
name.className = "fsdocs-tip-doc-name";
40+
name.textContent = label;
41+
div.append(name, " ");
42+
}
43+
div.append(body);
44+
return div;
45+
}
46+
47+
function render(text) {
48+
const xml = new DOMParser().parseFromString(`<doc>${text}</doc>`, "application/xml");
49+
if (xml.getElementsByTagName("parsererror").length > 0) return null;
50+
const doc = document.createElement("div");
51+
doc.className = "fsdocs-tip-doc";
52+
for (const el of xml.documentElement.children) {
53+
switch (el.localName) {
54+
case "summary": doc.append(line("fsdocs-tip-doc-summary", null, el)); break;
55+
case "remarks": doc.append(line("fsdocs-tip-doc-remarks", null, el)); break;
56+
case "param":
57+
case "typeparam": doc.append(line("fsdocs-tip-doc-param", el.getAttribute("name") + ":", el)); break;
58+
case "returns": doc.append(line("fsdocs-tip-doc-param", "Returns:", el)); break;
59+
case "exception": doc.append(line("fsdocs-tip-doc-param", "Throws " + refName(el) + ":", el)); break;
60+
}
61+
}
62+
return doc.childNodes.length > 0 ? doc : null;
63+
}
64+
65+
for (const em of document.querySelectorAll("div.fsdocs-tip > em")) {
66+
if (!docTag.test(em.textContent)) continue;
67+
const doc = render(em.textContent);
68+
if (doc) em.replaceWith(doc);
69+
}

0 commit comments

Comments
 (0)