mirror of
https://github.com/thiloho/archtika.git
synced 2025-11-22 10:51:36 +01:00
Add slugified id for sections
This commit is contained in:
@@ -13,33 +13,43 @@
|
||||
|
||||
<svelte:head>
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>{title}</title>
|
||||
<link rel="stylesheet" href="../styles.css" />
|
||||
</head>
|
||||
</svelte:head>
|
||||
|
||||
<nav>
|
||||
{#if logoType === "text"}
|
||||
<p>
|
||||
<strong>{logo}</strong>
|
||||
</p>
|
||||
{:else}
|
||||
<img src={logo} alt="" />
|
||||
{/if}
|
||||
<div class="container">
|
||||
{#if logoType === "text"}
|
||||
<p>
|
||||
<strong>{logo}</strong>
|
||||
</p>
|
||||
{:else}
|
||||
<img src={logo} alt="" />
|
||||
{/if}
|
||||
</div>
|
||||
</nav>
|
||||
|
||||
<header>
|
||||
{#if coverImage}
|
||||
<img src={coverImage} alt="" />
|
||||
{/if}
|
||||
<h1>{title}</h1>
|
||||
<p>{publicationDate}</p>
|
||||
<div class="container">
|
||||
{#if coverImage}
|
||||
<img src={coverImage} alt="" />
|
||||
{/if}
|
||||
<h1>{title}</h1>
|
||||
<p>{publicationDate}</p>
|
||||
</div>
|
||||
</header>
|
||||
|
||||
<main>
|
||||
{@html mainContent}
|
||||
<div class="container">
|
||||
{@html mainContent}
|
||||
</div>
|
||||
</main>
|
||||
|
||||
<footer>
|
||||
{footerAdditionalText}
|
||||
<div class="container">
|
||||
{footerAdditionalText}
|
||||
</div>
|
||||
</footer>
|
||||
|
||||
@@ -11,6 +11,8 @@
|
||||
|
||||
<svelte:head>
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>{title}</title>
|
||||
<link rel="stylesheet" href="./styles.css" />
|
||||
</head>
|
||||
|
||||
@@ -30,10 +30,21 @@ export const md = markdownit({
|
||||
const sections: { header: number; nesting: number }[] = [];
|
||||
let nestedLevel = 0;
|
||||
|
||||
const openSection = (attrs: [string, string][] | null) => {
|
||||
const slugify = (text: string) => {
|
||||
return text
|
||||
.toLowerCase()
|
||||
.replace(/\s+/g, "-")
|
||||
.replace(/[^\w-]+/g, "")
|
||||
.replace(/--+/g, "-")
|
||||
.replace(/^-+/, "")
|
||||
.replace(/-+$/, "");
|
||||
};
|
||||
|
||||
const openSection = (attrs: [string, string][] | null, headingText: string) => {
|
||||
const t = new Token("section_open", "section", 1);
|
||||
t.block = true;
|
||||
t.attrs = attrs ? attrs.map((attr) => [attr[0], attr[1]]) : null;
|
||||
t.attrs = attrs ? attrs.map((attr) => [attr[0], attr[1]]) : [];
|
||||
t.attrs.push(["id", slugify(headingText)]);
|
||||
return t;
|
||||
};
|
||||
|
||||
@@ -63,7 +74,8 @@ export const md = markdownit({
|
||||
}
|
||||
};
|
||||
|
||||
for (const token of state.tokens) {
|
||||
for (let i = 0; i < state.tokens.length; i++) {
|
||||
const token = state.tokens[i];
|
||||
if (token.type.search("heading") !== 0) {
|
||||
nestedLevel += token.nesting;
|
||||
}
|
||||
@@ -79,7 +91,11 @@ export const md = markdownit({
|
||||
if (sections.length && section.header <= sections[sections.length - 1].header) {
|
||||
closeSections(section);
|
||||
}
|
||||
tokens.push(openSection(token.attrs));
|
||||
|
||||
const headingTextToken = state.tokens[i + 1];
|
||||
const headingText = headingTextToken.content;
|
||||
|
||||
tokens.push(openSection(token.attrs, headingText));
|
||||
const idIndex = token.attrIndex("id");
|
||||
if (idIndex !== -1) {
|
||||
token.attrs?.splice(idIndex, 1);
|
||||
|
||||
Reference in New Issue
Block a user