mirror of
https://github.com/notion-enhancer/notion-enhancer.git
synced 2025-04-09 15:09:02 +00:00
add ids to markdown headings
This commit is contained in:
parent
f3c384e25d
commit
9a0ffdd9bc
@ -142,8 +142,41 @@ fmt.md.renderer.rules.code_block = (tokens, idx, options, env, slf) =>
|
|||||||
web.html`<pre${slf.renderAttrs(tokens[idx])}><code>${web.escapeHtml(
|
web.html`<pre${slf.renderAttrs(tokens[idx])}><code>${web.escapeHtml(
|
||||||
tokens[idx].content
|
tokens[idx].content
|
||||||
)}</code></pre>\n`;
|
)}</code></pre>\n`;
|
||||||
|
fmt.md.core.ruler.push(
|
||||||
|
'heading_ids',
|
||||||
|
function (md, state) {
|
||||||
|
const slugs = new Set();
|
||||||
|
state.tokens.forEach(function (token, i) {
|
||||||
|
if (token.type === 'heading_open') {
|
||||||
|
const text = md.renderer.render(state.tokens[i + 1].children, md.options),
|
||||||
|
slug = fmt.slugger(text, slugs);
|
||||||
|
slugs.add(slug);
|
||||||
|
const idx = token.attrIndex('id');
|
||||||
|
if (idx === -1) {
|
||||||
|
token.attrPush(['id', slug]);
|
||||||
|
} else {
|
||||||
|
token.attrs[idx][1] = slug;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}.bind(null, fmt.md)
|
||||||
|
);
|
||||||
// delete globalThis['markdownit'];
|
// delete globalThis['markdownit'];
|
||||||
|
|
||||||
|
fmt.slugger = (heading, slugs = new Set()) => {
|
||||||
|
heading = heading
|
||||||
|
.replace(/\s/g, '-')
|
||||||
|
.replace(/[^A-Za-z0-9-_]/g, '')
|
||||||
|
.toLowerCase();
|
||||||
|
let i = 0,
|
||||||
|
slug = heading;
|
||||||
|
while (slugs.has(slug)) {
|
||||||
|
i++;
|
||||||
|
slug = `${heading}-${i}`;
|
||||||
|
}
|
||||||
|
return slug;
|
||||||
|
};
|
||||||
|
|
||||||
/** - */
|
/** - */
|
||||||
|
|
||||||
export const fs = {};
|
export const fs = {};
|
||||||
|
@ -27,6 +27,7 @@ body {
|
|||||||
|
|
||||||
header {
|
header {
|
||||||
display: flex;
|
display: flex;
|
||||||
|
flex-wrap: wrap;
|
||||||
margin-bottom: 1.25rem;
|
margin-bottom: 1.25rem;
|
||||||
}
|
}
|
||||||
header > * {
|
header > * {
|
||||||
@ -66,6 +67,14 @@ header h1 a:not([data-active]) {
|
|||||||
[data-container='mod'] > .documentation--body {
|
[data-container='mod'] > .documentation--body {
|
||||||
grid-column: span 2;
|
grid-column: span 2;
|
||||||
}
|
}
|
||||||
|
[data-container='mod'] .library--card,
|
||||||
|
.documentation--body {
|
||||||
|
max-height: calc(100vh - 10rem);
|
||||||
|
overflow-y: auto;
|
||||||
|
}
|
||||||
|
body {
|
||||||
|
overflow: hidden;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@media (min-width: 1350px) {
|
@media (min-width: 1350px) {
|
||||||
[data-container] {
|
[data-container] {
|
||||||
@ -99,6 +108,10 @@ header h1 a:not([data-active]) {
|
|||||||
max-width: 100%;
|
max-width: 100%;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[data-container='mod'] .library--card,
|
||||||
|
.documentation--body {
|
||||||
|
overflow-x: auto;
|
||||||
|
}
|
||||||
.documentation--buttons,
|
.documentation--buttons,
|
||||||
.library--expand {
|
.library--expand {
|
||||||
margin: 0;
|
margin: 0;
|
||||||
@ -324,11 +337,6 @@ header h1 a:not([data-active]) {
|
|||||||
padding: 1rem;
|
padding: 1rem;
|
||||||
}
|
}
|
||||||
|
|
||||||
[data-container='mod'] .library--card,
|
|
||||||
.documentation--body {
|
|
||||||
max-height: calc(100vh - 10rem);
|
|
||||||
overflow: auto;
|
|
||||||
}
|
|
||||||
.documentation--body {
|
.documentation--body {
|
||||||
padding: 1rem 2rem;
|
padding: 1rem 2rem;
|
||||||
font-size: 0.8rem;
|
font-size: 0.8rem;
|
||||||
|
@ -212,12 +212,16 @@ const views = {
|
|||||||
},
|
},
|
||||||
_navigator(event) {
|
_navigator(event) {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
console.log(event);
|
document.getElementById(event.target.getAttribute('href').slice(1)).scrollIntoView(true);
|
||||||
|
document.documentElement.scrollTop = 0;
|
||||||
},
|
},
|
||||||
_reset() {
|
_reset() {
|
||||||
document
|
document
|
||||||
.querySelectorAll('a[href^="?"]')
|
.querySelectorAll('a[href^="?"]')
|
||||||
.forEach((a) => a.removeEventListener('click', this._router));
|
.forEach((a) => a.removeEventListener('click', this._router));
|
||||||
|
document
|
||||||
|
.querySelectorAll('a[href^="#"]')
|
||||||
|
.forEach((a) => a.removeEventListener('click', this._navigator));
|
||||||
this.$container.style.opacity = 0;
|
this.$container.style.opacity = 0;
|
||||||
return new Promise((res, rej) => {
|
return new Promise((res, rej) => {
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
@ -262,6 +266,9 @@ const views = {
|
|||||||
document
|
document
|
||||||
.querySelectorAll('a[href^="?"]')
|
.querySelectorAll('a[href^="?"]')
|
||||||
.forEach((a) => a.addEventListener('click', this._router));
|
.forEach((a) => a.addEventListener('click', this._router));
|
||||||
|
document
|
||||||
|
.querySelectorAll('a[href^="#"]')
|
||||||
|
.forEach((a) => a.addEventListener('click', this._navigator));
|
||||||
},
|
},
|
||||||
async alerts() {
|
async alerts() {
|
||||||
this.$container.dataset.container = 'alerts';
|
this.$container.dataset.container = 'alerts';
|
||||||
|
Loading…
Reference in New Issue
Block a user