const defaultOpt = { lineBreak : '' , indent : 2 , } ; export default ( mdIt , opt ) => { opt = { ... defaultOpt , ... opt } ; let renderType = null ; mdIt . renderer . rules . table_open = function ( ) { return '<table class="rde-table">\n' ; } ; mdIt . renderer . rules . table_close = function ( ) { return '</table>\n' ; } ; mdIt . renderer . rules . th_open = function ( tokens , idx ) { if ( tokens [ idx ] . nesting === 1 ) { const { content } = tokens [ idx ] ; const title = content . split ( ' ' ) [ 0 ] || '' ; const thMatch = content . match ( /\[@th(.*)\]/ ) ; let thStyle = '' ; if ( thMatch ) { thStyle = thMatch [ 1 ] ; } return `<th style="${ thStyle }">${ title }` ; } } ; mdIt . renderer . rules . line_break = ( ) => { return opt . lineBreak ; } ; mdIt . renderer . rules . indent_space = ( ) => { return Array ( opt . indent + 1 ) . join ( ' ' ) ; } ; mdIt . block . ruler . before ( 'fence' , 'table' , function ( state , startLine , _endLine , _silent ) { function getLine ( state , line ) { const pos = state . bMarks [ line ] + state . blkIndent ; const max = state . eMarks [ line ] ; return state . src . substr ( pos , max - pos ) ; } function pushToken ( token , state , tag ) { token = state . push ( 'indent_space' ) ; token = state . push ( tag + '_open' , tag , 1 ) ; token = state . push ( 'inline' , '' , 0 ) ; token . content = content ; token . children = [ ] ; } let token ; let nextLine ; let lineText ; let content ; const startLineEndPos = state . eMarks [ startLine ] ; const pos = state . bMarks [ startLine ] + state . tShift [ startLine ] ; const marker = state . src . slice ( pos + 3 , startLineEndPos ) ; if ( marker !== 'table' ) { return false ; } let ruleEndLine = startLine ; do { ++ ruleEndLine ; } while ( getLine ( state , ruleEndLine ) . trim ( ) !== '```' ) ; token = state . push ( 'line_break' ) ; for ( nextLine = startLine + 1 ; nextLine <= ruleEndLine ; nextLine ++ ) { lineText = getLine ( state , nextLine ) . trim ( ) ; if ( renderType === null ) { token = state . push ( 'table_open' , 'table' , 1 ) ; token = state . push ( 'tr_open' , 'tr' , 1 ) ; } if ( lineText === '```' ) { if ( renderType === 2 ) { token = state . push ( 'td_close' , 'td' , - 1 ) ; } token = state . push ( 'tr_close' , 'tr' , - 1 ) ; token = state . push ( 'table_close' , 'table' , - 1 ) ; renderType = null ; state . line = ruleEndLine + 2 ; return true ; } if ( /^\|-|\|-$/g . test ( lineText ) ) { if ( renderType === 0 ) { token = state . push ( 'th_close' , 'th' , - 1 ) ; token = state . push ( 'tr_close' , 'tr' , - 1 ) ; } if ( renderType === 1 ) { token = state . push ( 'td_close' , 'td' , - 1 ) ; token = state . push ( 'tr_close' , 'tr' , - 1 ) ; } if ( renderType === 2 ) { token = state . push ( 'td_close' , 'td' , - 1 ) ; } renderType = 1 ; } else if ( /^\||\|$/g . test ( lineText ) ) { if ( renderType === 0 ) { throw new Error ( 'th后不能写普通td' ) ; } token = state . push ( 'td_close' , 'td' , - 1 ) ; renderType = 2 ; } else { if ( [ 1 , 2 ] . includes ( renderType ) ) { throw new Error ( 'th应写在td前面' ) ; } token = state . push ( 'th_close' , 'th' , - 1 ) ; renderType = 0 ; } if ( renderType === 0 ) { token = state . push ( 'th_open' , 'th' , 1 ) ; token . content = lineText ; } else if ( renderType === 1 ) { content = lineText . replace ( /^\|-\s|\|-$/g , '' ) ; token = state . push ( 'tr_open' , 'tr' , 1 ) ; pushToken ( token , state , 'td' ) ; } else if ( renderType === 2 ) { content = lineText . replace ( /^\|\s|\|$/g , '' ) ; pushToken ( token , state , 'td' ) ; } } } ) ; } ;