Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Markdown generates anchor links in headers #15

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 131 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
"katex": "^0.15.3",
"nanoid": "^3.3.2",
"percival-wasm": "file:./crates/percival-wasm/pkg",
"rehype-autolink-headings": "^6.1.1",
"rehype-katex": "^6.0.2",
"rehype-slug": "^5.0.1",
"rehype-stringify": "^9.0.3",
"remark-math": "^5.1.1",
"remark-parse": "^10.0.1",
Expand Down
7 changes: 6 additions & 1 deletion src/components/App.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script lang="ts">
import { onMount } from "svelte";
import { onMount, tick } from "svelte";
import type { Readable } from "svelte/store";
import { NotebookState } from "@/lib/notebook";
import { createNotebookStore } from "@/lib/stores";
Expand Down Expand Up @@ -32,6 +32,11 @@
}
notebookStore = createNotebookStore(NotebookState.load(unmarshal(text)));
}
// Navigate to hash after the page loads
if (window.location.hash) {
await tick();
document.getElementById(window.location.hash.slice(1))?.scrollIntoView();
}
});

/** Handler to prompt the user before navigating away from the page. */
Expand Down
21 changes: 21 additions & 0 deletions src/components/cell/CellOutput.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,25 @@
.markdown-output :global(code) {
@apply text-sm text-gray-900;
}

.markdown-output :global(:where(h1, h2, h3, h4, h5, h6) span.icon.icon-link) {
@apply transition-all;
}

.markdown-output
:global(:where(h1, h2, h3, h4, h5, h6):hover span.icon.icon-link) {
display: inline;
background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg>');
background-repeat: no-repeat;
background-position: center center;
background-size: 0.75em;
padding-left: 1em;
padding-right: 0.5em;
}

.markdown-output
:global(:where(h1, h2, h3, h4, h5, h6):hover span.icon.icon-link:hover) {
background-size: 0.9em;
filter: invert(0.2);
}
</style>
4 changes: 4 additions & 0 deletions src/lib/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@ import { unified } from "unified";
import remarkParse from "remark-parse";
import remarkMath from "remark-math";
import remarkRehype from "remark-rehype";
import rehypeSlug from "rehype-slug";
import rehypeAutolinkHeadings from "rehype-autolink-headings";
import rehypeKatex from "rehype-katex";
import rehypeStringify from "rehype-stringify";

const pipeline = unified()
.use(remarkParse)
.use(remarkMath)
.use(remarkRehype)
.use(rehypeSlug)
.use(rehypeAutolinkHeadings, { behavior: "append" })
.use(rehypeKatex)
.use(rehypeStringify);

Expand Down