@@ -55,13 +55,24 @@ export function preprocessReadme(opts) {
5555 return {
5656 markup : ( { content, filename } ) => {
5757 if ( / n o d e _ m o d u l e s / . test ( filename ) || ! filename . endsWith ( ".md" ) ) return null ;
58+
59+ content = content . replace (
60+ "<!-- TOC -->" ,
61+ `
62+ ## Table of Contents
63+ `
64+ ) ;
65+
5866 let script_content = "" ;
5967 let style_content = "" ;
6068 let result = md . render ( content ) ;
6169 let cursor = 0 ;
6270
6371 const ast = parse ( result ) ;
6472
73+ let headings = [ ] ;
74+ let prev ;
75+
6576 walk ( ast , {
6677 enter ( node , parent ) {
6778 if ( node . type === "Attribute" && node . name === "href" ) {
@@ -81,6 +92,34 @@ export function preprocessReadme(opts) {
8192 cursor -= replace_style . length ;
8293 }
8394
95+ if ( node . type === "Element" && node . name === "h2" ) {
96+ const id = node . attributes . find ( ( attr ) => attr . name === "id" ) . value [ 0 ] . raw ;
97+
98+ if ( id === "table-of-contents" ) return ;
99+
100+ const text = node . children [ 0 ] . raw ;
101+ if ( prev === "h3" ) {
102+ headings . push ( `</ul><li><a href="#${ id } ">${ text } </a></li>` ) ;
103+ } else {
104+ headings . push ( `<li><a href="#${ id } ">${ text } </a></li>` ) ;
105+ }
106+
107+ prev = "h2" ;
108+ }
109+
110+ if ( node . type === "Element" && node . name === "h3" ) {
111+ const id = node . attributes . find ( ( attr ) => attr . name === "id" ) . value [ 0 ] . raw ;
112+ const text = node . children [ 0 ] . raw ;
113+
114+ if ( prev === "h2" ) {
115+ headings . push ( `<ul><li><a href="#${ id } ">${ text } </a></li>` ) ;
116+ } else {
117+ headings . push ( `<li><a href="#${ id } ">${ text } </a></li>` ) ;
118+ }
119+
120+ prev = "h3" ;
121+ }
122+
84123 if ( node . type === "Attribute" && node . name === "data-svelte" ) {
85124 const raw_value = node . value [ 0 ] . raw ;
86125 const value = decodeURI ( raw_value ) ;
@@ -102,6 +141,15 @@ export function preprocessReadme(opts) {
102141 } ,
103142 } ) ;
104143
144+ if ( prev === "h3" ) {
145+ headings . push ( "</ul>" ) ;
146+ }
147+
148+ result = result . replace (
149+ `<h2 id="table-of-contents">Table of Contents</h2>` ,
150+ `<p><strong>Table of Contents</strong></p><ul>${ headings . join ( "\n" ) } </ul>`
151+ ) ;
152+
105153 return {
106154 code : `
107155 <script>${ script_content } </script>
0 commit comments