Skip to content

Commit 8222a01

Browse files
committed
include anchors to point to cell ids when possible
1 parent 0e96353 commit 8222a01

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

src/format/html/format-html-notebook.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,12 +172,29 @@ export async function processNotebookEmbeds(
172172
if (inline) {
173173
const id = "nblink-" + count++;
174174

175+
// If the first element of the notebook cell is a cell div with an id
176+
// then use that as the hash
177+
let firstCellId;
178+
if (
179+
nbDivEl.firstElementChild &&
180+
nbDivEl.firstElementChild.classList.contains("cell")
181+
) {
182+
firstCellId = nbDivEl.firstElementChild.getAttribute("id");
183+
}
184+
175185
const nbLinkEl = doc.createElement("a");
176186
nbLinkEl.classList.add("quarto-notebook-link");
177187
nbLinkEl.setAttribute("id", `${id}`);
178-
nbLinkEl.setAttribute("href", nbPath.href);
188+
179189
if (nbPath.filename) {
180190
nbLinkEl.setAttribute("download", nbPath.filename);
191+
nbLinkEl.setAttribute("href", nbPath.href);
192+
} else {
193+
if (firstCellId) {
194+
nbLinkEl.setAttribute("href", `${nbPath.href}#${firstCellId}`);
195+
} else {
196+
nbLinkEl.setAttribute("href", `${nbPath.href}`);
197+
}
181198
}
182199
nbLinkEl.appendChild(
183200
doc.createTextNode(

0 commit comments

Comments
 (0)