@@ -76,17 +76,29 @@ function stripMediaTokens(text) {
7676}
7777
7878/**
79- * Replace every indented code line (4+ leading spaces or a tab) with a
80- * placeholder of equal length so media stripping cannot touch it. Returns the
81- * masked text plus the original lines for restoration.
79+ * Replace indented code lines (4+ leading spaces or a tab) with placeholders
80+ * so media stripping cannot touch them. Indented children of an unindented
81+ * HTML media block remain visible because their indentation is just HTML
82+ * formatting. Returns the masked text plus the original lines for restoration.
8283 */
8384function protectIndentedCodeLines ( text ) {
8485 const lines = [ ] ;
86+ let mediaBlockDepth = 0 ;
8587 const masked = text . split ( "\n" ) . map ( ( line ) => {
86- if ( / ^ (?: { 4 , } | \t ) / . test ( line ) ) {
88+ const isIndented = / ^ (?: { 4 , } | \t ) / . test ( line ) ;
89+ if ( isIndented && mediaBlockDepth === 0 ) {
8790 lines . push ( line ) ;
8891 return "\u0000" + line . replace ( / [ ^ \n ] / g, " " ) . slice ( 1 ) ;
8992 }
93+
94+ // Indentation inside a real HTML media block is formatting, not a
95+ // Markdown indented-code block. Leave child tags visible to the HTML
96+ // stripper. An indented opening tag at depth zero remains protected as
97+ // literal Markdown code and therefore must not start a block here.
98+ for ( const tag of line . matchAll ( / < ( \/ ) ? ( p i c t u r e | v i d e o | a u d i o ) \b [ ^ > ] * > / gi) ) {
99+ if ( tag [ 1 ] ) mediaBlockDepth = Math . max ( 0 , mediaBlockDepth - 1 ) ;
100+ else if ( ! / \/ \s * > $ / . test ( tag [ 0 ] ) ) mediaBlockDepth += 1 ;
101+ }
90102 lines . push ( null ) ;
91103 return line ;
92104 } ) ;
0 commit comments