Files
koodo-reader/src/utils/htmlParser.tsx
troyeguo 10ebd4f5ab fix bug
Former-commit-id: 924785aa776d93c4e0235e57ba1de960ee6d2633
2021-06-13 17:14:13 +08:00

37 lines
1.0 KiB
TypeScript

class HtmlParser {
bookDoc: any;
contentList: HTMLElement[];
contentTitleList: any[];
constructor(bookDoc: any) {
this.bookDoc = bookDoc;
this.contentList = [];
this.contentTitleList = [];
this.getContent(bookDoc);
}
getContent(bookDoc: HTMLElement) {
this.contentList = Array.from(
bookDoc.querySelectorAll("h1,h2,h3,h4,h5,font")
);
for (let i = 0; i < this.contentList.length; i++) {
let random = Math.floor(Math.random() * 900) + 100;
this.contentTitleList.push({
label: this.contentList[i].innerText,
id: this.contentList[i].innerText.replaceAll(" ", "_") + random,
href: "#" + this.contentList[i].innerText.replaceAll(" ", "_") + random,
subitems: [],
});
}
for (let i = 0; i < this.contentList.length; i++) {
this.contentList[i].id = this.contentTitleList[i].id;
}
}
getAnchoredDoc() {
return this.bookDoc;
}
getContentList() {
return this.contentTitleList;
}
}
export default HtmlParser;