@@ -49,3 +49,117 @@ export function renderInline(text, resolveLink) {
4949 } while ( s !== before ) ;
5050 return s ;
5151}
52+
53+ const UNSUPPORTED = [
54+ [ / ^ \s * \| / , "a table row" ] ,
55+ [ / ^ \s * ` ` ` / , "a code fence" ] ,
56+ [ / ^ \s * > / , "a blockquote" ] ,
57+ [ / ^ \s * ! \[ / , "an image" ] ,
58+ [ / ^ \s * < [ A - Z a - z ! / ] / , "raw HTML" ] ,
59+ [ / ^ # { 3 , } \s / , "a heading below level 2" ] ,
60+ [ / ^ \s * \d + [ . ) ] \s / , "an ordered list" ] ,
61+ ] ;
62+
63+ const lines = ( md ) => String ( md ) . replace ( / \r \n ? / g, "\n" ) . split ( "\n" ) ;
64+
65+ export function renderBlocks ( md , { file, resolveLink, firstLine = 1 } ) {
66+ const html = [ ] ;
67+ const stack = [ ] ;
68+ let para = [ ] ;
69+ let item = null ;
70+ let blank = false ;
71+ const inline = ( t ) => renderInline ( t , resolveLink ) ;
72+ const flushPara = ( ) => {
73+ if ( para . length ) html . push ( `<p>${ inline ( para . join ( " " ) ) } </p>` ) ;
74+ para = [ ] ;
75+ } ;
76+ const flushItem = ( ) => {
77+ if ( item !== null ) html . push ( inline ( item ) ) ;
78+ item = null ;
79+ } ;
80+ const closeLists = ( ) => {
81+ flushItem ( ) ;
82+ while ( stack . length ) {
83+ html . push ( "</li></ul>" ) ;
84+ stack . pop ( ) ;
85+ }
86+ } ;
87+ lines ( md ) . forEach ( ( line , i ) => {
88+ for ( const [ re , what ] of UNSUPPORTED ) {
89+ if ( re . test ( line ) ) {
90+ throw new MarkdownError ( `${ file } :${ firstLine + i } : ${ what } is not supported by the site renderer` ) ;
91+ }
92+ }
93+ if ( ! line . trim ( ) ) {
94+ flushPara ( ) ;
95+ blank = true ;
96+ return ;
97+ }
98+ const li = line . match ( / ^ ( \s * ) [ - * ] \s + ( .* ) $ / ) ;
99+ if ( li ) {
100+ flushPara ( ) ;
101+ const indent = li [ 1 ] . replace ( / \t / g, " " ) . length ;
102+ if ( ! stack . length ) {
103+ html . push ( "<ul><li>" ) ;
104+ stack . push ( indent ) ;
105+ } else if ( indent > stack [ stack . length - 1 ] ) {
106+ flushItem ( ) ;
107+ html . push ( "<ul><li>" ) ;
108+ stack . push ( indent ) ;
109+ } else {
110+ flushItem ( ) ;
111+ while ( stack . length > 1 && indent < stack [ stack . length - 1 ] ) {
112+ html . push ( "</li></ul>" ) ;
113+ stack . pop ( ) ;
114+ }
115+ html . push ( "</li><li>" ) ;
116+ }
117+ item = li [ 2 ] ;
118+ blank = false ;
119+ return ;
120+ }
121+ if ( stack . length && ! blank && / ^ \s / . test ( line ) ) {
122+ item += ` ${ line . trim ( ) } ` ;
123+ return ;
124+ }
125+ closeLists ( ) ;
126+ blank = false ;
127+ const h = line . match ( / ^ ( # { 1 , 2 } ) \s + ( .* ) $ / ) ;
128+ if ( h ) {
129+ flushPara ( ) ;
130+ if ( h [ 1 ] === "#" ) return ; // the page shows the entry name as its h1
131+ const id = h [ 2 ] . toLowerCase ( ) . replace ( / [ ^ a - z 0 - 9 ] + / g, "-" ) . replace ( / ^ - | - $ / g, "" ) ;
132+ html . push ( `<h2 id="${ esc ( id ) } ">${ inline ( h [ 2 ] ) } </h2>` ) ;
133+ return ;
134+ }
135+ para . push ( line . trim ( ) ) ;
136+ } ) ;
137+ flushPara ( ) ;
138+ closeLists ( ) ;
139+ // Items and their markup were pushed as separate strings; join tight.
140+ return html . join ( "" ) . replace ( / < \/ p > < ( h 2 | p | u l ) / g, "</p>\n<$1" ) . replace ( / < \/ u l > < ( h 2 | p ) / g, "</ul>\n<$1" ) . replace ( / < \/ h 2 > < ( p | u l ) / g, "</h2>\n<$1" ) ;
141+ }
142+
143+ /** Lead (before the first `## `) and the rest, with the rest's first line number. */
144+ export function splitEntryBody ( body ) {
145+ const ls = lines ( body ) ;
146+ const i = ls . findIndex ( ( l ) => / ^ # # \s / . test ( l ) ) ;
147+ if ( i === - 1 ) return { lead : ls . join ( "\n" ) , rest : "" , restFirstLine : ls . length + 1 } ;
148+ return { lead : ls . slice ( 0 , i ) . join ( "\n" ) , rest : ls . slice ( i ) . join ( "\n" ) , restFirstLine : i + 1 } ;
149+ }
150+
151+ /** Raw markdown under `## <heading>`, up to the next `## `, or null. */
152+ export function sectionMarkdown ( body , heading ) {
153+ const ls = lines ( body ) ;
154+ const start = ls . findIndex ( ( l ) => l . trim ( ) === `## ${ heading } ` ) ;
155+ if ( start === - 1 ) return null ;
156+ let end = ls . findIndex ( ( l , j ) => j > start && / ^ # # \s / . test ( l ) ) ;
157+ if ( end === - 1 ) end = ls . length ;
158+ return ls . slice ( start + 1 , end ) . join ( "\n" ) . trim ( ) ;
159+ }
160+
161+ /** The text after the `- Deprecated:` marker line, or null. */
162+ export function deprecationReason ( body ) {
163+ const m = String ( body ) . match ( / ^ - D e p r e c a t e d : \s * ( .+ ) $ / m) ;
164+ return m ? m [ 1 ] . trim ( ) : null ;
165+ }
0 commit comments