forked from getzola/zola
-
Notifications
You must be signed in to change notification settings - Fork 0
feat(graph): zola graph migrate (once) + refresh (local) CLI #2
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 hidden or 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 hidden or 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 hidden or 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,162 @@ | ||
| //! Firecrawl fetcher — **migrate-only**. Only `migrate.rs` imports this module; | ||
| //! `refresh.rs` must not (hard rule: Firecrawl never on refresh). | ||
| //! | ||
| //! Wraps Firecrawl's `/v1/scrape` endpoint with `formats:["markdown"]` so the | ||
| //! page body arrives as markdown. The [`PageFetcher`] trait lets unit tests | ||
| //! inject a [`MockFetcher`] without touching the network or a real API key. | ||
|
|
||
| use std::time::Duration; | ||
|
|
||
| use errors::{Result, anyhow, bail}; | ||
| use reqwest::blocking::Client; | ||
| use serde_json::{json, Value}; | ||
|
|
||
| use super::html_to_md; | ||
|
|
||
| const FIRECRAWL_URL: &str = "https://api.firecrawl.dev/v1/scrape"; | ||
|
|
||
| /// One fetched remote page. `markdown` is what gets written to disk. | ||
| #[derive(Clone, Debug, PartialEq, Eq)] | ||
| pub struct FetchedPage { | ||
| pub url: String, | ||
| pub title: String, | ||
| pub markdown: String, | ||
| } | ||
|
|
||
| /// Page fetch abstraction so tests mock without Firecrawl/a key. | ||
| pub trait PageFetcher { | ||
| fn fetch(&self, url: &str) -> Result<FetchedPage>; | ||
| } | ||
|
|
||
| /// Live Firecrawl client. `FIRECRAWL_API_KEY` is required. | ||
| pub struct FirecrawlFetcher { | ||
| api_key: String, | ||
| client: Client, | ||
| } | ||
|
|
||
| impl FirecrawlFetcher { | ||
| pub fn new(api_key: String) -> Result<Self> { | ||
| let client = Client::builder() | ||
| .timeout(Duration::from_secs(90)) | ||
| .user_agent("zola-graph/0.1 (firecrawl)") | ||
| .build()?; | ||
| Ok(Self { api_key, client }) | ||
| } | ||
| } | ||
|
|
||
| impl PageFetcher for FirecrawlFetcher { | ||
| fn fetch(&self, url: &str) -> Result<FetchedPage> { | ||
| let payload = json!({ | ||
| "url": url, | ||
| "formats": ["markdown"], | ||
| "onlyMainContent": true, | ||
| }); | ||
| let body = serde_json::to_vec(&payload)?; | ||
| let resp = self | ||
| .client | ||
| .post(FIRECRAWL_URL) | ||
| .bearer_auth(&self.api_key) | ||
| .header(reqwest::header::CONTENT_TYPE, "application/json") | ||
| .body(body) | ||
| .send()?; | ||
| let status = resp.status(); | ||
| let text = resp.text()?; | ||
| if !status.is_success() { | ||
| bail!("Firecrawl HTTP {status}: {}", take200(&text)); | ||
| } | ||
| let data: Value = serde_json::from_str(&text) | ||
| .map_err(|e| anyhow!("Firecrawl non-JSON response: {e}"))?; | ||
| let inner = &data["data"]; | ||
| let md = inner["markdown"].as_str().unwrap_or("").to_string(); | ||
| let mut title = inner["metadata"]["title"] | ||
| .as_str() | ||
| .unwrap_or("") | ||
| .to_string(); | ||
| // markdown fallback: some sites return html only — convert then. | ||
| let (markdown, html) = if md.trim().is_empty() { | ||
| let html = inner["html"].as_str().unwrap_or("").to_string(); | ||
| (html_to_md::html_to_markdown(&html), html) | ||
| } else { | ||
| (md, String::new()) | ||
| }; | ||
| if title.is_empty() && !html.is_empty() { | ||
| title = html_to_md::extract_title(&html); | ||
| } | ||
| if markdown.trim().is_empty() { | ||
| bail!("Firecrawl: empty body for {url}"); | ||
| } | ||
| Ok(FetchedPage { | ||
| url: url.to_string(), | ||
| title, | ||
| markdown, | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| /// In-memory fetcher for tests and offline runs. | ||
| #[cfg(test)] | ||
| pub struct MockFetcher { | ||
| pages: std::collections::HashMap<String, FetchedPage>, | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| impl MockFetcher { | ||
| pub fn new() -> Self { | ||
| Self { pages: std::collections::HashMap::new() } | ||
| } | ||
| pub fn with(mut self, url: &str, title: &str, markdown: &str) -> Self { | ||
| self.pages.insert( | ||
| url.to_string(), | ||
| FetchedPage { | ||
| url: url.to_string(), | ||
| title: title.to_string(), | ||
| markdown: markdown.to_string(), | ||
| }, | ||
| ); | ||
| self | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| impl Default for MockFetcher { | ||
| fn default() -> Self { | ||
| Self::new() | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| impl PageFetcher for MockFetcher { | ||
| fn fetch(&self, url: &str) -> Result<FetchedPage> { | ||
| self.pages | ||
| .get(url) | ||
| .cloned() | ||
| .ok_or_else(|| anyhow!("MockFetcher: no page for {url}")) | ||
| } | ||
| } | ||
|
|
||
| fn take200(s: &str) -> String { | ||
| s.chars().take(200).collect() | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn mock_fetcher_returns_registered_page() { | ||
| let f = MockFetcher::new().with( | ||
| "https://x/a", | ||
| "A", | ||
| "# A\n\nBody of a.\n", | ||
| ); | ||
| let p = f.fetch("https://x/a").unwrap(); | ||
| assert_eq!(p.title, "A"); | ||
| assert!(p.markdown.contains("Body of a.")); | ||
| } | ||
|
|
||
| #[test] | ||
| fn mock_fetcher_missing_errors() { | ||
| let f = MockFetcher::new(); | ||
| assert!(f.fetch("https://nope").is_err()); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win
🧩 Analysis chain
🌐 Web query:
Firecrawl v1 scrape endpoint formats parameter markdown html response data fields💡 Result:
In the Firecrawl v1
/scrapeendpoint, theformatsparameter is an array used to specify the desired output content types [1][2]. If not provided, it defaults to["markdown"][3]. When you request specific formats, the response object returns data in fields corresponding to those formats [2][4]. Below is a summary of the common format parameters and their associated response data fields: Supported Formats and Response Fields: - Markdown: Usemarkdownin theformatsarray; the result is returned in themarkdownfield [1][2][4]. - HTML: Usehtmlin theformatsarray to receive cleaned HTML (scripts/styles removed) in thehtmlfield [2][4]. - Raw HTML: UserawHtmlin theformatsarray to receive the unmodified page source in theraw_htmlfield [2][4]. - Links: Uselinksto receive a list of links found on the page in thelinksfield [2][4]. - Screenshot: Usescreenshot(orscreenshot@fullPage) to receive a signed URL in thescreenshotfield [1][5][3]. - JSON: Usejson(often with an object containingschemaandprompt) to receive structured data in thejsonfield [2][3][4]. - Summary: Usesummaryto receive an AI-generated summary in thesummaryfield [2][3][4]. - Additional formats: Supported formats also includeimages,branding,product,audio,video, andchangeTracking(which requiresmarkdownto be enabled) [5][3][4]. Important Notes: - The response object also consistently includes metadata fields such asmetadata,metadata_dict, andmetadata_typed[4]. - Fields corresponding to formats not requested in theformatsparameter will typically be returned asnull[4]. - For object-based formats likejson,screenshot, orchangeTracking, you pass an object in theformatsarray containing the relevant configuration (e.g.,schema,prompt,fullPage) instead of just a string [3].Citations:
🏁 Script executed:
Repository: curriculo-tech/zola
Length of output: 13688
🏁 Script executed:
Repository: curriculo-tech/zola
Length of output: 30698
🏁 Script executed:
Repository: curriculo-tech/zola
Length of output: 311
🏁 Script executed:
Repository: curriculo-tech/zola
Length of output: 311
Request both
markdownandhtmlformats, and use the HTML for title fallback.With
formats: ["markdown"], Firecrawl does not provide a usabledata.html. Whendata.markdownis empty, the fallback produces an empty body andfetchreturnsFirecrawl: empty body. Also,extract_titleis skipped whenever markdown is present because the HTML value is discarded. A missingmetadata.titlecan therefore still write an empty title to front matter.🤖 Prompt for AI Agents