Skip to content

Commit aeabbea

Browse files
committed
Improve notebook link appearance, add inline
1 parent c7869f1 commit aeabbea

File tree

2 files changed

+88
-12
lines changed

2 files changed

+88
-12
lines changed

src/format/html/format-html-bootstrap.ts

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ const fileBsIconName = (format: Format) => {
431431
} else if (isPdfOutput(format.pandoc)) {
432432
return "file-pdf";
433433
} else if (isIpynbOutput(format.pandoc)) {
434-
return "journal-arrow-down";
434+
return "journal-code";
435435
} else if (isMarkdownOutput(format.pandoc)) {
436436
return "file-code";
437437
} else if (isPresentationOutput(format.pandoc)) {
@@ -518,15 +518,47 @@ function processNotebookEmbeds(
518518
) {
519519
const notebookDivNodes = doc.querySelectorAll("[data-notebook]");
520520
if (notebookDivNodes.length > 0) {
521-
const nbPaths: { path: string; title: string | null }[] = [];
521+
const nbPaths: { path: string; title: string; filename: string }[] = [];
522+
let count = 1;
522523
notebookDivNodes.forEach((nbDivNode) => {
523524
const nbDivEl = nbDivNode as Element;
524-
const nbPath = nbDivEl.getAttribute("data-notebook");
525-
if (nbPath) {
526-
nbPaths.push({
527-
path: nbPath,
528-
title: nbDivEl.getAttribute("data-notebook-title"),
529-
});
525+
nbDivEl.classList.add("quarto-notebook");
526+
const notebookPath = nbDivEl.getAttribute("data-notebook");
527+
if (notebookPath) {
528+
const title = nbDivEl.getAttribute("data-notebook-title");
529+
const filename = basename(notebookPath);
530+
531+
const nbPath = {
532+
path: notebookPath,
533+
title: title || filename,
534+
filename,
535+
};
536+
nbPaths.push(nbPath);
537+
538+
// Add a decoration to this div node
539+
const id = "nblink-" + count++;
540+
541+
const nbLinkEl = doc.createElement("a");
542+
nbLinkEl.classList.add("quarto-notebook-link");
543+
nbLinkEl.setAttribute("id", `${id}`);
544+
nbLinkEl.setAttribute("href", nbPath.path);
545+
nbLinkEl.setAttribute("download", nbPath.filename);
546+
nbLinkEl.appendChild(doc.createTextNode(`Source: ${nbPath.title}`));
547+
548+
const nbParentEl = nbDivEl.parentElement;
549+
if (nbParentEl?.tagName.toLocaleLowerCase() === "figure") {
550+
const figCapEl = nbDivEl.parentElement?.querySelector("figcaption");
551+
if (figCapEl) {
552+
figCapEl.after(nbLinkEl);
553+
} else {
554+
nbDivEl.appendChild(nbLinkEl);
555+
}
556+
} else {
557+
nbDivEl.appendChild(nbLinkEl);
558+
}
559+
560+
// Try to place it after the figure caption, if possible
561+
// For tables, place after the table caption if they are on the bottom
530562
}
531563
});
532564

@@ -544,19 +576,18 @@ function processNotebookEmbeds(
544576
ld.uniqBy(nbPaths, (nbPath: { path: string; title?: string }) => {
545577
return nbPath.path;
546578
}).forEach((nbPath) => {
547-
const filename = basename(nbPath.path);
548579
const li = doc.createElement("li");
549580

550581
const link = doc.createElement("a");
551582
link.setAttribute("href", nbPath.path);
552-
link.setAttribute("download", filename);
583+
link.setAttribute("download", nbPath.filename);
553584

554585
const icon = doc.createElement("i");
555586
icon.classList.add("bi");
556-
icon.classList.add(`bi-journal-arrow-down`);
587+
icon.classList.add(`bi-journal-code`);
557588
link.appendChild(icon);
558589
link.appendChild(
559-
doc.createTextNode(`${nbPath.title || filename}`),
590+
doc.createTextNode(nbPath.title),
560591
);
561592

562593
li.appendChild(link);

src/resources/formats/html/bootstrap/_bootstrap-rules.scss

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -977,11 +977,56 @@ td code:not(.sourceCode) {
977977
}
978978
}
979979

980+
figure .quarto-notebook-link {
981+
margin-top: 0.5em;
982+
}
983+
984+
.quarto-notebook-link {
985+
font-size: 0.75em;
986+
color: $text-muted;
987+
margin-bottom: 1em;
988+
text-decoration: none;
989+
display: block;
990+
}
991+
992+
.quarto-notebook-link:hover {
993+
text-decoration: underline;
994+
color: $link-color;
995+
}
996+
997+
.quarto-notebook-link::before {
998+
display: inline-block;
999+
height: 0.75rem;
1000+
width: 0.75rem;
1001+
margin-bottom: 0em;
1002+
margin-right: 0.25em;
1003+
content: "";
1004+
vertical-align: -0.125em;
1005+
1006+
@if variable-exists(text-muted) {
1007+
background-image: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="#{colorToRGB($text-muted)}" class="bi bi-journal-code" viewBox="0 0 16 16"><path fill-rule="evenodd" d="M8.646 5.646a.5.5 0 0 1 .708 0l2 2a.5.5 0 0 1 0 .708l-2 2a.5.5 0 0 1-.708-.708L10.293 8 8.646 6.354a.5.5 0 0 1 0-.708zm-1.292 0a.5.5 0 0 0-.708 0l-2 2a.5.5 0 0 0 0 .708l2 2a.5.5 0 0 0 .708-.708L5.707 8l1.647-1.646a.5.5 0 0 0 0-.708z"/><path d="M3 0h10a2 2 0 0 1 2 2v12a2 2 0 0 1-2 2H3a2 2 0 0 1-2-2v-1h1v1a1 1 0 0 0 1 1h10a1 1 0 0 0 1-1V2a1 1 0 0 0-1-1H3a1 1 0 0 0-1 1v1H1V2a2 2 0 0 1 2-2z"/><path d="M1 5v-.5a.5.5 0 0 1 1 0V5h.5a.5.5 0 0 1 0 1h-2a.5.5 0 0 1 0-1H1zm0 3v-.5a.5.5 0 0 1 1 0V8h.5a.5.5 0 0 1 0 1h-2a.5.5 0 0 1 0-1H1zm0 3v-.5a.5.5 0 0 1 1 0v.5h.5a.5.5 0 0 1 0 1h-2a.5.5 0 0 1 0-1H1z"/></svg>');
1008+
} @else {
1009+
background-image: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-journal-code" viewBox="0 0 16 16"><path fill-rule="evenodd" d="M8.646 5.646a.5.5 0 0 1 .708 0l2 2a.5.5 0 0 1 0 .708l-2 2a.5.5 0 0 1-.708-.708L10.293 8 8.646 6.354a.5.5 0 0 1 0-.708zm-1.292 0a.5.5 0 0 0-.708 0l-2 2a.5.5 0 0 0 0 .708l2 2a.5.5 0 0 0 .708-.708L5.707 8l1.647-1.646a.5.5 0 0 0 0-.708z"/><path d="M3 0h10a2 2 0 0 1 2 2v12a2 2 0 0 1-2 2H3a2 2 0 0 1-2-2v-1h1v1a1 1 0 0 0 1 1h10a1 1 0 0 0 1-1V2a1 1 0 0 0-1-1H3a1 1 0 0 0-1 1v1H1V2a2 2 0 0 1 2-2z"/><path d="M1 5v-.5a.5.5 0 0 1 1 0V5h.5a.5.5 0 0 1 0 1h-2a.5.5 0 0 1 0-1H1zm0 3v-.5a.5.5 0 0 1 1 0V8h.5a.5.5 0 0 1 0 1h-2a.5.5 0 0 1 0-1H1zm0 3v-.5a.5.5 0 0 1 1 0v.5h.5a.5.5 0 0 1 0 1h-2a.5.5 0 0 1 0-1H1z"/></svg>');
1010+
}
1011+
background-repeat: no-repeat;
1012+
background-size: 0.75rem 0.75rem;
1013+
}
1014+
9801015
.quarto-alternate-notebooks i.bi,
9811016
.quarto-alternate-formats i.bi {
9821017
margin-right: 0.4em;
9831018
}
9841019

1020+
.sidebar .quarto-alternate-formats a,
1021+
.sidebar .quarto-alternate-notebooks a {
1022+
text-decoration: none;
1023+
}
1024+
1025+
.sidebar .quarto-alternate-formats a:hover,
1026+
.sidebar .quarto-alternate-notebooks a:hover {
1027+
color: $link-color;
1028+
}
1029+
9851030
.sidebar .quarto-alternate-notebooks h2,
9861031
.sidebar .quarto-alternate-formats h2,
9871032
.sidebar nav[role="doc-toc"] > h2 {

0 commit comments

Comments
 (0)