2019-11-07 12:49:10 -06:00
/ * *
2023-07-25 05:11:13 +00:00
* marked v5 . 1.2 - a markdown parser
2023-01-13 21:27:58 -05:00
* Copyright ( c ) 2011 - 2023 , Christopher Jeffrey . ( MIT Licensed )
2019-11-07 12:49:10 -06:00
* https : //github.com/markedjs/marked
* /
/ * *
* DO NOT EDIT THIS FILE
* The code in this file is generated from files in . / src /
* /
2023-07-29 08:31:34 +02:00
var _ _accessCheck = ( obj , member , msg ) => {
if ( ! member . has ( obj ) )
throw TypeError ( "Cannot " + msg ) ;
} ;
var _ _privateAdd = ( obj , member , value ) => {
if ( member . has ( obj ) )
throw TypeError ( "Cannot add the same private member more than once" ) ;
member instanceof WeakSet ? member . add ( obj ) : member . set ( obj , value ) ;
} ;
var _ _privateMethod = ( obj , member , method ) => {
_ _accessCheck ( obj , member , "access private method" ) ;
return method ;
} ;
// src/defaults.ts
function _getDefaults ( ) {
2019-11-07 12:49:10 -06:00
return {
2022-08-30 14:37:45 +00:00
async : false ,
2019-11-07 12:49:10 -06:00
baseUrl : null ,
breaks : false ,
2021-06-15 19:22:00 -04:00
extensions : null ,
2019-11-07 12:49:10 -06:00
gfm : true ,
headerIds : true ,
2023-07-29 08:31:34 +02:00
headerPrefix : "" ,
2019-11-07 12:49:10 -06:00
highlight : null ,
2023-03-22 05:52:21 +00:00
hooks : null ,
2023-07-29 08:31:34 +02:00
langPrefix : "language-" ,
2019-11-07 12:49:10 -06:00
mangle : true ,
pedantic : false ,
renderer : null ,
sanitize : false ,
sanitizer : null ,
silent : false ,
smartypants : false ,
2020-04-08 13:06:43 -05:00
tokenizer : null ,
2020-05-14 15:54:39 +00:00
walkTokens : null ,
2019-11-07 12:49:10 -06:00
xhtml : false
} ;
}
2023-07-29 08:31:34 +02:00
var _defaults = _getDefaults ( ) ;
2021-11-02 07:32:17 -07:00
function changeDefaults ( newDefaults ) {
2023-07-29 08:31:34 +02:00
_defaults = newDefaults ;
2021-11-02 07:32:17 -07:00
}
2019-11-07 12:49:10 -06:00
2023-07-29 08:31:34 +02:00
// src/helpers.ts
var escapeTest = /[&<>"']/ ;
var escapeReplace = new RegExp ( escapeTest . source , "g" ) ;
var escapeTestNoEncode = /[<>"']|&(?!(#\d{1,7}|#[Xx][a-fA-F0-9]{1,6}|\w+);)/ ;
var escapeReplaceNoEncode = new RegExp ( escapeTestNoEncode . source , "g" ) ;
var escapeReplacements = {
"&" : "&" ,
"<" : "<" ,
">" : ">" ,
'"' : """ ,
"'" : "'"
2019-12-05 23:08:43 +00:00
} ;
2023-07-29 08:31:34 +02:00
var getEscapeReplacement = ( ch ) => escapeReplacements [ ch ] ;
2021-11-02 07:32:17 -07:00
function escape ( html , encode ) {
2019-11-07 12:49:10 -06:00
if ( encode ) {
2019-12-05 23:08:43 +00:00
if ( escapeTest . test ( html ) ) {
return html . replace ( escapeReplace , getEscapeReplacement ) ;
2019-11-07 12:49:10 -06:00
}
} else {
2019-12-05 23:08:43 +00:00
if ( escapeTestNoEncode . test ( html ) ) {
return html . replace ( escapeReplaceNoEncode , getEscapeReplacement ) ;
2019-11-07 12:49:10 -06:00
}
}
return html ;
}
2023-07-29 08:31:34 +02:00
var unescapeTest = /&(#(?:\d+)|(?:#x[0-9A-Fa-f]+)|(?:\w+));?/ig ;
2021-11-02 07:32:17 -07:00
function unescape ( html ) {
2019-12-05 23:08:43 +00:00
return html . replace ( unescapeTest , ( _ , n ) => {
2019-11-07 12:49:10 -06:00
n = n . toLowerCase ( ) ;
2023-07-29 08:31:34 +02:00
if ( n === "colon" )
return ":" ;
if ( n . charAt ( 0 ) === "#" ) {
return n . charAt ( 1 ) === "x" ? String . fromCharCode ( parseInt ( n . substring ( 2 ) , 16 ) ) : String . fromCharCode ( + n . substring ( 1 ) ) ;
2019-11-07 12:49:10 -06:00
}
2023-07-29 08:31:34 +02:00
return "" ;
2019-11-07 12:49:10 -06:00
} ) ;
}
2023-07-29 08:31:34 +02:00
var caret = /(^|[^\[])\^/g ;
2021-11-02 07:32:17 -07:00
function edit ( regex , opt ) {
2023-07-29 08:31:34 +02:00
regex = typeof regex === "string" ? regex : regex . source ;
opt = opt || "" ;
2019-11-07 12:49:10 -06:00
const obj = {
replace : ( name , val ) => {
2023-07-29 08:31:34 +02:00
val = typeof val === "object" && "source" in val ? val . source : val ;
val = val . replace ( caret , "$1" ) ;
2019-11-07 12:49:10 -06:00
regex = regex . replace ( name , val ) ;
return obj ;
} ,
getRegex : ( ) => {
return new RegExp ( regex , opt ) ;
}
} ;
return obj ;
}
2023-07-29 08:31:34 +02:00
var nonWordAndColonTest = /[^\w:]/g ;
var originIndependentUrl = /^$|^[a-z][a-z0-9+.-]*:|^[?#]/i ;
2021-11-02 07:32:17 -07:00
function cleanUrl ( sanitize , base , href ) {
2019-11-07 12:49:10 -06:00
if ( sanitize ) {
let prot ;
try {
2023-07-29 08:31:34 +02:00
prot = decodeURIComponent ( unescape ( href ) ) . replace ( nonWordAndColonTest , "" ) . toLowerCase ( ) ;
2019-11-07 12:49:10 -06:00
} catch ( e ) {
return null ;
}
2023-07-29 08:31:34 +02:00
if ( prot . indexOf ( "javascript:" ) === 0 || prot . indexOf ( "vbscript:" ) === 0 || prot . indexOf ( "data:" ) === 0 ) {
2019-11-07 12:49:10 -06:00
return null ;
}
}
2019-12-05 23:08:43 +00:00
if ( base && ! originIndependentUrl . test ( href ) ) {
2019-11-07 12:49:10 -06:00
href = resolveUrl ( base , href ) ;
}
try {
2023-07-29 08:31:34 +02:00
href = encodeURI ( href ) . replace ( /%25/g , "%" ) ;
2019-11-07 12:49:10 -06:00
} catch ( e ) {
return null ;
}
return href ;
}
2023-07-29 08:31:34 +02:00
var baseUrls = { } ;
var justDomain = /^[^:]+:\/*[^/]*$/ ;
var protocol = /^([^:]+:)[\s\S]*$/ ;
var domain = /^([^:]+:\/*[^/]*)[\s\S]*$/ ;
2019-11-07 12:49:10 -06:00
function resolveUrl ( base , href ) {
2023-07-29 08:31:34 +02:00
if ( ! baseUrls [ " " + base ] ) {
2019-12-05 23:08:43 +00:00
if ( justDomain . test ( base ) ) {
2023-07-29 08:31:34 +02:00
baseUrls [ " " + base ] = base + "/" ;
2019-11-07 12:49:10 -06:00
} else {
2023-07-29 08:31:34 +02:00
baseUrls [ " " + base ] = rtrim ( base , "/" , true ) ;
2019-11-07 12:49:10 -06:00
}
}
2023-07-29 08:31:34 +02:00
base = baseUrls [ " " + base ] ;
const relativeBase = base . indexOf ( ":" ) === - 1 ;
if ( href . substring ( 0 , 2 ) === "//" ) {
2019-11-07 12:49:10 -06:00
if ( relativeBase ) {
return href ;
}
2023-07-29 08:31:34 +02:00
return base . replace ( protocol , "$1" ) + href ;
} else if ( href . charAt ( 0 ) === "/" ) {
2019-11-07 12:49:10 -06:00
if ( relativeBase ) {
return href ;
}
2023-07-29 08:31:34 +02:00
return base . replace ( domain , "$1" ) + href ;
2019-11-07 12:49:10 -06:00
} else {
return base + href ;
}
}
2023-07-29 08:31:34 +02:00
var noopTest = { exec : ( ) => null } ;
2021-11-02 07:32:17 -07:00
function splitCells ( tableRow , count ) {
2019-11-07 12:49:10 -06:00
const row = tableRow . replace ( /\|/g , ( match , offset , str ) => {
2023-07-29 08:31:34 +02:00
let escaped = false , curr = offset ;
while ( -- curr >= 0 && str [ curr ] === "\\" )
escaped = ! escaped ;
if ( escaped ) {
return "|" ;
} else {
return " |" ;
}
} ) , cells = row . split ( / \|/ ) ;
2019-11-07 12:49:10 -06:00
let i = 0 ;
2023-07-29 08:31:34 +02:00
if ( ! cells [ 0 ] . trim ( ) ) {
cells . shift ( ) ;
}
if ( cells . length > 0 && ! cells [ cells . length - 1 ] . trim ( ) ) {
cells . pop ( ) ;
}
2019-11-07 12:49:10 -06:00
if ( cells . length > count ) {
cells . splice ( count ) ;
} else {
2023-07-29 08:31:34 +02:00
while ( cells . length < count )
cells . push ( "" ) ;
2019-11-07 12:49:10 -06:00
}
for ( ; i < cells . length ; i ++ ) {
2023-07-29 08:31:34 +02:00
cells [ i ] = cells [ i ] . trim ( ) . replace ( /\\\|/g , "|" ) ;
2019-11-07 12:49:10 -06:00
}
return cells ;
}
2021-11-02 07:32:17 -07:00
function rtrim ( str , c , invert ) {
2019-11-07 12:49:10 -06:00
const l = str . length ;
if ( l === 0 ) {
2023-07-29 08:31:34 +02:00
return "" ;
2019-11-07 12:49:10 -06:00
}
let suffLen = 0 ;
while ( suffLen < l ) {
const currChar = str . charAt ( l - suffLen - 1 ) ;
if ( currChar === c && ! invert ) {
suffLen ++ ;
} else if ( currChar !== c && invert ) {
suffLen ++ ;
} else {
break ;
}
}
2022-04-08 01:54:20 +00:00
return str . slice ( 0 , l - suffLen ) ;
2019-11-07 12:49:10 -06:00
}
2021-11-02 07:32:17 -07:00
function findClosingBracket ( str , b ) {
2019-11-07 12:49:10 -06:00
if ( str . indexOf ( b [ 1 ] ) === - 1 ) {
return - 1 ;
}
const l = str . length ;
2023-07-29 08:31:34 +02:00
let level = 0 , i = 0 ;
2019-11-07 12:49:10 -06:00
for ( ; i < l ; i ++ ) {
2023-07-29 08:31:34 +02:00
if ( str [ i ] === "\\" ) {
2019-11-07 12:49:10 -06:00
i ++ ;
} else if ( str [ i ] === b [ 0 ] ) {
level ++ ;
} else if ( str [ i ] === b [ 1 ] ) {
level -- ;
if ( level < 0 ) {
return i ;
}
}
}
return - 1 ;
}
2023-05-02 04:35:05 +00:00
function checkDeprecations ( opt , callback ) {
if ( ! opt || opt . silent ) {
return ;
}
if ( callback ) {
2023-07-29 08:31:34 +02:00
console . warn ( "marked(): callback is deprecated since version 5.0.0, should not be used and will be removed in the future. Read more here: https://marked.js.org/using_pro#async" ) ;
2023-05-02 04:35:05 +00:00
}
if ( opt . sanitize || opt . sanitizer ) {
2023-07-29 08:31:34 +02:00
console . warn ( "marked(): sanitize and sanitizer parameters are deprecated since version 0.7.0, should not be used and will be removed in the future. Read more here: https://marked.js.org/#/USING_ADVANCED.md#options" ) ;
2019-11-07 12:49:10 -06:00
}
2023-07-29 08:31:34 +02:00
if ( opt . highlight || opt . langPrefix !== "language-" ) {
console . warn ( "marked(): highlight and langPrefix parameters are deprecated since version 5.0.0, should not be used and will be removed in the future. Instead use https://www.npmjs.com/package/marked-highlight." ) ;
2023-05-02 04:35:05 +00:00
}
if ( opt . mangle ) {
2023-07-29 08:31:34 +02:00
console . warn ( "marked(): mangle parameter is enabled by default, but is deprecated since version 5.0.0, and will be removed in the future. To clear this warning, install https://www.npmjs.com/package/marked-mangle, or disable by setting `{mangle: false}`." ) ;
2023-05-02 04:35:05 +00:00
}
if ( opt . baseUrl ) {
2023-07-29 08:31:34 +02:00
console . warn ( "marked(): baseUrl parameter is deprecated since version 5.0.0, should not be used and will be removed in the future. Instead use https://www.npmjs.com/package/marked-base-url." ) ;
2023-05-02 04:35:05 +00:00
}
if ( opt . smartypants ) {
2023-07-29 08:31:34 +02:00
console . warn ( "marked(): smartypants parameter is deprecated since version 5.0.0, should not be used and will be removed in the future. Instead use https://www.npmjs.com/package/marked-smartypants." ) ;
2023-05-02 04:35:05 +00:00
}
if ( opt . xhtml ) {
2023-07-29 08:31:34 +02:00
console . warn ( "marked(): xhtml parameter is deprecated since version 5.0.0, should not be used and will be removed in the future. Instead use https://www.npmjs.com/package/marked-xhtml." ) ;
2023-05-02 04:35:05 +00:00
}
if ( opt . headerIds || opt . headerPrefix ) {
2023-07-29 08:31:34 +02:00
console . warn ( "marked(): headerIds and headerPrefix parameters enabled by default, but are deprecated since version 5.0.0, and will be removed in the future. To clear this warning, install https://www.npmjs.com/package/marked-gfm-heading-id, or disable by setting `{headerIds: false}`." ) ;
2023-05-02 04:35:05 +00:00
}
2019-11-07 12:49:10 -06:00
}
2023-07-29 08:31:34 +02:00
// src/Tokenizer.ts
function outputLink ( cap , link , raw , lexer2 ) {
2020-04-14 16:41:10 -05:00
const href = link . href ;
2021-11-02 07:32:17 -07:00
const title = link . title ? escape ( link . title ) : null ;
2023-07-29 08:31:34 +02:00
const text = cap [ 1 ] . replace ( /\\([\[\]])/g , "$1" ) ;
if ( cap [ 0 ] . charAt ( 0 ) !== "!" ) {
lexer2 . state . inLink = true ;
2021-08-23 18:48:45 +00:00
const token = {
2023-07-29 08:31:34 +02:00
type : "link" ,
2020-04-14 16:41:10 -05:00
raw ,
href ,
title ,
2021-08-09 23:41:45 -04:00
text ,
2023-07-29 08:31:34 +02:00
tokens : lexer2 . inlineTokens ( text )
2020-04-14 16:41:10 -05:00
} ;
2023-07-29 08:31:34 +02:00
lexer2 . state . inLink = false ;
2021-08-23 18:48:45 +00:00
return token ;
2020-04-14 16:41:10 -05:00
}
2022-05-02 06:13:56 +00:00
return {
2023-07-29 08:31:34 +02:00
type : "image" ,
2022-05-02 06:13:56 +00:00
raw ,
href ,
title ,
text : escape ( text )
} ;
2020-04-14 16:41:10 -05:00
}
2020-05-02 01:31:47 +00:00
function indentCodeCompensation ( raw , text ) {
const matchIndentToCode = raw . match ( /^(\s+)(?:```)/ ) ;
if ( matchIndentToCode === null ) {
return text ;
}
const indentToCode = matchIndentToCode [ 1 ] ;
2023-07-29 08:31:34 +02:00
return text . split ( "\n" ) . map ( ( node ) => {
const matchIndentInNode = node . match ( /^\s+/ ) ;
if ( matchIndentInNode === null ) {
2020-05-02 01:31:47 +00:00
return node ;
2023-07-29 08:31:34 +02:00
}
const [ indentInNode ] = matchIndentInNode ;
if ( indentInNode . length >= indentToCode . length ) {
return node . slice ( indentToCode . length ) ;
}
return node ;
} ) . join ( "\n" ) ;
2020-05-02 01:31:47 +00:00
}
2023-07-29 08:31:34 +02:00
var _Tokenizer = class {
constructor ( options2 ) {
this . options = options2 || _defaults ;
2020-04-14 16:41:10 -05:00
}
space ( src ) {
const cap = this . rules . block . newline . exec ( src ) ;
2022-01-06 15:33:25 +00:00
if ( cap && cap [ 0 ] . length > 0 ) {
return {
2023-07-29 08:31:34 +02:00
type : "space" ,
2022-01-06 15:33:25 +00:00
raw : cap [ 0 ]
} ;
2020-04-14 16:41:10 -05:00
}
}
2021-02-07 17:22:47 -05:00
code ( src ) {
2020-04-14 16:41:10 -05:00
const cap = this . rules . block . code . exec ( src ) ;
if ( cap ) {
2023-07-29 08:31:34 +02:00
const text = cap [ 0 ] . replace ( /^ {1,4}/gm , "" ) ;
2020-05-03 19:20:54 +00:00
return {
2023-07-29 08:31:34 +02:00
type : "code" ,
2020-05-03 19:20:54 +00:00
raw : cap [ 0 ] ,
2023-07-29 08:31:34 +02:00
codeBlockStyle : "indented" ,
text : ! this . options . pedantic ? rtrim ( text , "\n" ) : text
2020-05-03 19:20:54 +00:00
} ;
2020-04-14 16:41:10 -05:00
}
}
fences ( src ) {
const cap = this . rules . block . fences . exec ( src ) ;
if ( cap ) {
2020-05-02 01:31:47 +00:00
const raw = cap [ 0 ] ;
2023-07-29 08:31:34 +02:00
const text = indentCodeCompensation ( raw , cap [ 3 ] || "" ) ;
2020-04-14 16:41:10 -05:00
return {
2023-07-29 08:31:34 +02:00
type : "code" ,
2020-05-02 01:31:47 +00:00
raw ,
2023-07-29 08:31:34 +02:00
lang : cap [ 2 ] ? cap [ 2 ] . trim ( ) . replace ( this . rules . inline . _escapes , "$1" ) : cap [ 2 ] ,
2020-05-02 01:31:47 +00:00
text
2020-04-14 16:41:10 -05:00
} ;
}
}
heading ( src ) {
const cap = this . rules . block . heading . exec ( src ) ;
if ( cap ) {
2020-12-10 10:28:58 -06:00
let text = cap [ 2 ] . trim ( ) ;
2020-12-15 14:14:22 -06:00
if ( /#$/ . test ( text ) ) {
2023-07-29 08:31:34 +02:00
const trimmed = rtrim ( text , "#" ) ;
2020-12-10 10:28:58 -06:00
if ( this . options . pedantic ) {
text = trimmed . trim ( ) ;
2020-12-15 14:14:22 -06:00
} else if ( ! trimmed || / $/ . test ( trimmed ) ) {
2020-12-10 10:28:58 -06:00
text = trimmed . trim ( ) ;
}
}
2022-08-30 14:37:45 +00:00
return {
2023-07-29 08:31:34 +02:00
type : "heading" ,
2020-04-14 16:41:10 -05:00
raw : cap [ 0 ] ,
depth : cap [ 1 ] . length ,
2022-05-02 06:13:56 +00:00
text ,
2022-08-30 14:37:45 +00:00
tokens : this . lexer . inline ( text )
2020-04-14 16:41:10 -05:00
} ;
}
}
hr ( src ) {
const cap = this . rules . block . hr . exec ( src ) ;
if ( cap ) {
return {
2023-07-29 08:31:34 +02:00
type : "hr" ,
2020-04-14 16:41:10 -05:00
raw : cap [ 0 ]
} ;
}
}
blockquote ( src ) {
const cap = this . rules . block . blockquote . exec ( src ) ;
if ( cap ) {
2023-07-29 08:31:34 +02:00
const text = cap [ 0 ] . replace ( /^ *>[ \t]?/gm , "" ) ;
2022-12-07 07:46:08 +00:00
const top = this . lexer . state . top ;
this . lexer . state . top = true ;
const tokens = this . lexer . blockTokens ( text ) ;
this . lexer . state . top = top ;
2020-04-14 16:41:10 -05:00
return {
2023-07-29 08:31:34 +02:00
type : "blockquote" ,
2020-04-14 16:41:10 -05:00
raw : cap [ 0 ] ,
2022-12-07 07:46:08 +00:00
tokens ,
2020-04-14 16:41:10 -05:00
text
} ;
}
}
list ( src ) {
2021-08-09 23:41:45 -04:00
let cap = this . rules . block . list . exec ( src ) ;
2020-04-14 16:41:10 -05:00
if ( cap ) {
2023-07-29 08:31:34 +02:00
let raw , istask , ischecked , indent , i , blankLine , endsWithBlankLine , line , nextLine , rawLine , itemContents , endEarly ;
2021-08-09 23:41:45 -04:00
let bull = cap [ 1 ] . trim ( ) ;
2020-04-14 16:41:10 -05:00
const isordered = bull . length > 1 ;
const list = {
2023-07-29 08:31:34 +02:00
type : "list" ,
raw : "" ,
2020-04-14 16:41:10 -05:00
ordered : isordered ,
2023-07-29 08:31:34 +02:00
start : isordered ? + bull . slice ( 0 , - 1 ) : "" ,
2020-04-14 16:41:10 -05:00
loose : false ,
items : [ ]
} ;
2021-08-09 23:41:45 -04:00
bull = isordered ? ` \\ d{1,9} \\ ${ bull . slice ( - 1 ) } ` : ` \\ ${ bull } ` ;
if ( this . options . pedantic ) {
2023-07-29 08:31:34 +02:00
bull = isordered ? bull : "[*+-]" ;
2021-08-09 23:41:45 -04:00
}
2023-07-29 08:31:34 +02:00
const itemRegex = new RegExp ( ` ^( {0,3} ${ bull } )((?:[ ][^ \\ n]*)?(?: \\ n| $ )) ` ) ;
2021-08-09 23:41:45 -04:00
while ( src ) {
2021-12-09 23:59:39 +00:00
endEarly = false ;
2021-12-01 22:18:07 -05:00
if ( ! ( cap = itemRegex . exec ( src ) ) ) {
2021-08-09 23:41:45 -04:00
break ;
2021-02-26 23:51:21 -06:00
}
2023-07-29 08:31:34 +02:00
if ( this . rules . block . hr . test ( src ) ) {
2021-08-09 23:41:45 -04:00
break ;
2020-11-04 21:24:40 +00:00
}
2021-12-01 22:18:07 -05:00
raw = cap [ 0 ] ;
src = src . substring ( raw . length ) ;
2023-07-29 08:31:34 +02:00
line = cap [ 2 ] . split ( "\n" , 1 ) [ 0 ] . replace ( /^\t+/ , ( t ) => " " . repeat ( 3 * t . length ) ) ;
nextLine = src . split ( "\n" , 1 ) [ 0 ] ;
2021-08-09 23:41:45 -04:00
if ( this . options . pedantic ) {
indent = 2 ;
2021-12-01 22:18:07 -05:00
itemContents = line . trimLeft ( ) ;
2021-08-09 23:41:45 -04:00
} else {
2023-07-29 08:31:34 +02:00
indent = cap [ 2 ] . search ( /[^ ]/ ) ;
indent = indent > 4 ? 1 : indent ;
2021-12-01 22:18:07 -05:00
itemContents = line . slice ( indent ) ;
indent += cap [ 1 ] . length ;
2020-04-14 16:41:10 -05:00
}
2021-08-09 23:41:45 -04:00
blankLine = false ;
2023-07-29 08:31:34 +02:00
if ( ! line && /^ *$/ . test ( nextLine ) ) {
raw += nextLine + "\n" ;
2021-12-01 22:18:07 -05:00
src = src . substring ( nextLine . length + 1 ) ;
2021-12-09 23:59:39 +00:00
endEarly = true ;
2021-02-26 23:51:21 -06:00
}
2021-12-09 23:59:39 +00:00
if ( ! endEarly ) {
2023-07-29 08:31:34 +02:00
const nextBulletRegex = new RegExp ( ` ^ {0, ${ Math . min ( 3 , indent - 1 ) } }(?:[*+-]| \\ d{1,9}[.)])((?:[ ][^ \\ n]*)?(?: \\ n| $ )) ` ) ;
2022-05-02 06:13:56 +00:00
const hrRegex = new RegExp ( ` ^ {0, ${ Math . min ( 3 , indent - 1 ) } }((?:- *){3,}|(?:_ *){3,}|(?: \\ * *){3,})(?: \\ n+| $ ) ` ) ;
2022-07-11 15:15:22 +00:00
const fencesBeginRegex = new RegExp ( ` ^ {0, ${ Math . min ( 3 , indent - 1 ) } }(?: \` \` \` |~~~) ` ) ;
const headingBeginRegex = new RegExp ( ` ^ {0, ${ Math . min ( 3 , indent - 1 ) } }# ` ) ;
2021-12-09 23:59:39 +00:00
while ( src ) {
2023-07-29 08:31:34 +02:00
rawLine = src . split ( "\n" , 1 ) [ 0 ] ;
2022-12-23 15:40:26 +00:00
nextLine = rawLine ;
2021-12-09 23:59:39 +00:00
if ( this . options . pedantic ) {
2023-07-29 08:31:34 +02:00
nextLine = nextLine . replace ( /^ {1,4}(?=( {4})*[^ ])/g , " " ) ;
2021-12-09 23:59:39 +00:00
}
2022-12-23 15:40:26 +00:00
if ( fencesBeginRegex . test ( nextLine ) ) {
2022-06-13 03:16:22 +00:00
break ;
}
2022-12-23 15:40:26 +00:00
if ( headingBeginRegex . test ( nextLine ) ) {
2022-06-13 03:16:22 +00:00
break ;
}
2022-12-23 15:40:26 +00:00
if ( nextBulletRegex . test ( nextLine ) ) {
2021-12-09 23:59:39 +00:00
break ;
}
2022-05-02 06:13:56 +00:00
if ( hrRegex . test ( src ) ) {
break ;
}
2023-07-29 08:31:34 +02:00
if ( nextLine . search ( /[^ ]/ ) >= indent || ! nextLine . trim ( ) ) {
itemContents += "\n" + nextLine . slice ( indent ) ;
2022-12-23 15:40:26 +00:00
} else {
if ( blankLine ) {
break ;
}
2023-07-29 08:31:34 +02:00
if ( line . search ( /[^ ]/ ) >= 4 ) {
2022-12-23 15:40:26 +00:00
break ;
}
if ( fencesBeginRegex . test ( line ) ) {
break ;
}
if ( headingBeginRegex . test ( line ) ) {
break ;
}
if ( hrRegex . test ( line ) ) {
break ;
}
2023-07-29 08:31:34 +02:00
itemContents += "\n" + nextLine ;
2021-12-09 23:59:39 +00:00
}
2023-07-29 08:31:34 +02:00
if ( ! blankLine && ! nextLine . trim ( ) ) {
2021-12-09 23:59:39 +00:00
blankLine = true ;
}
2023-07-29 08:31:34 +02:00
raw += rawLine + "\n" ;
2021-12-09 23:59:39 +00:00
src = src . substring ( rawLine . length + 1 ) ;
2022-12-23 15:40:26 +00:00
line = nextLine . slice ( indent ) ;
2021-08-09 23:41:45 -04:00
}
2020-04-14 16:41:10 -05:00
}
2021-08-09 23:41:45 -04:00
if ( ! list . loose ) {
if ( endsWithBlankLine ) {
list . loose = true ;
} else if ( /\n *\n *$/ . test ( raw ) ) {
endsWithBlankLine = true ;
}
2020-04-14 16:41:10 -05:00
}
2020-11-19 14:32:02 +00:00
if ( this . options . gfm ) {
2021-08-09 23:41:45 -04:00
istask = /^\[[ xX]\] / . exec ( itemContents ) ;
2020-11-19 14:32:02 +00:00
if ( istask ) {
2023-07-29 08:31:34 +02:00
ischecked = istask [ 0 ] !== "[ ] " ;
itemContents = itemContents . replace ( /^\[[ xX]\] +/ , "" ) ;
2020-11-19 14:32:02 +00:00
}
2020-04-14 16:41:10 -05:00
}
list . items . push ( {
2023-07-29 08:31:34 +02:00
type : "list_item" ,
2022-05-02 06:13:56 +00:00
raw ,
2021-08-09 23:41:45 -04:00
task : ! ! istask ,
2020-04-14 16:41:10 -05:00
checked : ischecked ,
2021-08-09 23:41:45 -04:00
loose : false ,
text : itemContents
2020-04-14 16:41:10 -05:00
} ) ;
2021-08-09 23:41:45 -04:00
list . raw += raw ;
}
list . items [ list . items . length - 1 ] . raw = raw . trimRight ( ) ;
list . items [ list . items . length - 1 ] . text = itemContents . trimRight ( ) ;
list . raw = list . raw . trimRight ( ) ;
const l = list . items . length ;
for ( i = 0 ; i < l ; i ++ ) {
this . lexer . state . top = false ;
list . items [ i ] . tokens = this . lexer . blockTokens ( list . items [ i ] . text , [ ] ) ;
2022-12-07 07:46:08 +00:00
if ( ! list . loose ) {
2023-07-29 08:31:34 +02:00
const spacers = list . items [ i ] . tokens . filter ( ( t ) => t . type === "space" ) ;
const hasMultipleLineBreaks = spacers . length > 0 && spacers . some ( ( t ) => / \ n . * \ n / . test ( t . raw ) ) ;
2022-12-07 07:46:08 +00:00
list . loose = hasMultipleLineBreaks ;
}
}
if ( list . loose ) {
for ( i = 0 ; i < l ; i ++ ) {
2021-08-09 23:41:45 -04:00
list . items [ i ] . loose = true ;
}
2020-04-14 16:41:10 -05:00
}
return list ;
}
}
html ( src ) {
const cap = this . rules . block . html . exec ( src ) ;
if ( cap ) {
2021-08-09 23:41:45 -04:00
const token = {
2023-07-29 08:31:34 +02:00
type : "html" ,
2023-05-02 04:35:05 +00:00
block : true ,
2020-04-14 16:41:10 -05:00
raw : cap [ 0 ] ,
2023-07-29 08:31:34 +02:00
pre : ! this . options . sanitizer && ( cap [ 1 ] === "pre" || cap [ 1 ] === "script" || cap [ 1 ] === "style" ) ,
2021-08-09 23:41:45 -04:00
text : cap [ 0 ]
2020-04-14 16:41:10 -05:00
} ;
2021-08-09 23:41:45 -04:00
if ( this . options . sanitize ) {
2022-08-30 14:37:45 +00:00
const text = this . options . sanitizer ? this . options . sanitizer ( cap [ 0 ] ) : escape ( cap [ 0 ] ) ;
2023-07-29 08:31:34 +02:00
const paragraph = token ;
paragraph . type = "paragraph" ;
paragraph . text = text ;
paragraph . tokens = this . lexer . inline ( text ) ;
2021-08-09 23:41:45 -04:00
}
return token ;
2020-04-14 16:41:10 -05:00
}
}
def ( src ) {
const cap = this . rules . block . def . exec ( src ) ;
if ( cap ) {
2023-07-29 08:31:34 +02:00
const tag = cap [ 1 ] . toLowerCase ( ) . replace ( /\s+/g , " " ) ;
const href = cap [ 2 ] ? cap [ 2 ] . replace ( /^<(.*)>$/ , "$1" ) . replace ( this . rules . inline . _escapes , "$1" ) : "" ;
const title = cap [ 3 ] ? cap [ 3 ] . substring ( 1 , cap [ 3 ] . length - 1 ) . replace ( this . rules . inline . _escapes , "$1" ) : cap [ 3 ] ;
2020-04-14 16:41:10 -05:00
return {
2023-07-29 08:31:34 +02:00
type : "def" ,
2020-04-14 16:41:10 -05:00
tag ,
raw : cap [ 0 ] ,
2022-11-20 16:07:47 +00:00
href ,
title
2020-04-14 16:41:10 -05:00
} ;
}
}
table ( src ) {
const cap = this . rules . block . table . exec ( src ) ;
if ( cap ) {
const item = {
2023-07-29 08:31:34 +02:00
type : "table" ,
// splitCells expects a number as second argument
// @ts-expect-error
header : splitCells ( cap [ 1 ] ) . map ( ( c ) => {
return { text : c } ;
} ) ,
align : cap [ 2 ] . replace ( /^ *|\| *$/g , "" ) . split ( / *\| */ ) ,
rows : cap [ 3 ] && cap [ 3 ] . trim ( ) ? cap [ 3 ] . replace ( /\n[ \t]*$/ , "" ) . split ( "\n" ) : [ ]
2020-04-14 16:41:10 -05:00
} ;
if ( item . header . length === item . align . length ) {
item . raw = cap [ 0 ] ;
let l = item . align . length ;
2021-08-09 23:41:45 -04:00
let i , j , k , row ;
2020-04-14 16:41:10 -05:00
for ( i = 0 ; i < l ; i ++ ) {
if ( /^ *-+: *$/ . test ( item . align [ i ] ) ) {
2023-07-29 08:31:34 +02:00
item . align [ i ] = "right" ;
2020-04-14 16:41:10 -05:00
} else if ( /^ *:-+: *$/ . test ( item . align [ i ] ) ) {
2023-07-29 08:31:34 +02:00
item . align [ i ] = "center" ;
2020-04-14 16:41:10 -05:00
} else if ( /^ *:-+ *$/ . test ( item . align [ i ] ) ) {
2023-07-29 08:31:34 +02:00
item . align [ i ] = "left" ;
2020-04-14 16:41:10 -05:00
} else {
item . align [ i ] = null ;
}
}
2021-08-16 03:09:18 +00:00
l = item . rows . length ;
2020-04-14 16:41:10 -05:00
for ( i = 0 ; i < l ; i ++ ) {
2023-07-29 08:31:34 +02:00
item . rows [ i ] = splitCells ( item . rows [ i ] , item . header . length ) . map ( ( c ) => {
return { text : c } ;
} ) ;
2021-08-09 23:41:45 -04:00
}
l = item . header . length ;
for ( j = 0 ; j < l ; j ++ ) {
2022-08-30 14:37:45 +00:00
item . header [ j ] . tokens = this . lexer . inline ( item . header [ j ] . text ) ;
2021-08-09 23:41:45 -04:00
}
2021-08-16 03:09:18 +00:00
l = item . rows . length ;
2021-08-09 23:41:45 -04:00
for ( j = 0 ; j < l ; j ++ ) {
2021-08-16 03:09:18 +00:00
row = item . rows [ j ] ;
2021-08-09 23:41:45 -04:00
for ( k = 0 ; k < row . length ; k ++ ) {
2022-08-30 14:37:45 +00:00
row [ k ] . tokens = this . lexer . inline ( row [ k ] . text ) ;
2021-08-09 23:41:45 -04:00
}
2020-04-14 16:41:10 -05:00
}
return item ;
}
}
}
lheading ( src ) {
const cap = this . rules . block . lheading . exec ( src ) ;
if ( cap ) {
2022-08-30 14:37:45 +00:00
return {
2023-07-29 08:31:34 +02:00
type : "heading" ,
2020-04-14 16:41:10 -05:00
raw : cap [ 0 ] ,
2023-07-29 08:31:34 +02:00
depth : cap [ 2 ] . charAt ( 0 ) === "=" ? 1 : 2 ,
2021-08-09 23:41:45 -04:00
text : cap [ 1 ] ,
2022-08-30 14:37:45 +00:00
tokens : this . lexer . inline ( cap [ 1 ] )
2020-04-14 16:41:10 -05:00
} ;
}
2020-04-08 13:06:43 -05:00
}
2020-04-14 16:41:10 -05:00
paragraph ( src ) {
const cap = this . rules . block . paragraph . exec ( src ) ;
if ( cap ) {
2023-07-29 08:31:34 +02:00
const text = cap [ 1 ] . charAt ( cap [ 1 ] . length - 1 ) === "\n" ? cap [ 1 ] . slice ( 0 , - 1 ) : cap [ 1 ] ;
2022-08-30 14:37:45 +00:00
return {
2023-07-29 08:31:34 +02:00
type : "paragraph" ,
2020-04-14 16:41:10 -05:00
raw : cap [ 0 ] ,
2022-08-30 14:37:45 +00:00
text ,
tokens : this . lexer . inline ( text )
2020-04-14 16:41:10 -05:00
} ;
}
2020-04-08 13:06:43 -05:00
}
2021-02-07 17:22:47 -05:00
text ( src ) {
2020-04-14 16:41:10 -05:00
const cap = this . rules . block . text . exec ( src ) ;
if ( cap ) {
2022-08-30 14:37:45 +00:00
return {
2023-07-29 08:31:34 +02:00
type : "text" ,
2020-04-14 16:41:10 -05:00
raw : cap [ 0 ] ,
2021-08-09 23:41:45 -04:00
text : cap [ 0 ] ,
2022-08-30 14:37:45 +00:00
tokens : this . lexer . inline ( cap [ 0 ] )
2020-04-14 16:41:10 -05:00
} ;
2020-04-08 13:06:43 -05:00
}
2020-04-06 23:25:33 -05:00
}
2020-04-14 16:41:10 -05:00
escape ( src ) {
const cap = this . rules . inline . escape . exec ( src ) ;
if ( cap ) {
return {
2023-07-29 08:31:34 +02:00
type : "escape" ,
2020-04-14 16:41:10 -05:00
raw : cap [ 0 ] ,
2021-11-02 07:32:17 -07:00
text : escape ( cap [ 1 ] )
2020-04-14 16:41:10 -05:00
} ;
}
2020-04-08 13:06:43 -05:00
}
2021-08-09 23:41:45 -04:00
tag ( src ) {
2020-04-14 16:41:10 -05:00
const cap = this . rules . inline . tag . exec ( src ) ;
2020-04-06 23:25:33 -05:00
if ( cap ) {
2021-08-09 23:41:45 -04:00
if ( ! this . lexer . state . inLink && /^<a /i . test ( cap [ 0 ] ) ) {
this . lexer . state . inLink = true ;
} else if ( this . lexer . state . inLink && /^<\/a>/i . test ( cap [ 0 ] ) ) {
this . lexer . state . inLink = false ;
2020-04-06 23:25:33 -05:00
}
2021-08-09 23:41:45 -04:00
if ( ! this . lexer . state . inRawBlock && /^<(pre|code|kbd|script)(\s|>)/i . test ( cap [ 0 ] ) ) {
this . lexer . state . inRawBlock = true ;
} else if ( this . lexer . state . inRawBlock && /^<\/(pre|code|kbd|script)(\s|>)/i . test ( cap [ 0 ] ) ) {
this . lexer . state . inRawBlock = false ;
2020-04-14 16:41:10 -05:00
}
return {
2023-07-29 08:31:34 +02:00
type : this . options . sanitize ? "text" : "html" ,
2020-04-14 16:41:10 -05:00
raw : cap [ 0 ] ,
2021-08-09 23:41:45 -04:00
inLink : this . lexer . state . inLink ,
inRawBlock : this . lexer . state . inRawBlock ,
2023-05-02 04:35:05 +00:00
block : false ,
2023-07-29 08:31:34 +02:00
text : this . options . sanitize ? this . options . sanitizer ? this . options . sanitizer ( cap [ 0 ] ) : escape ( cap [ 0 ] ) : cap [ 0 ]
2020-04-14 16:41:10 -05:00
} ;
2020-04-06 23:25:33 -05:00
}
}
2020-04-14 16:41:10 -05:00
link ( src ) {
const cap = this . rules . inline . link . exec ( src ) ;
2020-04-06 23:25:33 -05:00
if ( cap ) {
2020-12-10 16:30:08 +00:00
const trimmedUrl = cap [ 2 ] . trim ( ) ;
2020-12-15 14:14:22 -06:00
if ( ! this . options . pedantic && /^</ . test ( trimmedUrl ) ) {
2023-07-29 08:31:34 +02:00
if ( ! />$/ . test ( trimmedUrl ) ) {
2020-12-10 16:30:08 +00:00
return ;
}
2023-07-29 08:31:34 +02:00
const rtrimSlash = rtrim ( trimmedUrl . slice ( 0 , - 1 ) , "\\" ) ;
2020-12-10 16:30:08 +00:00
if ( ( trimmedUrl . length - rtrimSlash . length ) % 2 === 0 ) {
return ;
}
} else {
2023-07-29 08:31:34 +02:00
const lastParenIndex = findClosingBracket ( cap [ 2 ] , "()" ) ;
2020-12-10 16:30:08 +00:00
if ( lastParenIndex > - 1 ) {
2023-07-29 08:31:34 +02:00
const start = cap [ 0 ] . indexOf ( "!" ) === 0 ? 5 : 4 ;
2020-12-10 16:30:08 +00:00
const linkLen = start + cap [ 1 ] . length + lastParenIndex ;
cap [ 2 ] = cap [ 2 ] . substring ( 0 , lastParenIndex ) ;
cap [ 0 ] = cap [ 0 ] . substring ( 0 , linkLen ) . trim ( ) ;
2023-07-29 08:31:34 +02:00
cap [ 3 ] = "" ;
2020-12-10 16:30:08 +00:00
}
2020-04-14 16:41:10 -05:00
}
let href = cap [ 2 ] ;
2023-07-29 08:31:34 +02:00
let title = "" ;
2020-04-14 16:41:10 -05:00
if ( this . options . pedantic ) {
const link = /^([^'"]*[^\s])\s+(['"])(.*)\2/ . exec ( href ) ;
if ( link ) {
href = link [ 1 ] ;
title = link [ 3 ] ;
}
2020-04-06 23:25:33 -05:00
} else {
2023-07-29 08:31:34 +02:00
title = cap [ 3 ] ? cap [ 3 ] . slice ( 1 , - 1 ) : "" ;
2020-04-14 16:41:10 -05:00
}
2020-12-10 16:30:08 +00:00
href = href . trim ( ) ;
2020-12-15 14:14:22 -06:00
if ( /^</ . test ( href ) ) {
2023-07-29 08:31:34 +02:00
if ( this . options . pedantic && ! />$/ . test ( trimmedUrl ) ) {
2020-12-10 16:30:08 +00:00
href = href . slice ( 1 ) ;
} else {
href = href . slice ( 1 , - 1 ) ;
}
}
return outputLink ( cap , {
2023-07-29 08:31:34 +02:00
href : href ? href . replace ( this . rules . inline . _escapes , "$1" ) : href ,
title : title ? title . replace ( this . rules . inline . _escapes , "$1" ) : title
2021-08-09 23:41:45 -04:00
} , cap [ 0 ] , this . lexer ) ;
2020-04-14 16:41:10 -05:00
}
}
reflink ( src , links ) {
let cap ;
2023-07-29 08:31:34 +02:00
if ( ( cap = this . rules . inline . reflink . exec ( src ) ) || ( cap = this . rules . inline . nolink . exec ( src ) ) ) {
let link = ( cap [ 2 ] || cap [ 1 ] ) . replace ( /\s+/g , " " ) ;
2020-04-14 16:41:10 -05:00
link = links [ link . toLowerCase ( ) ] ;
2022-11-20 16:07:47 +00:00
if ( ! link ) {
2020-04-14 16:41:10 -05:00
const text = cap [ 0 ] . charAt ( 0 ) ;
2020-04-06 23:25:33 -05:00
return {
2023-07-29 08:31:34 +02:00
type : "text" ,
2020-04-14 16:41:10 -05:00
raw : text ,
text
2020-04-06 23:25:33 -05:00
} ;
}
2021-08-09 23:41:45 -04:00
return outputLink ( cap , link , cap [ 0 ] , this . lexer ) ;
2020-04-06 23:25:33 -05:00
}
}
2023-07-29 08:31:34 +02:00
emStrong ( src , maskedSrc , prevChar = "" ) {
2021-02-07 17:25:01 -05:00
let match = this . rules . inline . emStrong . lDelim . exec ( src ) ;
2023-07-29 08:31:34 +02:00
if ( ! match )
return ;
if ( match [ 3 ] && prevChar . match ( /[\p{L}\p{N}]/u ) )
return ;
const nextChar = match [ 1 ] || match [ 2 ] || "" ;
2023-06-10 03:13:30 +00:00
if ( ! nextChar || ! prevChar || this . rules . inline . punctuation . exec ( prevChar ) ) {
2021-02-07 17:25:01 -05:00
const lLength = match [ 0 ] . length - 1 ;
let rDelim , rLength , delimTotal = lLength , midDelimTotal = 0 ;
2023-07-29 08:31:34 +02:00
const endReg = match [ 0 ] [ 0 ] === "*" ? this . rules . inline . emStrong . rDelimAst : this . rules . inline . emStrong . rDelimUnd ;
2020-07-13 13:35:58 +00:00
endReg . lastIndex = 0 ;
2021-06-01 19:27:49 +00:00
maskedSrc = maskedSrc . slice ( - 1 * src . length + lLength ) ;
2020-07-13 13:35:58 +00:00
while ( ( match = endReg . exec ( maskedSrc ) ) != null ) {
2021-02-07 17:25:01 -05:00
rDelim = match [ 1 ] || match [ 2 ] || match [ 3 ] || match [ 4 ] || match [ 5 ] || match [ 6 ] ;
2023-07-29 08:31:34 +02:00
if ( ! rDelim )
continue ;
2021-02-07 17:25:01 -05:00
rLength = rDelim . length ;
2023-07-29 08:31:34 +02:00
if ( match [ 3 ] || match [ 4 ] ) {
2021-02-07 17:25:01 -05:00
delimTotal += rLength ;
continue ;
2023-07-29 08:31:34 +02:00
} else if ( match [ 5 ] || match [ 6 ] ) {
2021-02-07 17:25:01 -05:00
if ( lLength % 3 && ! ( ( lLength + rLength ) % 3 ) ) {
midDelimTotal += rLength ;
2023-07-29 08:31:34 +02:00
continue ;
2021-02-07 17:25:01 -05:00
}
2020-07-13 13:35:58 +00:00
}
2021-02-07 17:25:01 -05:00
delimTotal -= rLength ;
2023-07-29 08:31:34 +02:00
if ( delimTotal > 0 )
continue ;
2021-06-01 19:27:49 +00:00
rLength = Math . min ( rLength , rLength + delimTotal + midDelimTotal ) ;
2023-06-07 04:22:11 +00:00
const raw = src . slice ( 0 , lLength + match . index + rLength + 1 ) ;
2021-02-07 17:25:01 -05:00
if ( Math . min ( lLength , rLength ) % 2 ) {
2023-07-29 08:31:34 +02:00
const text2 = raw . slice ( 1 , - 1 ) ;
2020-07-13 13:35:58 +00:00
return {
2023-07-29 08:31:34 +02:00
type : "em" ,
2022-11-02 02:05:13 +00:00
raw ,
2023-07-29 08:31:34 +02:00
text : text2 ,
tokens : this . lexer . inlineTokens ( text2 )
2021-02-07 17:25:01 -05:00
} ;
}
2022-11-02 02:05:13 +00:00
const text = raw . slice ( 2 , - 2 ) ;
2021-06-01 19:27:49 +00:00
return {
2023-07-29 08:31:34 +02:00
type : "strong" ,
2022-11-02 02:05:13 +00:00
raw ,
2021-08-09 23:41:45 -04:00
text ,
2022-08-30 14:37:45 +00:00
tokens : this . lexer . inlineTokens ( text )
2021-06-01 19:27:49 +00:00
} ;
2020-07-13 13:35:58 +00:00
}
2020-04-06 23:25:33 -05:00
}
}
2020-04-14 16:41:10 -05:00
codespan ( src ) {
const cap = this . rules . inline . code . exec ( src ) ;
2020-04-06 23:25:33 -05:00
if ( cap ) {
2023-07-29 08:31:34 +02:00
let text = cap [ 2 ] . replace ( /\n/g , " " ) ;
2020-05-03 19:20:54 +00:00
const hasNonSpaceChars = /[^ ]/ . test ( text ) ;
2020-12-15 14:14:22 -06:00
const hasSpaceCharsOnBothEnds = /^ / . test ( text ) && / $/ . test ( text ) ;
2020-05-03 19:20:54 +00:00
if ( hasNonSpaceChars && hasSpaceCharsOnBothEnds ) {
text = text . substring ( 1 , text . length - 1 ) ;
}
2021-11-02 07:32:17 -07:00
text = escape ( text , true ) ;
2020-04-14 16:41:10 -05:00
return {
2023-07-29 08:31:34 +02:00
type : "codespan" ,
2020-04-14 16:41:10 -05:00
raw : cap [ 0 ] ,
2020-05-03 19:20:54 +00:00
text
2020-04-06 23:25:33 -05:00
} ;
}
}
2020-04-14 16:41:10 -05:00
br ( src ) {
const cap = this . rules . inline . br . exec ( src ) ;
2020-04-06 23:25:33 -05:00
if ( cap ) {
return {
2023-07-29 08:31:34 +02:00
type : "br" ,
2020-04-06 23:25:33 -05:00
raw : cap [ 0 ]
} ;
}
}
2020-04-14 16:41:10 -05:00
del ( src ) {
const cap = this . rules . inline . del . exec ( src ) ;
2020-04-06 23:25:33 -05:00
if ( cap ) {
return {
2023-07-29 08:31:34 +02:00
type : "del" ,
2020-04-06 23:25:33 -05:00
raw : cap [ 0 ] ,
2021-08-09 23:41:45 -04:00
text : cap [ 2 ] ,
2022-08-30 14:37:45 +00:00
tokens : this . lexer . inlineTokens ( cap [ 2 ] )
2020-04-06 23:25:33 -05:00
} ;
}
}
2023-07-29 08:31:34 +02:00
autolink ( src , mangle2 ) {
2020-04-14 16:41:10 -05:00
const cap = this . rules . inline . autolink . exec ( src ) ;
2020-04-06 23:25:33 -05:00
if ( cap ) {
2020-04-14 16:41:10 -05:00
let text , href ;
2023-07-29 08:31:34 +02:00
if ( cap [ 2 ] === "@" ) {
text = escape ( this . options . mangle ? mangle2 ( cap [ 1 ] ) : cap [ 1 ] ) ;
href = "mailto:" + text ;
2020-04-14 16:41:10 -05:00
} else {
2021-11-02 07:32:17 -07:00
text = escape ( cap [ 1 ] ) ;
2020-04-14 16:41:10 -05:00
href = text ;
2020-04-06 23:25:33 -05:00
}
2020-04-14 16:41:10 -05:00
return {
2023-07-29 08:31:34 +02:00
type : "link" ,
2020-04-14 16:41:10 -05:00
raw : cap [ 0 ] ,
text ,
href ,
tokens : [
{
2023-07-29 08:31:34 +02:00
type : "text" ,
2020-04-14 16:41:10 -05:00
raw : text ,
text
}
]
} ;
2020-04-06 23:25:33 -05:00
}
}
2023-07-29 08:31:34 +02:00
url ( src , mangle2 ) {
2020-04-14 16:41:10 -05:00
let cap ;
if ( cap = this . rules . inline . url . exec ( src ) ) {
let text , href ;
2023-07-29 08:31:34 +02:00
if ( cap [ 2 ] === "@" ) {
text = escape ( this . options . mangle ? mangle2 ( cap [ 0 ] ) : cap [ 0 ] ) ;
href = "mailto:" + text ;
2020-04-14 16:41:10 -05:00
} else {
let prevCapZero ;
do {
prevCapZero = cap [ 0 ] ;
cap [ 0 ] = this . rules . inline . _backpedal . exec ( cap [ 0 ] ) [ 0 ] ;
} while ( prevCapZero !== cap [ 0 ] ) ;
2021-11-02 07:32:17 -07:00
text = escape ( cap [ 0 ] ) ;
2023-07-29 08:31:34 +02:00
if ( cap [ 1 ] === "www." ) {
href = "http://" + cap [ 0 ] ;
2020-04-14 16:41:10 -05:00
} else {
2022-12-07 07:46:08 +00:00
href = cap [ 0 ] ;
2020-04-14 16:41:10 -05:00
}
}
2020-04-06 23:25:33 -05:00
return {
2023-07-29 08:31:34 +02:00
type : "link" ,
2020-04-06 23:25:33 -05:00
raw : cap [ 0 ] ,
2020-04-14 16:41:10 -05:00
text ,
href ,
tokens : [
{
2023-07-29 08:31:34 +02:00
type : "text" ,
2020-04-14 16:41:10 -05:00
raw : text ,
text
}
]
2020-04-06 23:25:33 -05:00
} ;
}
}
2023-07-29 08:31:34 +02:00
inlineText ( src , smartypants2 ) {
2020-04-14 16:41:10 -05:00
const cap = this . rules . inline . text . exec ( src ) ;
2020-04-06 23:25:33 -05:00
if ( cap ) {
2020-04-14 16:41:10 -05:00
let text ;
2021-08-09 23:41:45 -04:00
if ( this . lexer . state . inRawBlock ) {
2023-07-29 08:31:34 +02:00
text = this . options . sanitize ? this . options . sanitizer ? this . options . sanitizer ( cap [ 0 ] ) : escape ( cap [ 0 ] ) : cap [ 0 ] ;
2020-04-14 16:41:10 -05:00
} else {
2023-07-29 08:31:34 +02:00
text = escape ( this . options . smartypants ? smartypants2 ( cap [ 0 ] ) : cap [ 0 ] ) ;
2020-04-14 16:41:10 -05:00
}
2020-04-06 23:25:33 -05:00
return {
2023-07-29 08:31:34 +02:00
type : "text" ,
2020-04-08 13:06:43 -05:00
raw : cap [ 0 ] ,
2020-04-14 16:41:10 -05:00
text
2020-04-06 23:25:33 -05:00
} ;
}
}
2023-07-29 08:31:34 +02:00
} ;
2020-04-06 23:25:33 -05:00
2023-07-29 08:31:34 +02:00
// src/rules.ts
var block = {
2021-01-26 14:21:28 +00:00
newline : /^(?: *(?:\n|$))+/ ,
code : /^( {4}[^\n]+(?:\n(?: *(?:\n|$))*)?)+/ ,
2023-03-21 19:50:28 -05:00
fences : /^ {0,3}(`{3,}(?=[^`\n]*(?:\n|$))|~{3,})([^\n]*)(?:\n|$)(?:|([\s\S]*?)(?:\n|$))(?: {0,3}\1[~`]* *(?=\n|$)|$)/ ,
2022-04-11 00:38:29 +00:00
hr : /^ {0,3}((?:-[\t ]*){3,}|(?:_[ \t]*){3,}|(?:\*[ \t]*){3,})(?:\n+|$)/ ,
2020-12-10 10:28:58 -06:00
heading : /^ {0,3}(#{1,6})(?=\s|$)(.*)(?:\n+|$)/ ,
2020-04-14 16:41:10 -05:00
blockquote : /^( {0,3}> ?(paragraph|[^\n]*)(?:\n|$))+/ ,
2022-04-11 00:38:29 +00:00
list : /^( {0,3}bull)([ \t][^\n]+?)?(?:\n|$)/ ,
2023-07-29 08:31:34 +02:00
html : "^ {0,3}(?:<(script|pre|style|textarea)[\\s>][\\s\\S]*?(?:</\\1>[^\\n]*\\n+|$)|comment[^\\n]*(\\n+|$)|<\\?[\\s\\S]*?(?:\\?>\\n*|$)|<![A-Z][\\s\\S]*?(?:>\\n*|$)|<!\\[CDATA\\[[\\s\\S]*?(?:\\]\\]>\\n*|$)|</?(tag)(?: +|\\n|/?>)[\\s\\S]*?(?:(?:\\n *)+\\n|$)|<(?!script|pre|style|textarea)([a-z][\\w-]*)(?:attribute)*? */?>(?=[ \\t]*(?:\\n|$))[\\s\\S]*?(?:(?:\\n *)+\\n|$)|</(?!script|pre|style|textarea)[a-z][\\w-]*\\s*>(?=[ \\t]*(?:\\n|$))[\\s\\S]*?(?:(?:\\n *)+\\n|$))" ,
2022-11-20 16:07:47 +00:00
def : /^ {0,3}\[(label)\]: *(?:\n *)?([^<\s][^\s]*|<.*?>)(?:(?: +(?:\n *)?| *\n *)(title))? *(?:\n+|$)/ ,
2021-08-16 03:09:18 +00:00
table : noopTest ,
2023-05-30 22:25:56 +00:00
lheading : /^((?:(?!^bull ).|\n(?!\n|bull ))+?)\n {0,3}(=+|-+) *(?:\n+|$)/ ,
2020-04-14 16:41:10 -05:00
// regex template, placeholders will be replaced according to different paragraph
// interruption rules of commonmark and the original markdown spec:
2021-11-25 00:12:00 +00:00
_paragraph : /^([^\n]+(?:\n(?!hr|heading|lheading|blockquote|fences|list|html|table| +\n)[^\n]+)*)/ ,
2020-04-14 16:41:10 -05:00
text : /^[^\n]+/
} ;
2022-01-13 02:03:16 +00:00
block . _label = /(?!\s*\])(?:\\.|[^\[\]\\])+/ ;
2021-11-02 07:32:17 -07:00
block . _title = /(?:"(?:\\"?|[^"\\])*"|'[^'\n]*(?:\n[^'\n]+)*\n?'|\([^()]*\))/ ;
2023-07-29 08:31:34 +02:00
block . def = edit ( block . def ) . replace ( "label" , block . _label ) . replace ( "title" , block . _title ) . getRegex ( ) ;
2021-11-02 07:32:17 -07:00
block . bullet = /(?:[*+-]|\d{1,9}[.)])/ ;
2023-07-29 08:31:34 +02:00
block . listItemStart = edit ( /^( *)(bull) */ ) . replace ( "bull" , block . bullet ) . getRegex ( ) ;
block . list = edit ( block . list ) . replace ( /bull/g , block . bullet ) . replace ( "hr" , "\\n+(?=\\1?(?:(?:- *){3,}|(?:_ *){3,}|(?:\\* *){3,})(?:\\n+|$))" ) . replace ( "def" , "\\n+(?=" + block . def . source + ")" ) . getRegex ( ) ;
block . _tag = "address|article|aside|base|basefont|blockquote|body|caption|center|col|colgroup|dd|details|dialog|dir|div|dl|dt|fieldset|figcaption|figure|footer|form|frame|frameset|h[1-6]|head|header|hr|html|iframe|legend|li|link|main|menu|menuitem|meta|nav|noframes|ol|optgroup|option|p|param|section|source|summary|table|tbody|td|tfoot|th|thead|title|tr|track|ul" ;
2021-11-02 07:32:17 -07:00
block . _comment = /<!--(?!-?>)[\s\S]*?(?:-->|$)/ ;
2023-07-29 08:31:34 +02:00
block . html = edit ( block . html , "i" ) . replace ( "comment" , block . _comment ) . replace ( "tag" , block . _tag ) . replace ( "attribute" , / +[a-zA-Z:_][\w.:-]*(?: *= *"[^"\n]*"| *= *'[^'\n]*'| *= *[^\s"'=<>`]+)?/ ) . getRegex ( ) ;
block . lheading = edit ( block . lheading ) . replace ( /bull/g , block . bullet ) . getRegex ( ) ;
block . paragraph = edit ( block . _paragraph ) . replace ( "hr" , block . hr ) . replace ( "heading" , " {0,3}#{1,6} " ) . replace ( "|lheading" , "" ) . replace ( "|table" , "" ) . replace ( "blockquote" , " {0,3}>" ) . replace ( "fences" , " {0,3}(?:`{3,}(?=[^`\\n]*\\n)|~{3,})[^\\n]*\\n" ) . replace ( "list" , " {0,3}(?:[*+-]|1[.)]) " ) . replace ( "html" , "</?(?:tag)(?: +|\\n|/?>)|<(?:script|pre|style|textarea|!--)" ) . replace ( "tag" , block . _tag ) . getRegex ( ) ;
block . blockquote = edit ( block . blockquote ) . replace ( "paragraph" , block . paragraph ) . getRegex ( ) ;
2023-03-21 19:50:28 -05:00
block . normal = { ... block } ;
block . gfm = {
... block . normal ,
2023-07-29 08:31:34 +02:00
table : "^ *([^\\n ].*\\|.*)\\n {0,3}(?:\\| *)?(:?-+:? *(?:\\| *:?-+:? *)*)(?:\\| *)?(?:\\n((?:(?! *\\n|hr|heading|blockquote|code|fences|list|html).*(?:\\n|$))*)\\n*|$)"
// Cells
2023-03-21 19:50:28 -05:00
} ;
2023-07-29 08:31:34 +02:00
block . gfm . table = edit ( block . gfm . table ) . replace ( "hr" , block . hr ) . replace ( "heading" , " {0,3}#{1,6} " ) . replace ( "blockquote" , " {0,3}>" ) . replace ( "code" , " {4}[^\\n]" ) . replace ( "fences" , " {0,3}(?:`{3,}(?=[^`\\n]*\\n)|~{3,})[^\\n]*\\n" ) . replace ( "list" , " {0,3}(?:[*+-]|1[.)]) " ) . replace ( "html" , "</?(?:tag)(?: +|\\n|/?>)|<(?:script|pre|style|textarea|!--)" ) . replace ( "tag" , block . _tag ) . getRegex ( ) ;
block . gfm . paragraph = edit ( block . _paragraph ) . replace ( "hr" , block . hr ) . replace ( "heading" , " {0,3}#{1,6} " ) . replace ( "|lheading" , "" ) . replace ( "table" , block . gfm . table ) . replace ( "blockquote" , " {0,3}>" ) . replace ( "fences" , " {0,3}(?:`{3,}(?=[^`\\n]*\\n)|~{3,})[^\\n]*\\n" ) . replace ( "list" , " {0,3}(?:[*+-]|1[.)]) " ) . replace ( "html" , "</?(?:tag)(?: +|\\n|/?>)|<(?:script|pre|style|textarea|!--)" ) . replace ( "tag" , block . _tag ) . getRegex ( ) ;
2023-03-21 19:50:28 -05:00
block . pedantic = {
... block . normal ,
2021-08-16 03:09:18 +00:00
html : edit (
2023-07-29 08:31:34 +02:00
` ^ *(?:comment *(?: \\ n| \\ s* $ )|<(tag)[ \\ s \\ S]+?</ \\ 1> *(?: \\ n{2,}| \\ s* $ )|<tag(?:"[^"]*"|'[^']*'| \\ s[^'"/> \\ s]*)*?/?> *(?: \\ n{2,}| \\ s* $ )) `
) . replace ( "comment" , block . _comment ) . replace ( /tag/g , "(?!(?:a|em|strong|small|s|cite|q|dfn|abbr|data|time|code|var|samp|kbd|sub|sup|i|b|u|mark|ruby|rt|rp|bdi|bdo|span|br|wbr|ins|del|img)\\b)\\w+(?!:|[^\\w\\s@]*@)\\b" ) . getRegex ( ) ,
2020-04-14 16:41:10 -05:00
def : /^ *\[([^\]]+)\]: *<?([^\s>]+)>?(?: +(["(][^\n]+[")]))? *(?:\n+|$)/ ,
2020-12-10 10:28:58 -06:00
heading : /^(#{1,6})(.*)(?:\n+|$)/ ,
2023-07-29 08:31:34 +02:00
fences : noopTest ,
// fences not supported
2022-11-20 16:07:47 +00:00
lheading : /^(.+?)\n {0,3}(=+|-+) *(?:\n+|$)/ ,
2023-07-29 08:31:34 +02:00
paragraph : edit ( block . normal . _paragraph ) . replace ( "hr" , block . hr ) . replace ( "heading" , " *#{1,6} *[^\n]" ) . replace ( "lheading" , block . lheading ) . replace ( "blockquote" , " {0,3}>" ) . replace ( "|fences" , "" ) . replace ( "|list" , "" ) . replace ( "|html" , "" ) . getRegex ( )
2023-03-21 19:50:28 -05:00
} ;
2023-07-29 08:31:34 +02:00
var inline = {
2020-04-14 16:41:10 -05:00
escape : /^\\([!"#$%&'()*+,\-./:;<=>?@\[\]\\^_`{|}~])/ ,
autolink : /^<(scheme:[^\s\x00-\x1f<>]*|email)>/ ,
2021-08-16 03:09:18 +00:00
url : noopTest ,
2023-07-29 08:31:34 +02:00
tag : "^comment|^</[a-zA-Z][\\w:-]*\\s*>|^<[a-zA-Z][\\w-]*(?:attribute)*?\\s*/?>|^<\\?[\\s\\S]*?\\?>|^<![a-zA-Z]+\\s[\\s\\S]*?>|^<!\\[CDATA\\[[\\s\\S]*?\\]\\]>" ,
// CDATA section
2020-04-14 16:41:10 -05:00
link : /^!?\[(label)\]\(\s*(href)(?:\s+(title))?\s*\)/ ,
2022-01-13 02:03:16 +00:00
reflink : /^!?\[(label)\]\[(ref)\]/ ,
nolink : /^!?\[(ref)\](?:\[\])?/ ,
2023-07-29 08:31:34 +02:00
reflinkSearch : "reflink|nolink(?!\\()" ,
2021-02-07 17:25:01 -05:00
emStrong : {
2023-06-10 03:13:30 +00:00
lDelim : /^(?:\*+(?:((?!\*)[punct])|[^\s*]))|^_+(?:((?!_)[punct])|([^\s_]))/ ,
// (1) and (2) can only be a Right Delimiter. (3) and (4) can only be Left. (5) and (6) can be either Left or Right.
// | Skip orphan inside strong | Consume to delim | (1) #*** | (2) a***#, a*** | (3) #***a, ***a | (4) ***# | (5) #***# | (6) a***a
rDelimAst : /^[^_*]*?__[^_*]*?\*[^_*]*?(?=__)|[^*]+(?=[^*])|(?!\*)[punct](\*+)(?=[\s]|$)|[^punct\s](\*+)(?!\*)(?=[punct\s]|$)|(?!\*)[punct\s](\*+)(?=[^punct\s])|[\s](\*+)(?!\*)(?=[punct])|(?!\*)[punct](\*+)(?!\*)(?=[punct])|[^punct\s](\*+)(?=[^punct\s])/ ,
2023-07-29 08:31:34 +02:00
rDelimUnd : /^[^_*]*?\*\*[^_*]*?_[^_*]*?(?=\*\*)|[^_]+(?=[^_])|(?!_)[punct](_+)(?=[\s]|$)|[^punct\s](_+)(?!_)(?=[punct\s]|$)|(?!_)[punct\s](_+)(?=[^punct\s])|[\s](_+)(?!_)(?=[punct])|(?!_)[punct](_+)(?!_)(?=[punct])/
// ^- Not allowed for _
2020-07-13 13:35:58 +00:00
} ,
2020-04-14 16:41:10 -05:00
code : /^(`+)([^`]|[^`][\s\S]*?[^`])\1(?!`)/ ,
br : /^( {2,}|\\)\n(?!\s*$)/ ,
2021-08-16 03:09:18 +00:00
del : noopTest ,
2021-02-07 17:25:01 -05:00
text : /^(`+|[^`])(?:(?= {2,}\n)|[\s\S]*?(?:(?=[\\<!\[`*_]|\b_|$)|[^ ](?= {2,}\n)))/ ,
2023-06-10 03:13:30 +00:00
punctuation : /^((?![*_])[\spunctuation])/
2020-04-14 16:41:10 -05:00
} ;
2023-07-29 08:31:34 +02:00
inline . _punctuation = "\\p{P}$+<=>`^|~" ;
inline . punctuation = edit ( inline . punctuation , "u" ) . replace ( /punctuation/g , inline . _punctuation ) . getRegex ( ) ;
2023-05-26 16:54:11 +00:00
inline . blockSkip = /\[[^[\]]*?\]\([^\(\)]*?\)|`[^`]*?`|<[^<>]*?>/g ;
2023-06-10 03:13:30 +00:00
inline . anyPunctuation = /\\[punct]/g ;
inline . _escapes = /\\([punct])/g ;
2023-07-29 08:31:34 +02:00
inline . _comment = edit ( block . _comment ) . replace ( "(?:-->|$)" , "-->" ) . getRegex ( ) ;
inline . emStrong . lDelim = edit ( inline . emStrong . lDelim , "u" ) . replace ( /punct/g , inline . _punctuation ) . getRegex ( ) ;
inline . emStrong . rDelimAst = edit ( inline . emStrong . rDelimAst , "gu" ) . replace ( /punct/g , inline . _punctuation ) . getRegex ( ) ;
inline . emStrong . rDelimUnd = edit ( inline . emStrong . rDelimUnd , "gu" ) . replace ( /punct/g , inline . _punctuation ) . getRegex ( ) ;
inline . anyPunctuation = edit ( inline . anyPunctuation , "gu" ) . replace ( /punct/g , inline . _punctuation ) . getRegex ( ) ;
inline . _escapes = edit ( inline . _escapes , "gu" ) . replace ( /punct/g , inline . _punctuation ) . getRegex ( ) ;
2021-11-02 07:32:17 -07:00
inline . _scheme = /[a-zA-Z][a-zA-Z0-9+.-]{1,31}/ ;
inline . _email = /[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+(@)[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)+(?![-_])/ ;
2023-07-29 08:31:34 +02:00
inline . autolink = edit ( inline . autolink ) . replace ( "scheme" , inline . _scheme ) . replace ( "email" , inline . _email ) . getRegex ( ) ;
2021-11-02 07:32:17 -07:00
inline . _attribute = /\s+[a-zA-Z:_][\w.:-]*(?:\s*=\s*"[^"]*"|\s*=\s*'[^']*'|\s*=\s*[^\s"'=<>`]+)?/ ;
2023-07-29 08:31:34 +02:00
inline . tag = edit ( inline . tag ) . replace ( "comment" , inline . _comment ) . replace ( "attribute" , inline . _attribute ) . getRegex ( ) ;
2021-11-02 07:32:17 -07:00
inline . _label = /(?:\[(?:\\.|[^\[\]\\])*\]|\\.|`[^`]*`|[^\[\]\\`])*?/ ;
inline . _href = /<(?:\\.|[^\n<>\\])+>|[^\s\x00-\x1f]*/ ;
inline . _title = /"(?:\\"?|[^"\\])*"|'(?:\\'?|[^'\\])*'|\((?:\\\)?|[^)\\])*\)/ ;
2023-07-29 08:31:34 +02:00
inline . link = edit ( inline . link ) . replace ( "label" , inline . _label ) . replace ( "href" , inline . _href ) . replace ( "title" , inline . _title ) . getRegex ( ) ;
inline . reflink = edit ( inline . reflink ) . replace ( "label" , inline . _label ) . replace ( "ref" , block . _label ) . getRegex ( ) ;
inline . nolink = edit ( inline . nolink ) . replace ( "ref" , block . _label ) . getRegex ( ) ;
inline . reflinkSearch = edit ( inline . reflinkSearch , "g" ) . replace ( "reflink" , inline . reflink ) . replace ( "nolink" , inline . nolink ) . getRegex ( ) ;
2023-03-21 19:50:28 -05:00
inline . normal = { ... inline } ;
inline . pedantic = {
... inline . normal ,
2020-07-13 13:35:58 +00:00
strong : {
start : /^__|\*\*/ ,
middle : /^__(?=\S)([\s\S]*?\S)__(?!_)|^\*\*(?=\S)([\s\S]*?\S)\*\*(?!\*)/ ,
endAst : /\*\*(?!\*)/g ,
endUnd : /__(?!_)/g
} ,
em : {
start : /^_|\*/ ,
middle : /^()\*(?=\S)([\s\S]*?\S)\*(?!\*)|^_(?=\S)([\s\S]*?\S)_(?!_)/ ,
endAst : /\*(?!\*)/g ,
endUnd : /_(?!_)/g
} ,
2023-07-29 08:31:34 +02:00
link : edit ( /^!?\[(label)\]\((.*?)\)/ ) . replace ( "label" , inline . _label ) . getRegex ( ) ,
reflink : edit ( /^!?\[(label)\]\s*\[([^\]]*)\]/ ) . replace ( "label" , inline . _label ) . getRegex ( )
2023-03-21 19:50:28 -05:00
} ;
inline . gfm = {
... inline . normal ,
2023-07-29 08:31:34 +02:00
escape : edit ( inline . escape ) . replace ( "])" , "~|])" ) . getRegex ( ) ,
2020-04-14 16:41:10 -05:00
_extended _email : /[A-Za-z0-9._+-]+(@)[a-zA-Z0-9-_]+(?:\.[a-zA-Z0-9-_]*[a-zA-Z0-9])+(?![-_])/ ,
url : /^((?:ftp|https?):\/\/|www\.)(?:[a-zA-Z0-9\-]+\.?)+[^\s<]*|^email/ ,
2022-12-07 07:46:08 +00:00
_backpedal : /(?:[^?!.,:;*_'"~()&]+|\([^)]*\)|&(?![a-zA-Z0-9]+;$)|[?!.,:;*_'"~)]+(?!$))+/ ,
2020-11-15 02:04:54 +00:00
del : /^(~~?)(?=[^\s~])([\s\S]*?[^\s~])\1(?=[^~]|$)/ ,
2021-05-27 16:16:52 +00:00
text : /^([`~]+|[^`~])(?:(?= {2,}\n)|(?=[a-zA-Z0-9.!#$%&'*+\/=?_`{\|}~-]+@)|[\s\S]*?(?:(?=[\\<!\[`*~_]|\b_|https?:\/\/|ftp:\/\/|www\.|$)|[^ ](?= {2,}\n)|[^a-zA-Z0-9.!#$%&'*+\/=?_`{\|}~-](?=[a-zA-Z0-9.!#$%&'*+\/=?_`{\|}~-]+@)))/
2023-03-21 19:50:28 -05:00
} ;
2023-07-29 08:31:34 +02:00
inline . gfm . url = edit ( inline . gfm . url , "i" ) . replace ( "email" , inline . gfm . _extended _email ) . getRegex ( ) ;
2023-03-21 19:50:28 -05:00
inline . breaks = {
... inline . gfm ,
2023-07-29 08:31:34 +02:00
br : edit ( inline . br ) . replace ( "{2,}" , "*" ) . getRegex ( ) ,
text : edit ( inline . gfm . text ) . replace ( "\\b_" , "\\b_| {2,}\\n" ) . replace ( /\{2,\}/g , "*" ) . getRegex ( )
2023-03-21 19:50:28 -05:00
} ;
2020-04-14 16:41:10 -05:00
2023-07-29 08:31:34 +02:00
// src/Lexer.ts
2020-04-14 16:41:10 -05:00
function smartypants ( text ) {
2023-07-29 08:31:34 +02:00
return text . replace ( /---/g , "\u2014" ) . replace ( /--/g , "\u2013" ) . replace ( /(^|[-\u2014/(\[{"\s])'/g , "$1\u2018" ) . replace ( /'/g , "\u2019" ) . replace ( /(^|[-\u2014/(\[{\u2018\s])"/g , "$1\u201C" ) . replace ( /"/g , "\u201D" ) . replace ( /\.{3}/g , "\u2026" ) ;
2020-04-14 16:41:10 -05:00
}
function mangle ( text ) {
2023-07-29 08:31:34 +02:00
let out = "" , i , ch ;
2020-04-14 16:41:10 -05:00
const l = text . length ;
for ( i = 0 ; i < l ; i ++ ) {
ch = text . charCodeAt ( i ) ;
if ( Math . random ( ) > 0.5 ) {
2023-07-29 08:31:34 +02:00
ch = "x" + ch . toString ( 16 ) ;
2020-04-14 16:41:10 -05:00
}
2023-07-29 08:31:34 +02:00
out += "&#" + ch + ";" ;
2020-04-14 16:41:10 -05:00
}
return out ;
}
2023-07-29 08:31:34 +02:00
var _Lexer = class {
constructor ( options2 ) {
2019-11-07 12:49:10 -06:00
this . tokens = [ ] ;
2023-07-29 08:31:34 +02:00
this . tokens . links = /* @__PURE__ */ Object . create ( null ) ;
this . options = options2 || _defaults ;
this . options . tokenizer = this . options . tokenizer || new _Tokenizer ( ) ;
2020-04-06 23:25:33 -05:00
this . tokenizer = this . options . tokenizer ;
this . tokenizer . options = this . options ;
2021-08-09 23:41:45 -04:00
this . tokenizer . lexer = this ;
this . inlineQueue = [ ] ;
this . state = {
inLink : false ,
inRawBlock : false ,
top : true
} ;
2020-04-14 16:41:10 -05:00
const rules = {
2021-08-16 03:09:18 +00:00
block : block . normal ,
inline : inline . normal
2020-04-14 16:41:10 -05:00
} ;
if ( this . options . pedantic ) {
2021-08-16 03:09:18 +00:00
rules . block = block . pedantic ;
rules . inline = inline . pedantic ;
2020-04-14 16:41:10 -05:00
} else if ( this . options . gfm ) {
2021-08-16 03:09:18 +00:00
rules . block = block . gfm ;
2020-04-14 16:41:10 -05:00
if ( this . options . breaks ) {
2021-08-16 03:09:18 +00:00
rules . inline = inline . breaks ;
2020-04-14 16:41:10 -05:00
} else {
2021-08-16 03:09:18 +00:00
rules . inline = inline . gfm ;
2020-04-14 16:41:10 -05:00
}
}
this . tokenizer . rules = rules ;
2019-11-07 12:49:10 -06:00
}
/ * *
2020-04-14 16:41:10 -05:00
* Expose Rules
2019-11-07 12:49:10 -06:00
* /
static get rules ( ) {
2020-04-14 16:41:10 -05:00
return {
2021-08-16 03:09:18 +00:00
block ,
inline
2020-04-14 16:41:10 -05:00
} ;
2019-11-07 12:49:10 -06:00
}
/ * *
* Static Lex Method
* /
2023-07-29 08:31:34 +02:00
static lex ( src , options2 ) {
const lexer2 = new _Lexer ( options2 ) ;
return lexer2 . lex ( src ) ;
2020-04-02 00:23:40 -05:00
}
2020-09-26 19:05:47 +00:00
/ * *
* Static Lex Inline Method
* /
2023-07-29 08:31:34 +02:00
static lexInline ( src , options2 ) {
const lexer2 = new _Lexer ( options2 ) ;
return lexer2 . inlineTokens ( src ) ;
2020-09-26 19:05:47 +00:00
}
2019-11-07 12:49:10 -06:00
/ * *
* Preprocessing
* /
lex ( src ) {
2023-07-29 08:31:34 +02:00
src = src . replace ( /\r\n|\r/g , "\n" ) ;
2021-08-09 23:41:45 -04:00
this . blockTokens ( src , this . tokens ) ;
let next ;
while ( next = this . inlineQueue . shift ( ) ) {
this . inlineTokens ( next . src , next . tokens ) ;
}
2020-04-02 00:23:40 -05:00
return this . tokens ;
}
2021-08-09 23:41:45 -04:00
blockTokens ( src , tokens = [ ] ) {
2021-01-26 14:21:28 +00:00
if ( this . options . pedantic ) {
2023-07-29 08:31:34 +02:00
src = src . replace ( /\t/g , " " ) . replace ( /^ +$/gm , "" ) ;
2022-04-11 00:38:29 +00:00
} else {
src = src . replace ( /^( *)(\t+)/gm , ( _ , leading , tabs ) => {
2023-07-29 08:31:34 +02:00
return leading + " " . repeat ( tabs . length ) ;
2022-04-11 00:38:29 +00:00
} ) ;
2021-01-26 14:21:28 +00:00
}
2021-08-09 23:41:45 -04:00
let token , lastToken , cutSrc , lastParagraphClipped ;
2019-11-07 12:49:10 -06:00
while ( src ) {
2023-07-29 08:31:34 +02:00
if ( this . options . extensions && this . options . extensions . block && this . options . extensions . block . some ( ( extTokenizer ) => {
if ( token = extTokenizer . call ( { lexer : this } , src , tokens ) ) {
src = src . substring ( token . raw . length ) ;
tokens . push ( token ) ;
return true ;
}
return false ;
} ) ) {
2021-06-15 19:22:00 -04:00
continue ;
}
2020-04-14 14:33:36 -05:00
if ( token = this . tokenizer . space ( src ) ) {
2020-04-08 13:06:43 -05:00
src = src . substring ( token . raw . length ) ;
2022-01-06 15:33:25 +00:00
if ( token . raw . length === 1 && tokens . length > 0 ) {
2023-07-29 08:31:34 +02:00
tokens [ tokens . length - 1 ] . raw += "\n" ;
2022-01-06 15:33:25 +00:00
} else {
2020-04-08 13:06:43 -05:00
tokens . push ( token ) ;
}
2020-04-06 23:25:33 -05:00
continue ;
2019-11-07 12:49:10 -06:00
}
2021-02-07 17:22:47 -05:00
if ( token = this . tokenizer . code ( src ) ) {
2020-04-08 13:06:43 -05:00
src = src . substring ( token . raw . length ) ;
2021-02-07 17:22:47 -05:00
lastToken = tokens [ tokens . length - 1 ] ;
2023-07-29 08:31:34 +02:00
if ( lastToken && ( lastToken . type === "paragraph" || lastToken . type === "text" ) ) {
lastToken . raw += "\n" + token . raw ;
lastToken . text += "\n" + token . text ;
2021-08-09 23:41:45 -04:00
this . inlineQueue [ this . inlineQueue . length - 1 ] . src = lastToken . text ;
2021-02-07 17:22:47 -05:00
} else {
tokens . push ( token ) ;
2020-05-03 19:20:54 +00:00
}
2019-11-07 12:49:10 -06:00
continue ;
}
2020-04-14 14:33:36 -05:00
if ( token = this . tokenizer . fences ( src ) ) {
2020-04-08 13:06:43 -05:00
src = src . substring ( token . raw . length ) ;
2020-04-06 23:25:33 -05:00
tokens . push ( token ) ;
2019-11-07 12:49:10 -06:00
continue ;
}
2020-04-14 14:33:36 -05:00
if ( token = this . tokenizer . heading ( src ) ) {
2020-04-08 13:06:43 -05:00
src = src . substring ( token . raw . length ) ;
2020-04-06 23:25:33 -05:00
tokens . push ( token ) ;
2019-11-07 12:49:10 -06:00
continue ;
}
2020-04-14 14:33:36 -05:00
if ( token = this . tokenizer . hr ( src ) ) {
2020-04-08 13:06:43 -05:00
src = src . substring ( token . raw . length ) ;
2020-04-06 23:25:33 -05:00
tokens . push ( token ) ;
2019-11-07 12:49:10 -06:00
continue ;
}
2020-04-14 14:33:36 -05:00
if ( token = this . tokenizer . blockquote ( src ) ) {
2020-04-08 13:06:43 -05:00
src = src . substring ( token . raw . length ) ;
tokens . push ( token ) ;
2019-11-07 12:49:10 -06:00
continue ;
}
2020-04-14 14:33:36 -05:00
if ( token = this . tokenizer . list ( src ) ) {
2020-04-08 13:06:43 -05:00
src = src . substring ( token . raw . length ) ;
tokens . push ( token ) ;
2019-11-07 12:49:10 -06:00
continue ;
}
2020-04-14 14:33:36 -05:00
if ( token = this . tokenizer . html ( src ) ) {
2020-04-08 13:06:43 -05:00
src = src . substring ( token . raw . length ) ;
2020-04-06 23:25:33 -05:00
tokens . push ( token ) ;
2019-11-07 12:49:10 -06:00
continue ;
}
2021-08-09 23:41:45 -04:00
if ( token = this . tokenizer . def ( src ) ) {
2020-04-08 13:06:43 -05:00
src = src . substring ( token . raw . length ) ;
2021-08-09 23:41:45 -04:00
lastToken = tokens [ tokens . length - 1 ] ;
2023-07-29 08:31:34 +02:00
if ( lastToken && ( lastToken . type === "paragraph" || lastToken . type === "text" ) ) {
lastToken . raw += "\n" + token . raw ;
lastToken . text += "\n" + token . raw ;
2021-08-09 23:41:45 -04:00
this . inlineQueue [ this . inlineQueue . length - 1 ] . src = lastToken . text ;
} else if ( ! this . tokens . links [ token . tag ] ) {
2020-04-06 23:25:33 -05:00
this . tokens . links [ token . tag ] = {
href : token . href ,
title : token . title
2019-11-07 12:49:10 -06:00
} ;
}
continue ;
}
2020-04-14 14:33:36 -05:00
if ( token = this . tokenizer . table ( src ) ) {
2020-04-08 13:06:43 -05:00
src = src . substring ( token . raw . length ) ;
2020-04-06 23:25:33 -05:00
tokens . push ( token ) ;
continue ;
2019-11-07 12:49:10 -06:00
}
2020-04-14 14:33:36 -05:00
if ( token = this . tokenizer . lheading ( src ) ) {
2020-04-08 13:06:43 -05:00
src = src . substring ( token . raw . length ) ;
2020-04-06 23:25:33 -05:00
tokens . push ( token ) ;
2019-11-07 12:49:10 -06:00
continue ;
}
2021-06-15 19:22:00 -04:00
cutSrc = src ;
2021-06-16 13:49:59 +00:00
if ( this . options . extensions && this . options . extensions . startBlock ) {
2021-06-15 19:22:00 -04:00
let startIndex = Infinity ;
const tempSrc = src . slice ( 1 ) ;
let tempStart ;
2023-07-29 08:31:34 +02:00
this . options . extensions . startBlock . forEach ( ( getStartIndex ) => {
2021-08-09 23:41:45 -04:00
tempStart = getStartIndex . call ( { lexer : this } , tempSrc ) ;
2023-07-29 08:31:34 +02:00
if ( typeof tempStart === "number" && tempStart >= 0 ) {
startIndex = Math . min ( startIndex , tempStart ) ;
}
2021-06-15 19:22:00 -04:00
} ) ;
if ( startIndex < Infinity && startIndex >= 0 ) {
cutSrc = src . substring ( 0 , startIndex + 1 ) ;
}
}
2021-08-09 23:41:45 -04:00
if ( this . state . top && ( token = this . tokenizer . paragraph ( cutSrc ) ) ) {
2021-06-15 19:22:00 -04:00
lastToken = tokens [ tokens . length - 1 ] ;
2023-07-29 08:31:34 +02:00
if ( lastParagraphClipped && lastToken . type === "paragraph" ) {
lastToken . raw += "\n" + token . raw ;
lastToken . text += "\n" + token . text ;
2021-08-09 23:41:45 -04:00
this . inlineQueue . pop ( ) ;
this . inlineQueue [ this . inlineQueue . length - 1 ] . src = lastToken . text ;
2021-06-15 19:22:00 -04:00
} else {
tokens . push ( token ) ;
}
2023-07-29 08:31:34 +02:00
lastParagraphClipped = cutSrc . length !== src . length ;
2020-04-08 13:06:43 -05:00
src = src . substring ( token . raw . length ) ;
2019-11-07 12:49:10 -06:00
continue ;
}
2021-02-07 17:22:47 -05:00
if ( token = this . tokenizer . text ( src ) ) {
2020-04-08 13:06:43 -05:00
src = src . substring ( token . raw . length ) ;
2021-02-07 17:22:47 -05:00
lastToken = tokens [ tokens . length - 1 ] ;
2023-07-29 08:31:34 +02:00
if ( lastToken && lastToken . type === "text" ) {
lastToken . raw += "\n" + token . raw ;
lastToken . text += "\n" + token . text ;
2021-08-09 23:41:45 -04:00
this . inlineQueue . pop ( ) ;
this . inlineQueue [ this . inlineQueue . length - 1 ] . src = lastToken . text ;
2021-02-07 17:22:47 -05:00
} else {
tokens . push ( token ) ;
2020-05-03 19:20:54 +00:00
}
2019-11-07 12:49:10 -06:00
continue ;
}
2020-04-08 13:06:43 -05:00
if ( src ) {
2023-07-29 08:31:34 +02:00
const errMsg = "Infinite loop on byte: " + src . charCodeAt ( 0 ) ;
2020-04-02 00:23:40 -05:00
if ( this . options . silent ) {
console . error ( errMsg ) ;
2020-04-06 23:25:33 -05:00
break ;
2020-04-02 00:23:40 -05:00
} else {
throw new Error ( errMsg ) ;
}
2019-11-07 12:49:10 -06:00
}
}
2021-08-09 23:41:45 -04:00
this . state . top = true ;
2020-04-02 00:23:40 -05:00
return tokens ;
2019-11-07 12:49:10 -06:00
}
2022-08-21 16:22:26 +00:00
inline ( src , tokens = [ ] ) {
2021-08-09 23:41:45 -04:00
this . inlineQueue . push ( { src , tokens } ) ;
2022-08-21 16:22:26 +00:00
return tokens ;
2020-04-02 00:23:40 -05:00
}
/ * *
* Lexing / Compiling
* /
2021-08-09 23:41:45 -04:00
inlineTokens ( src , tokens = [ ] ) {
2021-06-15 19:22:00 -04:00
let token , lastToken , cutSrc ;
2020-07-13 13:35:58 +00:00
let maskedSrc = src ;
let match ;
2020-11-19 14:32:02 +00:00
let keepPrevChar , prevChar ;
2020-07-13 13:35:58 +00:00
if ( this . tokens . links ) {
const links = Object . keys ( this . tokens . links ) ;
if ( links . length > 0 ) {
while ( ( match = this . tokenizer . rules . inline . reflinkSearch . exec ( maskedSrc ) ) != null ) {
2023-07-29 08:31:34 +02:00
if ( links . includes ( match [ 0 ] . slice ( match [ 0 ] . lastIndexOf ( "[" ) + 1 , - 1 ) ) ) {
maskedSrc = maskedSrc . slice ( 0 , match . index ) + "[" + "a" . repeat ( match [ 0 ] . length - 2 ) + "]" + maskedSrc . slice ( this . tokenizer . rules . inline . reflinkSearch . lastIndex ) ;
2020-07-13 13:35:58 +00:00
}
}
}
}
while ( ( match = this . tokenizer . rules . inline . blockSkip . exec ( maskedSrc ) ) != null ) {
2023-07-29 08:31:34 +02:00
maskedSrc = maskedSrc . slice ( 0 , match . index ) + "[" + "a" . repeat ( match [ 0 ] . length - 2 ) + "]" + maskedSrc . slice ( this . tokenizer . rules . inline . blockSkip . lastIndex ) ;
2020-07-13 13:35:58 +00:00
}
2023-06-10 03:13:30 +00:00
while ( ( match = this . tokenizer . rules . inline . anyPunctuation . exec ( maskedSrc ) ) != null ) {
2023-07-29 08:31:34 +02:00
maskedSrc = maskedSrc . slice ( 0 , match . index ) + "++" + maskedSrc . slice ( this . tokenizer . rules . inline . anyPunctuation . lastIndex ) ;
2021-02-07 17:25:01 -05:00
}
2020-04-08 13:06:43 -05:00
while ( src ) {
2020-11-19 14:32:02 +00:00
if ( ! keepPrevChar ) {
2023-07-29 08:31:34 +02:00
prevChar = "" ;
2020-11-19 14:32:02 +00:00
}
keepPrevChar = false ;
2023-07-29 08:31:34 +02:00
if ( this . options . extensions && this . options . extensions . inline && this . options . extensions . inline . some ( ( extTokenizer ) => {
if ( token = extTokenizer . call ( { lexer : this } , src , tokens ) ) {
src = src . substring ( token . raw . length ) ;
tokens . push ( token ) ;
return true ;
}
return false ;
} ) ) {
2021-06-15 19:22:00 -04:00
continue ;
}
2020-04-14 14:33:36 -05:00
if ( token = this . tokenizer . escape ( src ) ) {
2020-04-08 13:06:43 -05:00
src = src . substring ( token . raw . length ) ;
2020-04-06 23:25:33 -05:00
tokens . push ( token ) ;
2020-04-02 00:23:40 -05:00
continue ;
}
2021-08-09 23:41:45 -04:00
if ( token = this . tokenizer . tag ( src ) ) {
2020-04-08 13:06:43 -05:00
src = src . substring ( token . raw . length ) ;
2021-06-15 19:22:00 -04:00
lastToken = tokens [ tokens . length - 1 ] ;
2023-07-29 08:31:34 +02:00
if ( lastToken && token . type === "text" && lastToken . type === "text" ) {
2021-02-07 17:22:47 -05:00
lastToken . raw += token . raw ;
lastToken . text += token . text ;
} else {
tokens . push ( token ) ;
}
2020-04-02 00:23:40 -05:00
continue ;
}
2020-04-14 14:33:36 -05:00
if ( token = this . tokenizer . link ( src ) ) {
2020-04-08 13:06:43 -05:00
src = src . substring ( token . raw . length ) ;
2020-04-06 23:25:33 -05:00
tokens . push ( token ) ;
2020-04-02 00:23:40 -05:00
continue ;
}
2020-04-14 14:33:36 -05:00
if ( token = this . tokenizer . reflink ( src , this . tokens . links ) ) {
2020-04-08 13:06:43 -05:00
src = src . substring ( token . raw . length ) ;
2021-06-15 19:22:00 -04:00
lastToken = tokens [ tokens . length - 1 ] ;
2023-07-29 08:31:34 +02:00
if ( lastToken && token . type === "text" && lastToken . type === "text" ) {
2021-02-07 17:22:47 -05:00
lastToken . raw += token . raw ;
lastToken . text += token . text ;
} else {
tokens . push ( token ) ;
2020-04-14 14:33:36 -05:00
}
2020-04-02 00:23:40 -05:00
continue ;
}
2021-02-07 17:25:01 -05:00
if ( token = this . tokenizer . emStrong ( src , maskedSrc , prevChar ) ) {
2020-04-08 13:06:43 -05:00
src = src . substring ( token . raw . length ) ;
tokens . push ( token ) ;
2020-04-02 00:23:40 -05:00
continue ;
}
2020-04-14 14:33:36 -05:00
if ( token = this . tokenizer . codespan ( src ) ) {
2020-04-08 13:06:43 -05:00
src = src . substring ( token . raw . length ) ;
2020-04-06 23:25:33 -05:00
tokens . push ( token ) ;
2020-04-02 00:23:40 -05:00
continue ;
}
2020-04-14 14:33:36 -05:00
if ( token = this . tokenizer . br ( src ) ) {
2020-04-08 13:06:43 -05:00
src = src . substring ( token . raw . length ) ;
2020-04-06 23:25:33 -05:00
tokens . push ( token ) ;
2020-04-02 00:23:40 -05:00
continue ;
}
2020-04-14 14:33:36 -05:00
if ( token = this . tokenizer . del ( src ) ) {
2020-04-08 13:06:43 -05:00
src = src . substring ( token . raw . length ) ;
tokens . push ( token ) ;
2020-04-02 00:23:40 -05:00
continue ;
}
2020-04-14 16:41:10 -05:00
if ( token = this . tokenizer . autolink ( src , mangle ) ) {
2020-04-08 13:06:43 -05:00
src = src . substring ( token . raw . length ) ;
2020-04-06 23:25:33 -05:00
tokens . push ( token ) ;
2020-04-02 00:23:40 -05:00
continue ;
}
2021-08-09 23:41:45 -04:00
if ( ! this . state . inLink && ( token = this . tokenizer . url ( src , mangle ) ) ) {
2020-04-08 13:06:43 -05:00
src = src . substring ( token . raw . length ) ;
2020-04-06 23:25:33 -05:00
tokens . push ( token ) ;
2020-04-02 00:23:40 -05:00
continue ;
}
2021-06-15 19:22:00 -04:00
cutSrc = src ;
2021-06-16 13:49:59 +00:00
if ( this . options . extensions && this . options . extensions . startInline ) {
2021-06-15 19:22:00 -04:00
let startIndex = Infinity ;
2021-06-15 23:23:19 +00:00
const tempSrc = src . slice ( 1 ) ;
let tempStart ;
2023-07-29 08:31:34 +02:00
this . options . extensions . startInline . forEach ( ( getStartIndex ) => {
2021-08-09 23:41:45 -04:00
tempStart = getStartIndex . call ( { lexer : this } , tempSrc ) ;
2023-07-29 08:31:34 +02:00
if ( typeof tempStart === "number" && tempStart >= 0 ) {
startIndex = Math . min ( startIndex , tempStart ) ;
}
2021-06-15 19:22:00 -04:00
} ) ;
2021-06-15 23:23:19 +00:00
if ( startIndex < Infinity && startIndex >= 0 ) {
cutSrc = src . substring ( 0 , startIndex + 1 ) ;
2021-06-15 19:22:00 -04:00
}
}
2021-08-09 23:41:45 -04:00
if ( token = this . tokenizer . inlineText ( cutSrc , smartypants ) ) {
2020-04-08 13:06:43 -05:00
src = src . substring ( token . raw . length ) ;
2023-07-29 08:31:34 +02:00
if ( token . raw . slice ( - 1 ) !== "_" ) {
2021-02-07 17:25:01 -05:00
prevChar = token . raw . slice ( - 1 ) ;
}
2020-11-19 14:32:02 +00:00
keepPrevChar = true ;
2021-02-07 17:22:47 -05:00
lastToken = tokens [ tokens . length - 1 ] ;
2023-07-29 08:31:34 +02:00
if ( lastToken && lastToken . type === "text" ) {
2021-02-07 17:22:47 -05:00
lastToken . raw += token . raw ;
lastToken . text += token . text ;
} else {
tokens . push ( token ) ;
}
2020-04-02 00:23:40 -05:00
continue ;
}
2020-04-08 13:06:43 -05:00
if ( src ) {
2023-07-29 08:31:34 +02:00
const errMsg = "Infinite loop on byte: " + src . charCodeAt ( 0 ) ;
2020-04-02 00:23:40 -05:00
if ( this . options . silent ) {
console . error ( errMsg ) ;
2020-04-06 23:25:33 -05:00
break ;
2020-04-02 00:23:40 -05:00
} else {
throw new Error ( errMsg ) ;
}
}
2019-11-07 12:49:10 -06:00
}
2020-04-08 13:06:43 -05:00
return tokens ;
2020-04-02 00:23:40 -05:00
}
2023-07-29 08:31:34 +02:00
} ;
2019-11-07 12:49:10 -06:00
2023-07-29 08:31:34 +02:00
// src/Renderer.ts
var _Renderer = class {
constructor ( options2 ) {
this . options = options2 || _defaults ;
2020-04-02 00:23:40 -05:00
}
code ( code , infostring , escaped ) {
2023-07-29 08:31:34 +02:00
const lang = ( infostring || "" ) . match ( /\S*/ ) [ 0 ] ;
2020-04-02 00:23:40 -05:00
if ( this . options . highlight ) {
const out = this . options . highlight ( code , lang ) ;
if ( out != null && out !== code ) {
escaped = true ;
code = out ;
}
2019-11-07 12:49:10 -06:00
}
2023-07-29 08:31:34 +02:00
code = code . replace ( /\n$/ , "" ) + "\n" ;
2020-04-02 00:23:40 -05:00
if ( ! lang ) {
2023-07-29 08:31:34 +02:00
return "<pre><code>" + ( escaped ? code : escape ( code , true ) ) + "</code></pre>\n" ;
2019-11-07 12:49:10 -06:00
}
2023-07-29 08:31:34 +02:00
return '<pre><code class="' + this . options . langPrefix + escape ( lang ) + '">' + ( escaped ? code : escape ( code , true ) ) + "</code></pre>\n" ;
2019-11-07 12:49:10 -06:00
}
2020-04-02 00:23:40 -05:00
blockquote ( quote ) {
2023-07-29 08:31:34 +02:00
return ` <blockquote>
$ { quote } < / b l o c k q u o t e >
` ;
2019-11-07 12:49:10 -06:00
}
2023-07-29 08:31:34 +02:00
html ( html , block2 ) {
2020-04-02 00:23:40 -05:00
return html ;
}
heading ( text , level , raw , slugger ) {
if ( this . options . headerIds ) {
2022-04-08 01:54:20 +00:00
const id = this . options . headerPrefix + slugger . slug ( raw ) ;
2023-07-29 08:31:34 +02:00
return ` <h ${ level } id=" ${ id } "> ${ text } </h ${ level } >
` ;
2020-04-02 00:23:40 -05:00
}
2023-07-29 08:31:34 +02:00
return ` <h ${ level } > ${ text } </h ${ level } >
` ;
2020-04-02 00:23:40 -05:00
}
hr ( ) {
2023-07-29 08:31:34 +02:00
return this . options . xhtml ? "<hr/>\n" : "<hr>\n" ;
2020-04-02 00:23:40 -05:00
}
list ( body , ordered , start ) {
2023-07-29 08:31:34 +02:00
const type = ordered ? "ol" : "ul" , startatt = ordered && start !== 1 ? ' start="' + start + '"' : "" ;
return "<" + type + startatt + ">\n" + body + "</" + type + ">\n" ;
2020-04-02 00:23:40 -05:00
}
2023-07-29 08:31:34 +02:00
listitem ( text , task , checked ) {
return ` <li> ${ text } </li>
` ;
2020-04-02 00:23:40 -05:00
}
checkbox ( checked ) {
2023-07-29 08:31:34 +02:00
return "<input " + ( checked ? 'checked="" ' : "" ) + 'disabled="" type="checkbox"' + ( this . options . xhtml ? " /" : "" ) + "> " ;
2020-04-02 00:23:40 -05:00
}
paragraph ( text ) {
2023-07-29 08:31:34 +02:00
return ` <p> ${ text } </p>
` ;
2020-04-02 00:23:40 -05:00
}
table ( header , body ) {
2023-07-29 08:31:34 +02:00
if ( body )
body = ` <tbody> ${ body } </tbody> ` ;
return "<table>\n<thead>\n" + header + "</thead>\n" + body + "</table>\n" ;
2020-04-02 00:23:40 -05:00
}
tablerow ( content ) {
2023-07-29 08:31:34 +02:00
return ` <tr>
$ { content } < / t r >
` ;
2019-11-07 12:49:10 -06:00
}
2020-04-02 00:23:40 -05:00
tablecell ( content , flags ) {
2023-07-29 08:31:34 +02:00
const type = flags . header ? "th" : "td" ;
const tag = flags . align ? ` < ${ type } align=" ${ flags . align } "> ` : ` < ${ type } > ` ;
return tag + content + ` </ ${ type } >
` ;
2019-11-07 12:49:10 -06:00
}
2022-04-08 01:54:20 +00:00
/ * *
* span level renderer
* /
2020-04-02 00:23:40 -05:00
strong ( text ) {
2022-04-08 01:54:20 +00:00
return ` <strong> ${ text } </strong> ` ;
2020-04-02 00:23:40 -05:00
}
em ( text ) {
2022-04-08 01:54:20 +00:00
return ` <em> ${ text } </em> ` ;
2019-11-07 12:49:10 -06:00
}
2020-04-02 00:23:40 -05:00
codespan ( text ) {
2022-04-08 01:54:20 +00:00
return ` <code> ${ text } </code> ` ;
2019-11-07 12:49:10 -06:00
}
2020-04-02 00:23:40 -05:00
br ( ) {
2023-07-29 08:31:34 +02:00
return this . options . xhtml ? "<br/>" : "<br>" ;
2020-04-02 00:23:40 -05:00
}
del ( text ) {
2022-04-08 01:54:20 +00:00
return ` <del> ${ text } </del> ` ;
2020-04-02 00:23:40 -05:00
}
link ( href , title , text ) {
2021-08-16 03:09:18 +00:00
href = cleanUrl ( this . options . sanitize , this . options . baseUrl , href ) ;
2020-04-02 00:23:40 -05:00
if ( href === null ) {
return text ;
}
2022-11-20 16:07:47 +00:00
let out = '<a href="' + href + '"' ;
2020-04-02 00:23:40 -05:00
if ( title ) {
out += ' title="' + title + '"' ;
}
2023-07-29 08:31:34 +02:00
out += ">" + text + "</a>" ;
2020-04-02 00:23:40 -05:00
return out ;
}
image ( href , title , text ) {
2021-08-16 03:09:18 +00:00
href = cleanUrl ( this . options . sanitize , this . options . baseUrl , href ) ;
2020-04-02 00:23:40 -05:00
if ( href === null ) {
return text ;
2019-11-07 12:49:10 -06:00
}
2022-04-08 01:54:20 +00:00
let out = ` <img src=" ${ href } " alt=" ${ text } " ` ;
2020-04-02 00:23:40 -05:00
if ( title ) {
2022-04-08 01:54:20 +00:00
out += ` title=" ${ title } " ` ;
2020-04-02 00:23:40 -05:00
}
2023-07-29 08:31:34 +02:00
out += this . options . xhtml ? "/>" : ">" ;
2019-11-07 12:49:10 -06:00
return out ;
}
2020-04-02 00:23:40 -05:00
text ( text ) {
return text ;
}
2023-07-29 08:31:34 +02:00
} ;
2019-11-07 12:49:10 -06:00
2023-07-29 08:31:34 +02:00
// src/TextRenderer.ts
var _TextRenderer = class {
2019-11-07 12:49:10 -06:00
// no need for block level renderers
strong ( text ) {
return text ;
}
em ( text ) {
return text ;
}
codespan ( text ) {
return text ;
}
del ( text ) {
2020-03-22 05:13:27 +00:00
return text ;
}
html ( text ) {
2019-11-07 12:49:10 -06:00
return text ;
}
text ( text ) {
return text ;
}
link ( href , title , text ) {
2023-07-29 08:31:34 +02:00
return "" + text ;
2019-11-07 12:49:10 -06:00
}
image ( href , title , text ) {
2023-07-29 08:31:34 +02:00
return "" + text ;
2019-11-07 12:49:10 -06:00
}
br ( ) {
2023-07-29 08:31:34 +02:00
return "" ;
2019-11-07 12:49:10 -06:00
}
2023-07-29 08:31:34 +02:00
} ;
2019-11-07 12:49:10 -06:00
2023-07-29 08:31:34 +02:00
// src/Slugger.ts
var _Slugger = class {
2020-04-02 00:23:40 -05:00
constructor ( ) {
this . seen = { } ;
}
2020-07-15 12:47:26 +00:00
serialize ( value ) {
2023-07-29 08:31:34 +02:00
return value . toLowerCase ( ) . trim ( ) . replace ( /<[!\/a-z].*?>/ig , "" ) . replace ( /[\u2000-\u206F\u2E00-\u2E7F\\'!"#$%&()*+,./:;<=>?@[\]^`{|}~]/g , "" ) . replace ( /\s/g , "-" ) ;
2020-07-15 12:47:26 +00:00
}
/ * *
* Finds the next safe ( unique ) slug to use
* /
getNextSafeSlug ( originalSlug , isDryRun ) {
let slug = originalSlug ;
let occurenceAccumulator = 0 ;
2020-04-02 00:23:40 -05:00
if ( this . seen . hasOwnProperty ( slug ) ) {
2020-07-15 12:47:26 +00:00
occurenceAccumulator = this . seen [ originalSlug ] ;
2020-04-02 00:23:40 -05:00
do {
2020-07-15 12:47:26 +00:00
occurenceAccumulator ++ ;
2023-07-29 08:31:34 +02:00
slug = originalSlug + "-" + occurenceAccumulator ;
2020-04-02 00:23:40 -05:00
} while ( this . seen . hasOwnProperty ( slug ) ) ;
}
2020-07-15 12:47:26 +00:00
if ( ! isDryRun ) {
this . seen [ originalSlug ] = occurenceAccumulator ;
this . seen [ slug ] = 0 ;
}
2020-04-02 00:23:40 -05:00
return slug ;
}
2020-07-15 12:47:26 +00:00
/ * *
* Convert string to unique id
* /
2023-07-29 08:31:34 +02:00
slug ( value , options2 = { } ) {
2020-07-15 12:47:26 +00:00
const slug = this . serialize ( value ) ;
2023-07-29 08:31:34 +02:00
return this . getNextSafeSlug ( slug , options2 . dryrun ) ;
2020-07-15 12:47:26 +00:00
}
2023-07-29 08:31:34 +02:00
} ;
2019-11-07 12:49:10 -06:00
2023-07-29 08:31:34 +02:00
// src/Parser.ts
var _Parser = class {
constructor ( options2 ) {
this . options = options2 || _defaults ;
this . options . renderer = this . options . renderer || new _Renderer ( ) ;
2019-11-07 12:49:10 -06:00
this . renderer = this . options . renderer ;
this . renderer . options = this . options ;
2023-07-29 08:31:34 +02:00
this . textRenderer = new _TextRenderer ( ) ;
this . slugger = new _Slugger ( ) ;
2019-11-07 12:49:10 -06:00
}
/ * *
* Static Parse Method
* /
2023-07-29 08:31:34 +02:00
static parse ( tokens , options2 ) {
const parser2 = new _Parser ( options2 ) ;
return parser2 . parse ( tokens ) ;
2020-04-02 00:23:40 -05:00
}
2020-09-26 19:05:47 +00:00
/ * *
* Static Parse Inline Method
* /
2023-07-29 08:31:34 +02:00
static parseInline ( tokens , options2 ) {
const parser2 = new _Parser ( options2 ) ;
return parser2 . parseInline ( tokens ) ;
2020-09-26 19:05:47 +00:00
}
2019-11-07 12:49:10 -06:00
/ * *
* Parse Loop
* /
2020-04-02 00:23:40 -05:00
parse ( tokens , top = true ) {
2023-07-29 08:31:34 +02:00
let out = "" , i , j , k , l2 , l3 , row , cell , header , body , token , ordered , start , loose , itemBody , item , checked , task , checkbox , ret ;
2020-04-02 00:23:40 -05:00
const l = tokens . length ;
2020-04-02 01:22:01 -05:00
for ( i = 0 ; i < l ; i ++ ) {
2020-04-02 00:23:40 -05:00
token = tokens [ i ] ;
2021-06-16 13:49:59 +00:00
if ( this . options . extensions && this . options . extensions . renderers && this . options . extensions . renderers [ token . type ] ) {
2021-08-09 23:41:45 -04:00
ret = this . options . extensions . renderers [ token . type ] . call ( { parser : this } , token ) ;
2023-07-29 08:31:34 +02:00
if ( ret !== false || ! [ "space" , "hr" , "heading" , "code" , "table" , "blockquote" , "list" , "html" , "paragraph" , "text" ] . includes ( token . type ) ) {
out += ret || "" ;
2021-06-15 23:23:19 +00:00
continue ;
}
}
2020-04-02 00:23:40 -05:00
switch ( token . type ) {
2023-07-29 08:31:34 +02:00
case "space" : {
2020-04-02 00:23:40 -05:00
continue ;
2019-11-07 12:49:10 -06:00
}
2023-07-29 08:31:34 +02:00
case "hr" : {
2020-04-02 00:23:40 -05:00
out += this . renderer . hr ( ) ;
continue ;
}
2023-07-29 08:31:34 +02:00
case "heading" : {
2020-04-02 00:23:40 -05:00
out += this . renderer . heading (
this . parseInline ( token . tokens ) ,
token . depth ,
2021-08-16 03:09:18 +00:00
unescape ( this . parseInline ( token . tokens , this . textRenderer ) ) ,
2023-07-29 08:31:34 +02:00
this . slugger
) ;
2020-04-02 00:23:40 -05:00
continue ;
}
2023-07-29 08:31:34 +02:00
case "code" : {
out += this . renderer . code (
token . text ,
2020-04-02 00:23:40 -05:00
token . lang ,
2023-07-29 08:31:34 +02:00
! ! token . escaped
) ;
2020-04-02 00:23:40 -05:00
continue ;
}
2023-07-29 08:31:34 +02:00
case "table" : {
header = "" ;
cell = "" ;
2020-04-02 00:23:40 -05:00
l2 = token . header . length ;
2020-04-02 01:22:01 -05:00
for ( j = 0 ; j < l2 ; j ++ ) {
2019-11-07 12:49:10 -06:00
cell += this . renderer . tablecell (
2021-08-16 03:09:18 +00:00
this . parseInline ( token . header [ j ] . tokens ) ,
2020-04-02 01:22:01 -05:00
{ header : true , align : token . align [ j ] }
2019-11-07 12:49:10 -06:00
) ;
}
2020-04-02 00:23:40 -05:00
header += this . renderer . tablerow ( cell ) ;
2023-07-29 08:31:34 +02:00
body = "" ;
2021-08-16 03:09:18 +00:00
l2 = token . rows . length ;
2020-04-02 01:22:01 -05:00
for ( j = 0 ; j < l2 ; j ++ ) {
2021-08-16 03:09:18 +00:00
row = token . rows [ j ] ;
2023-07-29 08:31:34 +02:00
cell = "" ;
2020-04-02 00:23:40 -05:00
l3 = row . length ;
2020-04-02 01:22:01 -05:00
for ( k = 0 ; k < l3 ; k ++ ) {
2020-04-02 00:23:40 -05:00
cell += this . renderer . tablecell (
2021-08-16 03:09:18 +00:00
this . parseInline ( row [ k ] . tokens ) ,
2020-04-02 01:22:01 -05:00
{ header : false , align : token . align [ k ] }
2020-04-02 00:23:40 -05:00
) ;
}
body += this . renderer . tablerow ( cell ) ;
}
out += this . renderer . table ( header , body ) ;
continue ;
2019-11-07 12:49:10 -06:00
}
2023-07-29 08:31:34 +02:00
case "blockquote" : {
2020-04-02 00:23:40 -05:00
body = this . parse ( token . tokens ) ;
out += this . renderer . blockquote ( body ) ;
continue ;
2019-11-07 12:49:10 -06:00
}
2023-07-29 08:31:34 +02:00
case "list" : {
2020-04-02 01:22:01 -05:00
ordered = token . ordered ;
start = token . start ;
loose = token . loose ;
l2 = token . items . length ;
2023-07-29 08:31:34 +02:00
body = "" ;
2020-04-02 01:22:01 -05:00
for ( j = 0 ; j < l2 ; j ++ ) {
item = token . items [ j ] ;
checked = item . checked ;
task = item . task ;
2023-07-29 08:31:34 +02:00
itemBody = "" ;
2020-04-02 00:23:40 -05:00
if ( item . task ) {
2023-07-29 08:31:34 +02:00
checkbox = this . renderer . checkbox ( ! ! checked ) ;
2020-04-02 00:23:40 -05:00
if ( loose ) {
2023-07-29 08:31:34 +02:00
if ( item . tokens . length > 0 && item . tokens [ 0 ] . type === "paragraph" ) {
item . tokens [ 0 ] . text = checkbox + " " + item . tokens [ 0 ] . text ;
if ( item . tokens [ 0 ] . tokens && item . tokens [ 0 ] . tokens . length > 0 && item . tokens [ 0 ] . tokens [ 0 ] . type === "text" ) {
item . tokens [ 0 ] . tokens [ 0 ] . text = checkbox + " " + item . tokens [ 0 ] . tokens [ 0 ] . text ;
2020-04-02 00:23:40 -05:00
}
} else {
item . tokens . unshift ( {
2023-07-29 08:31:34 +02:00
type : "text" ,
2020-04-02 00:23:40 -05:00
text : checkbox
} ) ;
}
} else {
itemBody += checkbox ;
}
}
itemBody += this . parse ( item . tokens , loose ) ;
2023-07-29 08:31:34 +02:00
body += this . renderer . listitem ( itemBody , task , ! ! checked ) ;
2020-04-02 00:23:40 -05:00
}
out += this . renderer . list ( body , ordered , start ) ;
continue ;
2019-11-07 12:49:10 -06:00
}
2023-07-29 08:31:34 +02:00
case "html" : {
2023-05-02 04:35:05 +00:00
out += this . renderer . html ( token . text , token . block ) ;
2020-04-02 00:23:40 -05:00
continue ;
}
2023-07-29 08:31:34 +02:00
case "paragraph" : {
2020-04-02 00:23:40 -05:00
out += this . renderer . paragraph ( this . parseInline ( token . tokens ) ) ;
continue ;
}
2023-07-29 08:31:34 +02:00
case "text" : {
2020-04-02 00:23:40 -05:00
body = token . tokens ? this . parseInline ( token . tokens ) : token . text ;
2023-07-29 08:31:34 +02:00
while ( i + 1 < l && tokens [ i + 1 ] . type === "text" ) {
2020-04-02 00:23:40 -05:00
token = tokens [ ++ i ] ;
2023-07-29 08:31:34 +02:00
body += "\n" + ( token . tokens ? this . parseInline ( token . tokens ) : token . text ) ;
2020-04-02 00:23:40 -05:00
}
out += top ? this . renderer . paragraph ( body ) : body ;
continue ;
}
default : {
const errMsg = 'Token with "' + token . type + '" type was not found.' ;
if ( this . options . silent ) {
console . error ( errMsg ) ;
2023-07-29 08:31:34 +02:00
return "" ;
2019-11-07 12:49:10 -06:00
} else {
2020-04-02 00:23:40 -05:00
throw new Error ( errMsg ) ;
2019-11-07 12:49:10 -06:00
}
}
2020-04-02 00:23:40 -05:00
}
}
return out ;
}
/ * *
* Parse Inline Tokens
* /
parseInline ( tokens , renderer ) {
renderer = renderer || this . renderer ;
2023-07-29 08:31:34 +02:00
let out = "" , i , token , ret ;
2020-04-02 00:23:40 -05:00
const l = tokens . length ;
for ( i = 0 ; i < l ; i ++ ) {
token = tokens [ i ] ;
2021-06-16 13:49:59 +00:00
if ( this . options . extensions && this . options . extensions . renderers && this . options . extensions . renderers [ token . type ] ) {
2021-08-09 23:41:45 -04:00
ret = this . options . extensions . renderers [ token . type ] . call ( { parser : this } , token ) ;
2023-07-29 08:31:34 +02:00
if ( ret !== false || ! [ "escape" , "html" , "link" , "image" , "strong" , "em" , "codespan" , "br" , "del" , "text" ] . includes ( token . type ) ) {
out += ret || "" ;
2021-06-15 23:23:19 +00:00
continue ;
}
}
2020-04-02 00:23:40 -05:00
switch ( token . type ) {
2023-07-29 08:31:34 +02:00
case "escape" : {
2020-04-14 13:20:35 +00:00
out += renderer . text ( token . text ) ;
2020-04-02 00:23:40 -05:00
break ;
2019-11-07 12:49:10 -06:00
}
2023-07-29 08:31:34 +02:00
case "html" : {
2020-04-02 00:23:40 -05:00
out += renderer . html ( token . text ) ;
break ;
}
2023-07-29 08:31:34 +02:00
case "link" : {
2020-04-02 00:23:40 -05:00
out += renderer . link ( token . href , token . title , this . parseInline ( token . tokens , renderer ) ) ;
break ;
}
2023-07-29 08:31:34 +02:00
case "image" : {
2020-04-02 00:23:40 -05:00
out += renderer . image ( token . href , token . title , token . text ) ;
break ;
}
2023-07-29 08:31:34 +02:00
case "strong" : {
2020-04-02 00:23:40 -05:00
out += renderer . strong ( this . parseInline ( token . tokens , renderer ) ) ;
break ;
}
2023-07-29 08:31:34 +02:00
case "em" : {
2020-04-02 00:23:40 -05:00
out += renderer . em ( this . parseInline ( token . tokens , renderer ) ) ;
break ;
}
2023-07-29 08:31:34 +02:00
case "codespan" : {
2020-04-02 00:23:40 -05:00
out += renderer . codespan ( token . text ) ;
break ;
}
2023-07-29 08:31:34 +02:00
case "br" : {
2020-04-02 00:23:40 -05:00
out += renderer . br ( ) ;
break ;
}
2023-07-29 08:31:34 +02:00
case "del" : {
2020-04-02 00:23:40 -05:00
out += renderer . del ( this . parseInline ( token . tokens , renderer ) ) ;
break ;
}
2023-07-29 08:31:34 +02:00
case "text" : {
2020-04-02 00:23:40 -05:00
out += renderer . text ( token . text ) ;
break ;
}
default : {
2020-04-08 17:56:45 +00:00
const errMsg = 'Token with "' + token . type + '" type was not found.' ;
2020-04-02 00:23:40 -05:00
if ( this . options . silent ) {
console . error ( errMsg ) ;
2023-07-29 08:31:34 +02:00
return "" ;
2020-04-02 00:23:40 -05:00
} else {
throw new Error ( errMsg ) ;
}
2019-11-07 12:49:10 -06:00
}
}
}
2020-04-02 00:23:40 -05:00
return out ;
}
2023-07-29 08:31:34 +02:00
} ;
2019-11-07 12:49:10 -06:00
2023-07-29 08:31:34 +02:00
// src/Hooks.ts
var _Hooks = class {
constructor ( options2 ) {
this . options = options2 || _defaults ;
2019-11-07 12:49:10 -06:00
}
2023-03-22 05:52:21 +00:00
/ * *
* Process markdown before marked
* /
preprocess ( markdown ) {
return markdown ;
2019-11-07 12:49:10 -06:00
}
2023-03-22 05:52:21 +00:00
/ * *
* Process HTML after marked is finished
* /
postprocess ( html ) {
return html ;
2020-05-14 15:54:39 +00:00
}
2023-07-29 08:31:34 +02:00
} ;
_Hooks . passThroughHooks = /* @__PURE__ */ new Set ( [
"preprocess" ,
"postprocess"
] ) ;
// src/Instance.ts
var _parseMarkdown , parseMarkdown _fn , _onError , onError _fn ;
var Marked = class {
2023-06-10 03:13:30 +00:00
constructor ( ... args ) {
2023-07-29 08:31:34 +02:00
_ _privateAdd ( this , _parseMarkdown ) ;
_ _privateAdd ( this , _onError ) ;
this . defaults = _getDefaults ( ) ;
this . options = this . setOptions ;
this . parse = _ _privateMethod ( this , _parseMarkdown , parseMarkdown _fn ) . call ( this , _Lexer . lex , _Parser . parse ) ;
this . parseInline = _ _privateMethod ( this , _parseMarkdown , parseMarkdown _fn ) . call ( this , _Lexer . lexInline , _Parser . parseInline ) ;
this . Parser = _Parser ;
this . parser = _Parser . parse ;
this . Renderer = _Renderer ;
this . TextRenderer = _TextRenderer ;
this . Lexer = _Lexer ;
this . lexer = _Lexer . lex ;
this . Tokenizer = _Tokenizer ;
this . Slugger = _Slugger ;
this . Hooks = _Hooks ;
2023-06-10 03:13:30 +00:00
this . use ( ... args ) ;
}
2023-07-29 08:31:34 +02:00
/ * *
* Run callback for every token
* /
2023-06-10 03:13:30 +00:00
walkTokens ( tokens , callback ) {
let values = [ ] ;
for ( const token of tokens ) {
values = values . concat ( callback . call ( this , token ) ) ;
switch ( token . type ) {
2023-07-29 08:31:34 +02:00
case "table" : {
2023-06-10 03:13:30 +00:00
for ( const cell of token . header ) {
values = values . concat ( this . walkTokens ( cell . tokens , callback ) ) ;
}
for ( const row of token . rows ) {
for ( const cell of row ) {
values = values . concat ( this . walkTokens ( cell . tokens , callback ) ) ;
}
}
break ;
}
2023-07-29 08:31:34 +02:00
case "list" : {
2023-06-10 03:13:30 +00:00
values = values . concat ( this . walkTokens ( token . items , callback ) ) ;
break ;
}
default : {
2023-07-29 08:31:34 +02:00
if ( this . defaults . extensions && this . defaults . extensions . childTokens && this . defaults . extensions . childTokens [ token . type ] ) {
2023-06-10 03:13:30 +00:00
this . defaults . extensions . childTokens [ token . type ] . forEach ( ( childTokens ) => {
values = values . concat ( this . walkTokens ( token [ childTokens ] , callback ) ) ;
} ) ;
} else if ( token . tokens ) {
values = values . concat ( this . walkTokens ( token . tokens , callback ) ) ;
}
}
}
2023-03-22 05:52:21 +00:00
}
2023-06-10 03:13:30 +00:00
return values ;
}
use ( ... args ) {
const extensions = this . defaults . extensions || { renderers : { } , childTokens : { } } ;
args . forEach ( ( pack ) => {
const opts = { ... pack } ;
opts . async = this . defaults . async || opts . async || false ;
if ( pack . extensions ) {
pack . extensions . forEach ( ( ext ) => {
if ( ! ext . name ) {
2023-07-29 08:31:34 +02:00
throw new Error ( "extension name required" ) ;
2023-06-10 03:13:30 +00:00
}
2023-07-29 08:31:34 +02:00
if ( "renderer" in ext ) {
2023-06-10 03:13:30 +00:00
const prevRenderer = extensions . renderers [ ext . name ] ;
if ( prevRenderer ) {
2023-07-29 08:31:34 +02:00
extensions . renderers [ ext . name ] = function ( ... args2 ) {
let ret = ext . renderer . apply ( this , args2 ) ;
2023-06-10 03:13:30 +00:00
if ( ret === false ) {
2023-07-29 08:31:34 +02:00
ret = prevRenderer . apply ( this , args2 ) ;
2023-06-10 03:13:30 +00:00
}
return ret ;
} ;
} else {
extensions . renderers [ ext . name ] = ext . renderer ;
}
}
2023-07-29 08:31:34 +02:00
if ( "tokenizer" in ext ) {
if ( ! ext . level || ext . level !== "block" && ext . level !== "inline" ) {
2023-06-10 03:13:30 +00:00
throw new Error ( "extension level must be 'block' or 'inline'" ) ;
}
if ( extensions [ ext . level ] ) {
extensions [ ext . level ] . unshift ( ext . tokenizer ) ;
} else {
extensions [ ext . level ] = [ ext . tokenizer ] ;
}
2023-07-29 08:31:34 +02:00
if ( ext . start ) {
if ( ext . level === "block" ) {
2023-06-10 03:13:30 +00:00
if ( extensions . startBlock ) {
extensions . startBlock . push ( ext . start ) ;
} else {
extensions . startBlock = [ ext . start ] ;
}
2023-07-29 08:31:34 +02:00
} else if ( ext . level === "inline" ) {
2023-06-10 03:13:30 +00:00
if ( extensions . startInline ) {
extensions . startInline . push ( ext . start ) ;
} else {
extensions . startInline = [ ext . start ] ;
}
}
}
}
2023-07-29 08:31:34 +02:00
if ( "childTokens" in ext && ext . childTokens ) {
2023-06-10 03:13:30 +00:00
extensions . childTokens [ ext . name ] = ext . childTokens ;
}
} ) ;
opts . extensions = extensions ;
2023-03-22 05:52:21 +00:00
}
2023-06-10 03:13:30 +00:00
if ( pack . renderer ) {
2023-07-29 08:31:34 +02:00
const renderer = this . defaults . renderer || new _Renderer ( this . defaults ) ;
2023-06-10 03:13:30 +00:00
for ( const prop in pack . renderer ) {
const prevRenderer = renderer [ prop ] ;
2023-07-29 08:31:34 +02:00
renderer [ prop ] = ( ... args2 ) => {
let ret = pack . renderer [ prop ] . apply ( renderer , args2 ) ;
2023-06-10 03:13:30 +00:00
if ( ret === false ) {
2023-07-29 08:31:34 +02:00
ret = prevRenderer . apply ( renderer , args2 ) ;
2020-05-22 15:37:31 +00:00
}
2023-06-10 03:13:30 +00:00
return ret ;
} ;
}
opts . renderer = renderer ;
}
if ( pack . tokenizer ) {
2023-07-29 08:31:34 +02:00
const tokenizer = this . defaults . tokenizer || new _Tokenizer ( this . defaults ) ;
2023-06-10 03:13:30 +00:00
for ( const prop in pack . tokenizer ) {
const prevTokenizer = tokenizer [ prop ] ;
2023-07-29 08:31:34 +02:00
tokenizer [ prop ] = ( ... args2 ) => {
let ret = pack . tokenizer [ prop ] . apply ( tokenizer , args2 ) ;
2023-06-10 03:13:30 +00:00
if ( ret === false ) {
2023-07-29 08:31:34 +02:00
ret = prevTokenizer . apply ( tokenizer , args2 ) ;
2020-05-22 15:37:31 +00:00
}
2023-06-10 03:13:30 +00:00
return ret ;
} ;
}
opts . tokenizer = tokenizer ;
}
if ( pack . hooks ) {
2023-07-29 08:31:34 +02:00
const hooks = this . defaults . hooks || new _Hooks ( ) ;
2023-06-10 03:13:30 +00:00
for ( const prop in pack . hooks ) {
const prevHook = hooks [ prop ] ;
2023-07-29 08:31:34 +02:00
if ( _Hooks . passThroughHooks . has ( prop ) ) {
2023-06-10 03:13:30 +00:00
hooks [ prop ] = ( arg ) => {
if ( this . defaults . async ) {
2023-07-29 08:31:34 +02:00
return Promise . resolve ( pack . hooks [ prop ] . call ( hooks , arg ) ) . then ( ( ret2 ) => {
return prevHook . call ( hooks , ret2 ) ;
2023-06-10 03:13:30 +00:00
} ) ;
}
const ret = pack . hooks [ prop ] . call ( hooks , arg ) ;
return prevHook . call ( hooks , ret ) ;
} ;
} else {
2023-07-29 08:31:34 +02:00
hooks [ prop ] = ( ... args2 ) => {
let ret = pack . hooks [ prop ] . apply ( hooks , args2 ) ;
2023-06-10 03:13:30 +00:00
if ( ret === false ) {
2023-07-29 08:31:34 +02:00
ret = prevHook . apply ( hooks , args2 ) ;
2023-06-10 03:13:30 +00:00
}
return ret ;
} ;
2023-03-22 05:52:21 +00:00
}
}
2023-06-10 03:13:30 +00:00
opts . hooks = hooks ;
}
if ( pack . walkTokens ) {
2023-07-29 08:31:34 +02:00
const walkTokens2 = this . defaults . walkTokens ;
2023-06-10 03:13:30 +00:00
opts . walkTokens = function ( token ) {
let values = [ ] ;
values . push ( pack . walkTokens . call ( this , token ) ) ;
2023-07-29 08:31:34 +02:00
if ( walkTokens2 ) {
values = values . concat ( walkTokens2 . call ( this , token ) ) ;
2023-06-10 03:13:30 +00:00
}
return values ;
} ;
}
this . defaults = { ... this . defaults , ... opts } ;
} ) ;
return this ;
}
setOptions ( opt ) {
this . defaults = { ... this . defaults , ... opt } ;
return this ;
}
2023-07-29 08:31:34 +02:00
} ;
_parseMarkdown = new WeakSet ( ) ;
parseMarkdown _fn = function ( lexer2 , parser2 ) {
return ( src , optOrCallback , callback ) => {
if ( typeof optOrCallback === "function" ) {
callback = optOrCallback ;
optOrCallback = null ;
}
const origOpt = { ... optOrCallback } ;
const opt = { ... this . defaults , ... origOpt } ;
const throwError = _ _privateMethod ( this , _onError , onError _fn ) . call ( this , ! ! opt . silent , ! ! opt . async , callback ) ;
if ( typeof src === "undefined" || src === null ) {
return throwError ( new Error ( "marked(): input parameter is undefined or null" ) ) ;
}
if ( typeof src !== "string" ) {
return throwError ( new Error ( "marked(): input parameter is of type " + Object . prototype . toString . call ( src ) + ", string expected" ) ) ;
}
checkDeprecations ( opt , callback ) ;
if ( opt . hooks ) {
opt . hooks . options = opt ;
}
if ( callback ) {
const highlight = opt . highlight ;
let tokens ;
try {
if ( opt . hooks ) {
src = opt . hooks . preprocess ( src ) ;
}
tokens = lexer2 ( src , opt ) ;
} catch ( e ) {
return throwError ( e ) ;
2020-05-14 15:54:39 +00:00
}
2023-07-29 08:31:34 +02:00
const done = ( err ) => {
let out ;
if ( ! err ) {
try {
if ( opt . walkTokens ) {
this . walkTokens ( tokens , opt . walkTokens ) ;
}
out = parser2 ( tokens , opt ) ;
if ( opt . hooks ) {
out = opt . hooks . postprocess ( out ) ;
}
} catch ( e ) {
err = e ;
2023-06-10 03:13:30 +00:00
}
}
2023-07-29 08:31:34 +02:00
opt . highlight = highlight ;
return err ? throwError ( err ) : callback ( null , out ) ;
} ;
if ( ! highlight || highlight . length < 3 ) {
return done ( ) ;
}
delete opt . highlight ;
if ( ! tokens . length )
return done ( ) ;
let pending = 0 ;
this . walkTokens ( tokens , ( token ) => {
if ( token . type === "code" ) {
pending ++ ;
setTimeout ( ( ) => {
highlight ( token . text , token . lang , ( err , code ) => {
if ( err ) {
return done ( err ) ;
2023-03-22 05:52:21 +00:00
}
2023-07-29 08:31:34 +02:00
if ( code != null && code !== token . text ) {
token . text = code ;
token . escaped = true ;
2023-03-22 05:52:21 +00:00
}
2023-07-29 08:31:34 +02:00
pending -- ;
if ( pending === 0 ) {
done ( ) ;
}
} ) ;
} , 0 ) ;
2023-03-22 05:52:21 +00:00
}
2023-07-29 08:31:34 +02:00
} ) ;
if ( pending === 0 ) {
done ( ) ;
2023-03-22 05:52:21 +00:00
}
2023-07-29 08:31:34 +02:00
return ;
}
if ( opt . async ) {
return Promise . resolve ( opt . hooks ? opt . hooks . preprocess ( src ) : src ) . then ( ( src2 ) => lexer2 ( src2 , opt ) ) . then ( ( tokens ) => opt . walkTokens ? Promise . all ( this . walkTokens ( tokens , opt . walkTokens ) ) . then ( ( ) => tokens ) : tokens ) . then ( ( tokens ) => parser2 ( tokens , opt ) ) . then ( ( html ) => opt . hooks ? opt . hooks . postprocess ( html ) : html ) . catch ( throwError ) ;
}
try {
if ( opt . hooks ) {
src = opt . hooks . preprocess ( src ) ;
2023-06-10 03:13:30 +00:00
}
2023-07-29 08:31:34 +02:00
const tokens = lexer2 ( src , opt ) ;
if ( opt . walkTokens ) {
this . walkTokens ( tokens , opt . walkTokens ) ;
2023-06-10 03:13:30 +00:00
}
2023-07-29 08:31:34 +02:00
let html = parser2 ( tokens , opt ) ;
if ( opt . hooks ) {
html = opt . hooks . postprocess ( html ) ;
2023-03-22 05:52:21 +00:00
}
2023-07-29 08:31:34 +02:00
return html ;
} catch ( e ) {
return throwError ( e ) ;
}
} ;
} ;
_onError = new WeakSet ( ) ;
onError _fn = function ( silent , async , callback ) {
return ( e ) => {
e . message += "\nPlease report this to https://github.com/markedjs/marked." ;
if ( silent ) {
const msg = "<p>An error occurred:</p><pre>" + escape ( e . message + "" , true ) + "</pre>" ;
2023-06-10 03:13:30 +00:00
if ( async ) {
2023-07-29 08:31:34 +02:00
return Promise . resolve ( msg ) ;
2023-03-22 05:52:21 +00:00
}
2023-06-10 03:13:30 +00:00
if ( callback ) {
2023-07-29 08:31:34 +02:00
callback ( null , msg ) ;
2023-06-10 03:13:30 +00:00
return ;
2023-03-22 05:52:21 +00:00
}
2023-07-29 08:31:34 +02:00
return msg ;
}
if ( async ) {
return Promise . reject ( e ) ;
}
if ( callback ) {
callback ( e ) ;
return ;
}
throw e ;
} ;
} ;
2023-06-10 03:13:30 +00:00
2023-07-29 08:31:34 +02:00
// src/marked.ts
var markedInstance = new Marked ( ) ;
2023-03-22 05:52:21 +00:00
function marked ( src , opt , callback ) {
2023-06-10 03:13:30 +00:00
return markedInstance . parse ( src , opt , callback ) ;
2019-11-07 12:49:10 -06:00
}
2023-07-29 08:31:34 +02:00
marked . options = marked . setOptions = function ( options2 ) {
markedInstance . setOptions ( options2 ) ;
2023-06-10 03:13:30 +00:00
marked . defaults = markedInstance . defaults ;
2021-11-02 07:32:17 -07:00
changeDefaults ( marked . defaults ) ;
return marked ;
2019-11-07 12:49:10 -06:00
} ;
2023-07-29 08:31:34 +02:00
marked . getDefaults = _getDefaults ;
marked . defaults = _defaults ;
2021-11-02 07:32:17 -07:00
marked . use = function ( ... args ) {
2023-06-10 03:13:30 +00:00
markedInstance . use ( ... args ) ;
marked . defaults = markedInstance . defaults ;
changeDefaults ( marked . defaults ) ;
return marked ;
2020-04-20 18:30:22 +00:00
} ;
2021-11-02 07:32:17 -07:00
marked . walkTokens = function ( tokens , callback ) {
2023-06-10 03:13:30 +00:00
return markedInstance . walkTokens ( tokens , callback ) ;
2020-05-14 15:54:39 +00:00
} ;
2023-06-10 03:13:30 +00:00
marked . parseInline = markedInstance . parseInline ;
2023-07-29 08:31:34 +02:00
marked . Parser = _Parser ;
marked . parser = _Parser . parse ;
marked . Renderer = _Renderer ;
marked . TextRenderer = _TextRenderer ;
marked . Lexer = _Lexer ;
marked . lexer = _Lexer . lex ;
marked . Tokenizer = _Tokenizer ;
marked . Slugger = _Slugger ;
marked . Hooks = _Hooks ;
2021-11-02 07:32:17 -07:00
marked . parse = marked ;
2023-07-29 08:31:34 +02:00
var options = marked . options ;
var setOptions = marked . setOptions ;
var use = marked . use ;
var walkTokens = marked . walkTokens ;
var parseInline = marked . parseInline ;
var parse = marked ;
var parser = _Parser . parse ;
var lexer = _Lexer . lex ;
export {
_Hooks as Hooks ,
_Lexer as Lexer ,
Marked ,
_Parser as Parser ,
_Renderer as Renderer ,
_Slugger as Slugger ,
_TextRenderer as TextRenderer ,
_Tokenizer as Tokenizer ,
_defaults as defaults ,
_getDefaults as getDefaults ,
lexer ,
marked ,
options ,
parse ,
parseInline ,
parser ,
setOptions ,
use ,
walkTokens
} ;
//# sourceMappingURL=marked.esm.js.map