')) return html;
const stack: string[] = [];
const output: string[] = [];
let inFirstParagraph = true;
let canSimplify = true;
const parser = new htmlparser2.Parser({
onopentag: (name: string, attrs: Record
. Don't simplify if these elements
// are present.
} else if (!['div', 'style', 'span'].includes(name)) {
canSimplify = false;
}
},
});
parser.write(html);
parser.end();
return canSimplify ? output.join('') : html;
};
export const htmlDocIsImageOnly = (html: string) => {
let imageCount = 0;
let nonImageFound = false;
let textFound = false;
// Ignore these tags that do not result in any Markdown (or HTML) code being generated.
const ignoredTags = ['meta', 'head', 'body', 'html'];
const parser = new htmlparser2.Parser({
onopentag: (name: string) => {
if (name === 'img') {
imageCount++;
} else if (ignoredTags.includes(name)) {
// Skip
} else {
nonImageFound = true;
}
},
ontext: (text: string) => {
if (text.trim()) textFound = true;
},
});
parser.write(html);
parser.end();
return imageCount === 1 && !nonImageFound && !textFound;
};
export default new HtmlUtils();