mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2025-05-18 00:54:43 -04:00
Restructure replacers (#266)
* Restructure replacers Signed-off-by: Tilman Vatteroth <tilman.vatteroth@tu-dortmund.de>
This commit is contained in:
parent
eb56da7871
commit
b74bb8e71d
16 changed files with 250 additions and 219 deletions
|
@ -1,17 +1,17 @@
|
|||
import { DomElement } from 'domhandler'
|
||||
import { ReactElement } from 'react'
|
||||
import { SubNodeConverter } from '../../markdown-renderer'
|
||||
import { ComponentReplacer, SubNodeConverter } from '../ComponentReplacer'
|
||||
|
||||
const getElementReplacement = (node: DomElement, index: number, counterMap: Map<string, number>, nodeConverter: SubNodeConverter): (ReactElement | undefined) => {
|
||||
if (node.name === 'p' && node.children && node.children.length === 1) {
|
||||
export class TocReplacer implements ComponentReplacer {
|
||||
getReplacement (node: DomElement, index: number, subNodeConverter: SubNodeConverter): React.ReactElement | undefined {
|
||||
if (node.name !== 'p' || node.children?.length !== 1) {
|
||||
return
|
||||
}
|
||||
const possibleTocDiv = node.children[0]
|
||||
if (possibleTocDiv.name === 'div' && possibleTocDiv.attribs && possibleTocDiv.attribs.class &&
|
||||
possibleTocDiv.attribs.class === 'table-of-contents' && possibleTocDiv.children && possibleTocDiv.children.length === 1) {
|
||||
possibleTocDiv.attribs.class === 'table-of-contents' && possibleTocDiv.children && possibleTocDiv.children.length === 1) {
|
||||
const listElement = possibleTocDiv.children[0]
|
||||
listElement.attribs = Object.assign(listElement.attribs || {}, { class: 'table-of-contents' })
|
||||
return nodeConverter(listElement, index)
|
||||
return subNodeConverter(listElement, index)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export { getElementReplacement as getTOCReplacement }
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue