Skip to content

Commit 4bf4562

Browse files
committed
0.05
1 parent cde9e3f commit 4bf4562

3 files changed

Lines changed: 50 additions & 71 deletions

File tree

docs/assets/javascripts/online-compile.js

Lines changed: 42 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -10,44 +10,14 @@
1010
return btoa(binary);
1111
}
1212

13-
function findCppCodeBlocks() {
13+
function findCppCodeBlocks(root) {
1414
const selectors = [
1515
"pre > code.language-cpp",
1616
"pre > code.language-c\\+\\+",
1717
"pre > code.lang-cpp",
1818
"pre > code.lang-c\\+\\+",
1919
];
20-
return Array.from(document.querySelectorAll(selectors.join(",")));
21-
}
22-
23-
function codeBlockHasMain(code) {
24-
return /\bmain\s*\(/.test(code);
25-
}
26-
27-
function getPreElement(codeEl) {
28-
if (!codeEl) return null;
29-
const pre = codeEl.closest("pre");
30-
return pre instanceof HTMLElement ? pre : null;
31-
}
32-
33-
function getHighlightContainer(preEl) {
34-
const highlight = preEl.closest(".highlight");
35-
return (highlight instanceof HTMLElement ? highlight : preEl.parentElement) || preEl;
36-
}
37-
38-
function createButton(onClick) {
39-
const btn = document.createElement("button");
40-
btn.type = "button";
41-
btn.className = "md-clipboard online-compile-button";
42-
btn.title = "在线编译(打开 Compiler Explorer)";
43-
btn.setAttribute("aria-label", btn.title);
44-
btn.addEventListener("click", (e) => {
45-
e.preventDefault();
46-
e.stopPropagation();
47-
onClick();
48-
});
49-
btn.textContent = "在线编译";
50-
return btn;
20+
return Array.from((root || document).querySelectorAll(selectors.join(",")));
5121
}
5222

5323
function buildCompilerExplorerUrl(source) {
@@ -77,32 +47,49 @@
7747
return `https://godbolt.org/clientstate/${encoded}`;
7848
}
7949

80-
function addButtons() {
81-
const blocks = findCppCodeBlocks();
82-
for (const codeEl of blocks) {
83-
const preEl = getPreElement(codeEl);
84-
if (!preEl) continue;
50+
function getSourceForCurrentPage() {
51+
const content = document.querySelector(".md-content");
52+
const blocks = findCppCodeBlocks(content || document);
53+
const first = blocks[0];
54+
const code = (first?.textContent || "").trim();
55+
if (code) return code;
56+
return "#include <iostream>\n\nint main() {\n std::cout << \"Hello, C++!\" << '\\n';\n return 0;\n}\n";
57+
}
58+
59+
function openCompilerExplorer() {
60+
const url = buildCompilerExplorerUrl(getSourceForCurrentPage());
61+
window.open(url, "_blank", "noopener,noreferrer");
62+
}
8563

86-
const container = getHighlightContainer(preEl);
87-
if (container.querySelector(".online-compile-button")) continue;
64+
function createHeaderButton() {
65+
const a = document.createElement("a");
66+
a.href = "https://godbolt.org/";
67+
a.className = "md-header__button md-icon online-compile-header-button";
68+
a.title = "在线编译(打开 Compiler Explorer)";
69+
a.setAttribute("aria-label", a.title);
70+
a.innerHTML =
71+
'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" aria-hidden="true">' +
72+
'<path d="M10 17l6-5-6-5v10ZM19 19H5V5h14v14Zm0-16H5a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2V5a2 2 0 0 0-2-2Z"/>' +
73+
"</svg>";
74+
75+
a.addEventListener("click", (e) => {
76+
e.preventDefault();
77+
openCompilerExplorer();
78+
});
8879

89-
const code = (codeEl.textContent || "").trim();
90-
if (!code) continue;
80+
return a;
81+
}
9182

92-
const btn = createButton(() => {
93-
const url = buildCompilerExplorerUrl(code);
94-
window.open(url, "_blank", "noopener,noreferrer");
95-
});
83+
function ensureHeaderButton() {
84+
const headerInner =
85+
document.querySelector(".md-header__inner") ||
86+
document.querySelector("header.md-header") ||
87+
document.body;
9688

97-
if (!codeBlockHasMain(code)) {
98-
btn.classList.add("online-compile-button--no-main");
99-
btn.title = "在线编译(片段可能需要补全 main)";
100-
btn.setAttribute("aria-label", btn.title);
101-
}
89+
if (!(headerInner instanceof HTMLElement)) return;
90+
if (headerInner.querySelector(".online-compile-header-button")) return;
10291

103-
container.appendChild(btn);
104-
container.classList.add("online-compile-container");
105-
}
92+
headerInner.appendChild(createHeaderButton());
10693
}
10794

10895
function onReady(fn) {
@@ -114,9 +101,9 @@
114101
}
115102

116103
onReady(() => {
117-
addButtons();
104+
ensureHeaderButton();
118105

119-
const observer = new MutationObserver(() => addButtons());
106+
const observer = new MutationObserver(() => ensureHeaderButton());
120107
observer.observe(document.body, { childList: true, subtree: true });
121108
});
122109
})();
Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,10 @@
1-
.online-compile-container {
2-
position: relative;
1+
.online-compile-header-button {
2+
display: inline-flex;
3+
align-items: center;
4+
justify-content: center;
35
}
46

5-
.online-compile-button {
6-
position: absolute;
7-
top: 0.4rem;
8-
right: 3rem;
9-
z-index: 3;
10-
padding: 0.25rem 0.5rem;
11-
font-size: 0.7rem;
12-
line-height: 1.1;
13-
border-radius: 0.4rem;
14-
}
15-
16-
.online-compile-button--no-main {
17-
opacity: 0.85;
7+
.online-compile-header-button svg {
8+
width: 1.2rem;
9+
height: 1.2rem;
1810
}

mkdocs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ nav:
7777
- 12-综合练习: cpp/12-综合练习小项目-通讯录.md
7878
- 13-C++20新特性: cpp/13-C++20新特性-ConceptsRangesCoroutinesModules.md
7979
- 14-C++23新特性: cpp/14-C++23新特性-标准库更好用与语法补强.md
80-
- 15-C++11到23演进: cpp/15-C++11到23-标准演进路线图.md
80+
- 15-标准演进路线图: cpp/15-C++11到23-标准演进路线图.md
8181
- 16-CMake入门: cpp/16-CMake入门-从单文件到多目标工程.md
8282
- Qt 开发:
8383
- 目录: Qt/00-目录.md

0 commit comments

Comments
 (0)