diff options
Diffstat (limited to 'content/frontend/shared/dom_parse_toc.js')
-rw-r--r-- | content/frontend/shared/dom_parse_toc.js | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/content/frontend/shared/dom_parse_toc.js b/content/frontend/shared/dom_parse_toc.js new file mode 100644 index 00000000..552550a1 --- /dev/null +++ b/content/frontend/shared/dom_parse_toc.js @@ -0,0 +1,49 @@ +/* eslint-disable import/prefer-default-export */ +import { findChildByTagName } from './dom'; + +const TAG_LI = 'LI'; +const TAG_A = 'A'; +const TAG_UL = 'UL'; + +/** + * Parses the given HTML Table of Contents into a data structure + * + * ``` + * type Item = { text: String, href: String, id: String, items: Item[] } + * + * parseTOC: Element => Item[] + * ``` + * + * @param {Element} menu Parent <ul> element + */ +export const parseTOC = menu => { + const items = []; + + if (!menu) { + return items; + } + + menu.childNodes.forEach(li => { + if (li.tagName !== TAG_LI) { + return; + } + + const link = findChildByTagName(li, TAG_A); + const subMenu = findChildByTagName(li, TAG_UL); + + if (!link) { + return; + } + + const item = { + text: link.textContent, + href: link.getAttribute('href'), + id: link.id, + items: parseTOC(subMenu), + }; + + items.push(item); + }); + + return items; +}; |