1- // BASED ON https://github.com/serkodev/markdown-exit/blob/fe1351070a5841426223ab4a0a5c7874ba2b1257/packages/markdown-exit/src/parser/block/rules/html_block.ts
1+ // Standard CommonMark html_block rule — see
2+ // https://spec.commonmark.org/0.30/#html-blocks
3+ //
4+ // 7 sequences in priority order, each: [opener regex, closer regex, can-terminate-paragraph]
25
36import type { StateBlock } from 'markdown-exit'
47import block_names from './html_blocks.ts'
58import { HTML_OPEN_CLOSE_TAG_RE } from './html_re.ts'
69
7- // An array of opening and corresponding closing sequences for html tags,
8- // last argument defines whether it can terminate a paragraph or not
9- //
1010const HTML_SEQUENCES : [ RegExp , RegExp , boolean ] [ ] = [
11- [ new RegExp ( `${ HTML_OPEN_CLOSE_TAG_RE . source } \\s*$` ) , / ^ < \/ [ ^ > ] + > $ / , true ] ,
1211 [ / ^ < ( s c r i p t | p r e | s t y l e | t e x t a r e a ) (? = ( \s | > | $ ) ) / i, / < \/ ( s c r i p t | p r e | s t y l e | t e x t a r e a ) > / i, true ] ,
1312 [ / ^ < ! - - / , / - - > / , true ] ,
1413 [ / ^ < \? / , / \? > / , true ] ,
@@ -22,9 +21,7 @@ export default function html_block(state: StateBlock, startLine: number, endLine
2221 let pos = state . bMarks [ startLine ] + state . tShift [ startLine ]
2322 let max = state . eMarks [ startLine ]
2423
25- // if it's indented more than 3 spaces, it should be a code block
2624 if ( state . sCount [ startLine ] - state . blkIndent >= 4 ) return false
27-
2825 if ( state . src . charCodeAt ( pos ) !== 0x3c /* < */ ) return false
2926
3027 let lineText = state . src . slice ( pos , max )
@@ -33,23 +30,16 @@ export default function html_block(state: StateBlock, startLine: number, endLine
3330 for ( ; i < HTML_SEQUENCES . length ; i ++ ) {
3431 if ( HTML_SEQUENCES [ i ] [ 0 ] . test ( lineText ) ) break
3532 }
36-
3733 if ( i === HTML_SEQUENCES . length ) return false
3834
39- if ( silent ) {
40- // true if this sequence can be a terminator, false otherwise
41- return HTML_SEQUENCES [ i ] [ 2 ]
42- }
35+ if ( silent ) return HTML_SEQUENCES [ i ] [ 2 ]
4336
4437 let nextLine = startLine + 1
4538
46- // If we are here - we detected HTML block.
47- // Let's roll down till block end.
48- if ( i !== 0 && ! HTML_SEQUENCES [ i ] [ 1 ] . test ( lineText ) ) {
39+ // Walk forward until the closer regex matches or we hit a blank line.
40+ if ( ! HTML_SEQUENCES [ i ] [ 1 ] . test ( lineText ) ) {
4941 for ( ; nextLine < endLine ; nextLine ++ ) {
50- if ( state . sCount [ nextLine ] < state . blkIndent ) {
51- break
52- }
42+ if ( state . sCount [ nextLine ] < state . blkIndent ) break
5343
5444 pos = state . bMarks [ nextLine ] + state . tShift [ nextLine ]
5545 max = state . eMarks [ nextLine ]
@@ -61,9 +51,9 @@ export default function html_block(state: StateBlock, startLine: number, endLine
6151 }
6252 }
6353 }
64- state . line = nextLine
6554
66- const token = lineText . startsWith ( '</' ) ? state . push ( 'html_block_close' , '' , - 1 ) : state . push ( 'html_block' , '' , 1 )
55+ state . line = nextLine
56+ const token = state . push ( 'html_block' , '' , 1 )
6757 token . map = [ startLine , nextLine ]
6858 token . content = state . getLines ( startLine , nextLine , state . blkIndent , true )
6959
0 commit comments