[dyad] Implemented headline tree structure - wrote 4 file(s), deleted 1 file(s)
This commit is contained in:
@@ -7,10 +7,12 @@ interface FaqItem {
|
||||
answer: string;
|
||||
}
|
||||
|
||||
interface HeadlineItem {
|
||||
export interface HeadlineNode {
|
||||
tag: string;
|
||||
text: string;
|
||||
length: number;
|
||||
level: number;
|
||||
children: HeadlineNode[];
|
||||
}
|
||||
|
||||
export async function extractMetaData(url: string) {
|
||||
@@ -79,17 +81,35 @@ export async function extractMetaData(url: string) {
|
||||
}
|
||||
});
|
||||
|
||||
const headlines: HeadlineItem[] = [];
|
||||
const headlines: HeadlineNode[] = [];
|
||||
const path: HeadlineNode[] = [];
|
||||
|
||||
$("h1, h2, h3, h4, h5, h6").each((i, el) => {
|
||||
const tag = $(el).prop("tagName").toLowerCase();
|
||||
const text = $(el).text().trim();
|
||||
if (text) {
|
||||
headlines.push({
|
||||
tag,
|
||||
text,
|
||||
length: text.length,
|
||||
});
|
||||
if (!text) return;
|
||||
|
||||
const level = parseInt(tag.replace("h", ""), 10);
|
||||
|
||||
const node: HeadlineNode = {
|
||||
tag,
|
||||
text,
|
||||
length: text.length,
|
||||
level,
|
||||
children: [],
|
||||
};
|
||||
|
||||
while (path.length > 0 && path[path.length - 1].level >= level) {
|
||||
path.pop();
|
||||
}
|
||||
|
||||
if (path.length === 0) {
|
||||
headlines.push(node);
|
||||
} else {
|
||||
path[path.length - 1].children.push(node);
|
||||
}
|
||||
|
||||
path.push(node);
|
||||
});
|
||||
|
||||
return {
|
||||
|
||||
Reference in New Issue
Block a user