A node responsible for converting HTML content to Markdown format. This node takes HTML content from the state and converts it to clean, readable Markdown. It uses the convert_to_md utility function to perform the conversion. Attributes: verbose (bool): A flag indicating w
| 9 | |
| 10 | |
| 11 | class MarkdownifyNode(BaseNode): |
| 12 | """ |
| 13 | A node responsible for converting HTML content to Markdown format. |
| 14 | |
| 15 | This node takes HTML content from the state and converts it to clean, readable Markdown. |
| 16 | It uses the convert_to_md utility function to perform the conversion. |
| 17 | |
| 18 | Attributes: |
| 19 | verbose (bool): A flag indicating whether to show print statements during execution. |
| 20 | |
| 21 | Args: |
| 22 | input (str): Boolean expression defining the input keys needed from the state. |
| 23 | output (List[str]): List of output keys to be updated in the state. |
| 24 | node_config (Optional[dict]): Additional configuration for the node. |
| 25 | node_name (str): The unique identifier name for the node, defaulting to "Markdownify". |
| 26 | """ |
| 27 | |
| 28 | def __init__( |
| 29 | self, |
| 30 | input: str, |
| 31 | output: List[str], |
| 32 | node_config: Optional[dict] = None, |
| 33 | node_name: str = "Markdownify", |
| 34 | ): |
| 35 | super().__init__(node_name, "node", input, output, 1, node_config) |
| 36 | |
| 37 | self.verbose = ( |
| 38 | False if node_config is None else node_config.get("verbose", False) |
| 39 | ) |
| 40 | |
| 41 | def execute(self, state: dict) -> dict: |
| 42 | """ |
| 43 | Executes the node's logic to convert HTML content to Markdown. |
| 44 | |
| 45 | Args: |
| 46 | state (dict): The current state of the graph. The input keys will be used to fetch the |
| 47 | HTML content from the state. |
| 48 | |
| 49 | Returns: |
| 50 | dict: The updated state with the output key containing the Markdown content. |
| 51 | |
| 52 | Raises: |
| 53 | KeyError: If the input keys are not found in the state, indicating that the |
| 54 | necessary HTML content is missing. |
| 55 | """ |
| 56 | self.logger.info(f"--- Executing {self.node_name} Node ---") |
| 57 | |
| 58 | input_keys = self.get_input_keys(state) |
| 59 | html_content = state[input_keys[0]] |
| 60 | |
| 61 | # Convert HTML to Markdown |
| 62 | markdown_content = convert_to_md(html_content) |
| 63 | |
| 64 | # Update state with markdown content |
| 65 | state.update({self.output[0]: markdown_content}) |
| 66 | |
| 67 | return state |