-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(integrations,transformers): add transformer for converting html …
…to markdown
- Loading branch information
Showing
5 changed files
with
141 additions
and
5 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
swiftide/src/integrations/scraping/html_to_markdown_transformer.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
use anyhow::Result; | ||
use async_trait::async_trait; | ||
use derive_builder::Builder; | ||
use htmd::HtmlToMarkdown; | ||
|
||
use crate::{ingestion::IngestionNode, Transformer}; | ||
|
||
#[derive(Builder)] | ||
#[builder(pattern = "owned")] | ||
/// Transforms HTML content into markdown. | ||
/// | ||
/// Useful for converting scraping results into markdown. | ||
pub struct HtmlToMarkdownTransformer { | ||
/// The `HtmlToMarkdown` instance used to convert HTML to markdown. | ||
/// | ||
/// Sets a sane default, but can be customized. | ||
htmd: HtmlToMarkdown, | ||
#[builder(default)] | ||
concurrency: Option<usize>, | ||
} | ||
|
||
impl Default for HtmlToMarkdownTransformer { | ||
fn default() -> Self { | ||
Self { | ||
htmd: HtmlToMarkdown::builder() | ||
.skip_tags(vec!["script", "style"]) | ||
.build(), | ||
concurrency: None, | ||
} | ||
} | ||
} | ||
|
||
impl HtmlToMarkdownTransformer { | ||
#[allow(dead_code)] | ||
pub fn builder() -> HtmlToMarkdownTransformerBuilder { | ||
HtmlToMarkdownTransformerBuilder::default() | ||
} | ||
} | ||
|
||
impl std::fmt::Debug for HtmlToMarkdownTransformer { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
f.debug_struct("HtmlToMarkdownTransformer").finish() | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl Transformer for HtmlToMarkdownTransformer { | ||
/// Converts the HTML content in the `IngestionNode` to markdown. | ||
/// | ||
/// Will Err the node if the conversion fails. | ||
async fn transform_node(&self, node: IngestionNode) -> Result<IngestionNode> { | ||
let chunk = self.htmd.convert(&node.chunk); | ||
Ok(IngestionNode { | ||
chunk: chunk?, | ||
..node | ||
}) | ||
} | ||
|
||
fn concurrency(&self) -> Option<usize> { | ||
self.concurrency | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::*; | ||
|
||
#[tokio::test] | ||
async fn test_html_to_markdown() { | ||
let node = IngestionNode::new("<h1>Hello, World!</h1>"); | ||
let transformer = HtmlToMarkdownTransformer::default(); | ||
let transformed = transformer.transform_node(node).await.unwrap(); | ||
assert_eq!(transformed.chunk, "# Hello, World!"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
mod html_to_markdown_transformer; | ||
mod loader; |