Skip to content

Add comment #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 17, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 35 additions & 4 deletions src/convert-dom-to-markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ import {
isTextElement,
} from "./utils";

/**
* Converts an array of DOM nodes into a Markdown string.
* @param nodes - Array of DOM nodes to convert.
* @param image - Image conversion options.
* @param markStyle - Style options for Markdown formatting.
* @returns The generated Markdown string.
*/
const convertDOMToMarkdown = ({
nodes,
image,
Expand Down Expand Up @@ -53,14 +60,20 @@ const convertDOMToMarkdown = ({
};

/**
* Get text in DOMNode
* Converts a text node into a Markdown string.
* @param node - The text node to convert.
* @returns The text content as a string.
*/
const convertTextNode = (node: Text): string => {
return node.data;
};

/**
* Converts HTML tags to marks
* Converts an HTML element node into Markdown based on its type.
* @param node - The HTML element node to convert.
* @param image - Image conversion options.
* @param markStyle - Style options for Markdown formatting.
* @returns The generated Markdown string for the element.
*/
const convertTagNode = (
node: Element,
Expand Down Expand Up @@ -202,7 +215,11 @@ const convertTagNode = (
};

/**
* Recursively process convertDOMToMarkdown to get the markdown string
* Recursively processes DOM nodes to generate a Markdown string.
* @param node - The parent DOM element.
* @param image - Image conversion options.
* @param markStyle - Style options for Markdown formatting.
* @returns The concatenated Markdown string for the children.
*/
const getRecursionMarks = (
node: Element,
Expand All @@ -228,14 +245,22 @@ const getRecursionMarks = (
};

/**
* Get child node class.
* Extracts the class names of a node's children.
* @param node - The DOM element.
* @returns An array of class names.
*/
const getChildNodeClass = (node: Element) => {
return node.children
.map((child) => (child.type === "tag" ? child.attribs.class : undefined))
.filter(Boolean);
};

/**
* Builds an image URL with optional size and query parameters.
* @param node - The image DOM element.
* @param image - Image conversion options (size and query parameters).
* @returns The constructed image URL.
*/
const buildImageUrl = (
node: Element,
image: { size?: boolean; query?: string },
Expand All @@ -259,6 +284,12 @@ const buildImageUrl = (
return url.href;
};

/**
* Calculates the nesting depth of a list element.
* @param node - The list element.
* @param currentDepth - The current depth (default is 0).
* @returns The calculated depth.
*/
const findDepth = (node: Element, currentDepth = 0): number => {
const depth = currentDepth;

Expand Down
Loading