@@ -2,7 +2,6 @@ import { NodeHtmlMarkdownOptions } from './options';
2
2
import { ElementNode , HtmlNode } from './nodes' ;
3
3
import { nodeHtmlParserConfig } from './config' ;
4
4
5
-
6
5
/* ****************************************************************************************************************** */
7
6
// region: String Utils
8
7
/* ****************************************************************************************************************** */
@@ -123,6 +122,9 @@ export const truthyStr = (v: any, value?: string): string => v ? ((value !== und
123
122
/* ****************************************************************************************************************** */
124
123
// region: Parser
125
124
/* ****************************************************************************************************************** */
125
+ // For esbuild removing code
126
+ declare global { const IS_BROWSER : boolean }
127
+
126
128
127
129
function tryParseWithNativeDom ( html : string ) : ElementNode | undefined {
128
130
try {
@@ -169,24 +171,25 @@ const getNodeHtmlParser = () => {
169
171
export function parseHTML ( html : string , options : NodeHtmlMarkdownOptions ) : ElementNode {
170
172
let nodeHtmlParse : ReturnType < typeof getNodeHtmlParser > ;
171
173
172
- /* If specified, try to parse with native engine, fallback to node-html-parser */
173
174
perfStart ( 'parse' ) ;
174
175
let el : ElementNode | undefined ;
175
- if ( options . preferNativeParser ) {
176
+ /* If specified, try to parse with native engine, fallback to node-html-parser */
177
+ if ( IS_BROWSER || options . preferNativeParser ) {
176
178
try {
177
179
el = tryParseWithNativeDom ( html ) ;
178
180
}
179
181
catch ( e ) {
182
+ if ( IS_BROWSER ) throw e ;
180
183
nodeHtmlParse = getNodeHtmlParser ( ) ;
181
184
if ( nodeHtmlParse ) console . warn ( 'Native DOM parser encountered an error during parse' , e ) ;
182
185
else throw e ;
183
186
}
184
187
} else nodeHtmlParse = getNodeHtmlParser ( ) ;
185
188
186
- if ( ! el ) el = nodeHtmlParse ! ( html , nodeHtmlParserConfig ) ;
189
+ if ( ! IS_BROWSER && ! el ) el = nodeHtmlParse ! ( html , nodeHtmlParserConfig ) ;
187
190
perfStop ( 'parse' ) ;
188
191
189
- return el ;
192
+ return el ! ;
190
193
}
191
194
192
195
// endregion
0 commit comments