@@ -3,7 +3,6 @@ import { Injectable, inject, InjectionToken } from '@angular/core';
33import {
44 ContentRenderer ,
55 RenderedContent ,
6- TableOfContentItem ,
76} from '../../../src/lib/content-renderer' ;
87
98/**
@@ -25,36 +24,6 @@ export interface Md4xRendererOptions {
2524export const MD4X_RENDERER_OPTIONS : InjectionToken < Md4xRendererOptions > =
2625 new InjectionToken < Md4xRendererOptions > ( 'md4x_renderer_options' ) ;
2726
28- function makeSlug ( text : string , slugCounts : Map < string , number > ) : string {
29- const baseSlug = text
30- . toLowerCase ( )
31- . replace ( / [ ^ \w \s - ] / g, '' )
32- . trim ( )
33- . replace ( / \s + / g, '-' ) ;
34- const count = slugCounts . get ( baseSlug ) ?? 0 ;
35- slugCounts . set ( baseSlug , count + 1 ) ;
36- return count === 0 ? baseSlug : `${ baseSlug } -${ count } ` ;
37- }
38-
39- function headingsToToc (
40- headings : Array < { level : number ; text : string } > ,
41- ) : TableOfContentItem [ ] {
42- const slugCounts = new Map < string , number > ( ) ;
43- return headings . map ( ( h ) => ( {
44- id : makeSlug ( h . text , slugCounts ) ,
45- level : h . level ,
46- text : h . text ,
47- } ) ) ;
48- }
49-
50- function injectHeadingIds ( html : string , toc : TableOfContentItem [ ] ) : string {
51- let tocIndex = 0 ;
52- return html . replace ( / < h ( [ 1 - 6 ] ) > / g, ( match , level ) => {
53- const item = toc [ tocIndex ++ ] ;
54- return item ? `<h${ level } id="${ item . id } ">` : match ;
55- } ) ;
56- }
57-
5827/**
5928 * Content renderer backed by md4x (C-based CommonMark parser compiled with Zig).
6029 * 50-70x faster than marked for complex documents.
@@ -70,12 +39,8 @@ export class Md4xContentRendererService extends ContentRenderer {
7039 const html = renderToHtml ( content , {
7140 heal : this . options ?. heal ,
7241 highlighter : this . options ?. highlighter ,
42+ headingIds : true ,
7343 } ) ;
74- const meta = parseMeta ( content ) ;
75- const toc = headingsToToc ( meta . headings ) ;
76- return {
77- content : injectHeadingIds ( html , toc ) ,
78- toc,
79- } ;
44+ return { content : html , toc : parseMeta ( content ) . headings } ;
8045 }
8146}
0 commit comments