mirror of
https://github.com/rzmk/learnhouse.git
synced 2025-12-19 04:19:25 +00:00
feat: add table of contents in dynamic activities
This commit is contained in:
parent
f712d68e28
commit
9bb5953959
5 changed files with 192 additions and 33 deletions
|
|
@ -0,0 +1,29 @@
|
|||
import Heading from '@tiptap/extension-heading'
|
||||
|
||||
// Custom Heading extension that adds IDs
|
||||
export const CustomHeading = Heading.extend({
|
||||
renderHTML({ node, HTMLAttributes }: { node: any; HTMLAttributes: any }) {
|
||||
const hasLevel = this.options.levels.includes(node.attrs.level)
|
||||
const level = hasLevel ? node.attrs.level : this.options.levels[0]
|
||||
|
||||
// Generate ID from heading text
|
||||
const headingText = node.textContent || ''
|
||||
const slug = headingText
|
||||
.toLowerCase()
|
||||
.trim()
|
||||
.replace(/[^\w\s-]/g, '') // Remove special characters
|
||||
.replace(/[\s_-]+/g, '-') // Replace spaces and underscores with hyphens
|
||||
.replace(/^-+|-+$/g, '') // Remove leading/trailing hyphens
|
||||
|
||||
const id = slug ? `heading-${slug}` : `heading-${Math.random().toString(36).substr(2, 9)}`
|
||||
|
||||
return [
|
||||
`h${level}`,
|
||||
{
|
||||
...HTMLAttributes,
|
||||
id,
|
||||
},
|
||||
0,
|
||||
]
|
||||
},
|
||||
})
|
||||
Loading…
Add table
Add a link
Reference in a new issue