diff --git a/fastn-core/src/apis/cr.rs b/fastn-core/src/apis/cr.rs index a147926d70..7b131387ce 100644 --- a/fastn-core/src/apis/cr.rs +++ b/fastn-core/src/apis/cr.rs @@ -49,7 +49,6 @@ async fn create_cr_page_worker( req: fastn_core::http::Request, ) -> fastn_core::Result { let config = fastn_core::Config::read(None, false).await?; - let mut req_config = fastn_core::RequestConfig::new(&config, &req); let create_cr_ftd = fastn_core::package_info_create_cr(&config)?; let main_document = fastn_core::Document { @@ -59,6 +58,9 @@ async fn create_cr_page_worker( package_name: config.package.name.clone(), }; + let mut req_config = + fastn_core::RequestConfig::new(&config, &req, main_document.id.as_str(), "/"); + fastn_core::package::package_doc::read_ftd(&mut req_config, &main_document, "/", false, false) .await .map(Into::into) diff --git a/fastn-core/src/apis/edit.rs b/fastn-core/src/apis/edit.rs index 60e72893ea..75f3ae04b6 100644 --- a/fastn-core/src/apis/edit.rs +++ b/fastn-core/src/apis/edit.rs @@ -32,7 +32,7 @@ pub async fn edit( Ok(config) => config, Err(err) => return fastn_core::http::api_error(err.to_string()), }; - let mut req_config = fastn_core::RequestConfig::new(&config, req); + let mut req_config = fastn_core::RequestConfig::new(&config, req, "edit.ftd", "/"); req_config.current_document = Some(req_data.path.to_string()); match req_config.can_write(req_data.path.as_str()).await { diff --git a/fastn-core/src/apis/edit_source.rs b/fastn-core/src/apis/edit_source.rs index 40b62c0ddd..ad3f060bd4 100644 --- a/fastn-core/src/apis/edit_source.rs +++ b/fastn-core/src/apis/edit_source.rs @@ -22,7 +22,7 @@ async fn handle_view_source( path: &str, ) -> fastn_core::Result> { let config = fastn_core::Config::read(None, false).await?; - let mut req_config = fastn_core::RequestConfig::new(&config, req); + let mut req_config = fastn_core::RequestConfig::new(&config, req, "editor-source.ftd", "/"); let file_name = config.get_file_path_and_resolve(path).await?; let file = req_config.get_file_and_package_by_id(path).await?; diff --git a/fastn-core/src/apis/view_source.rs b/fastn-core/src/apis/view_source.rs index 3aa2f2d7bd..bba3dcd823 100644 --- a/fastn-core/src/apis/view_source.rs +++ b/fastn-core/src/apis/view_source.rs @@ -22,7 +22,7 @@ async fn handle_view_source( path: &str, ) -> fastn_core::Result> { let config = fastn_core::Config::read(None, false).await?; - let mut req_config = fastn_core::RequestConfig::new(&config, req); + let mut req_config = fastn_core::RequestConfig::new(&config, req, "editor.ftd", "/"); let file_name = config.get_file_path_and_resolve(path).await?; let file = req_config.get_file_and_package_by_id(path).await?; diff --git a/fastn-core/src/commands/build.rs b/fastn-core/src/commands/build.rs index b674b96bc1..dc5b380fc7 100644 --- a/fastn-core/src/commands/build.rs +++ b/fastn-core/src/commands/build.rs @@ -594,7 +594,8 @@ async fn handle_file_( } let req = fastn_core::http::Request::default(); - let mut req_config = fastn_core::RequestConfig::new(config, &req); + let mut req_config = + fastn_core::RequestConfig::new(config, &req, doc.id.as_str(), base_url); req_config.current_document = Some(document.get_id().to_string()); let resp = fastn_core::package::package_doc::process_ftd( diff --git a/fastn-core/src/commands/serve.rs b/fastn-core/src/commands/serve.rs index 5f4155b2f5..8c69eca160 100644 --- a/fastn-core/src/commands/serve.rs +++ b/fastn-core/src/commands/serve.rs @@ -239,7 +239,7 @@ pub async fn serve( ) -> fastn_core::Result { let _lock = LOCK.read().await; - let mut req_config = fastn_core::RequestConfig::new(config, &req); + let mut req_config = fastn_core::RequestConfig::new(config, &req, "", "/"); let path: camino::Utf8PathBuf = req.path().replacen('/', "", 1).parse()?; diff --git a/fastn-core/src/config/mod.rs b/fastn-core/src/config/mod.rs index 7f24f1453f..982bf4d5a2 100644 --- a/fastn-core/src/config/mod.rs +++ b/fastn-core/src/config/mod.rs @@ -51,10 +51,22 @@ pub struct RequestConfig<'a> { pub dependencies_during_render: Vec, pub request: &'a fastn_core::http::Request, pub config: &'a Config, + /// If the current module being parsed is a markdown file, `.markdown` contains the name and + /// content of that file + pub markdown: Option<(String, String)>, + pub document_id: String, + pub translated_data: fastn_core::TranslationData, + pub base_url: String, + pub module_package_map: std::collections::BTreeMap, } impl<'a> RequestConfig<'a> { - pub fn new(config: &'a Config, request: &'a fastn_core::http::Request) -> Self { + pub fn new( + config: &'a Config, + request: &'a fastn_core::http::Request, + document_id: &str, + base_url: &str, + ) -> Self { RequestConfig { named_parameters: vec![], extra_data: Default::default(), @@ -63,6 +75,11 @@ impl<'a> RequestConfig<'a> { dependencies_during_render: vec![], request, config, + markdown: None, + document_id: document_id.to_string(), + translated_data: Default::default(), + base_url: base_url.to_string(), + module_package_map: Default::default(), } } diff --git a/fastn-core/src/doc.rs b/fastn-core/src/doc.rs index b6da5feab3..3dd32177f4 100644 --- a/fastn-core/src/doc.rs +++ b/fastn-core/src/doc.rs @@ -39,7 +39,7 @@ pub async fn interpret_helper<'a>( let mut s = ftd::interpreter::interpret_with_line_number(name, doc, line_number)?; lib.module_package_map.insert( name.trim_matches('/').to_string(), - lib.config.config.package.name.to_string(), + lib.config.package.name.to_string(), ); let document; loop { @@ -56,7 +56,7 @@ pub async fn interpret_helper<'a>( let (source, path, foreign_variable, foreign_function, ignore_line_numbers) = resolve_import_2022(lib, &mut st, module.as_str(), caller_module.as_str()) .await?; - lib.config.dependencies_during_render.push(path); + lib.dependencies_during_render.push(path); let doc = cached_parse(module.as_str(), source.as_str(), ignore_line_numbers)?; s = st.continue_after_import( module.as_str(), @@ -229,7 +229,6 @@ pub async fn resolve_import_2022<'a>( if module.starts_with(alias) { lib.push_package_under_process(module, package).await?; font_ftd = lib - .config .config .all_packages .borrow() @@ -326,7 +325,6 @@ pub async fn resolve_foreign_variable2022( if alias.eq(&package_name) { lib.push_package_under_process(doc_name, package).await?; let package = lib - .config .config .all_packages .borrow() @@ -355,7 +353,7 @@ pub async fn resolve_foreign_variable2022( module: &str, package: &fastn_core::Package, files: &str, - lib: &mut fastn_core::Library2022<'_>, + lib: &mut fastn_core::RequestConfig<'_>, base_url: &str, download_assets: bool, // true: in case of `fastn build` ) -> ftd::ftd2021::p1::Result { @@ -395,7 +393,6 @@ pub async fn resolve_foreign_variable2022( let light_path = format!("{}.{}", file.replace('.', "/"), ext); if download_assets && !lib - .config .downloaded_assets .contains_key(&format!("{}/{}", package.name, light_path)) { @@ -410,11 +407,7 @@ pub async fn resolve_foreign_variable2022( })?; print!("Processing {}/{} ... ", package.name.as_str(), light_path); fastn_core::utils::write( - &lib.config - .config - .build_dir() - .join("-") - .join(package.name.as_str()), + &lib.config.build_dir().join("-").join(package.name.as_str()), light_path.as_str(), light.as_slice(), ) @@ -424,7 +417,7 @@ pub async fn resolve_foreign_variable2022( doc_id: lib.document_id.to_string(), line_number: 0, })?; - lib.config.downloaded_assets.insert( + lib.downloaded_assets.insert( format!("{}/{}", package.name, light_path), light_mode.to_string(), ); @@ -457,7 +450,6 @@ pub async fn resolve_foreign_variable2022( if download_assets && !file.ends_with("-dark") { let start = std::time::Instant::now(); if let Some(dark) = lib - .config .downloaded_assets .get(&format!("{}/{}", package.name, dark_path)) { @@ -468,11 +460,7 @@ pub async fn resolve_foreign_variable2022( { print!("Processing {}/{} ... ", package.name.as_str(), dark_path); fastn_core::utils::write( - &lib.config - .config - .build_dir() - .join("-") - .join(package.name.as_str()), + &lib.config.build_dir().join("-").join(package.name.as_str()), dark_path.as_str(), dark.as_slice(), ) @@ -491,7 +479,7 @@ pub async fn resolve_foreign_variable2022( } else { dark_mode = light_mode.clone(); } - lib.config.downloaded_assets.insert( + lib.downloaded_assets.insert( format!("{}/{}", package.name, dark_path), dark_mode.to_string(), ); @@ -556,7 +544,6 @@ async fn download( ) -> ftd::ftd2021::p1::Result<()> { if download_assets && !lib - .config .downloaded_assets .contains_key(&format!("{}/{}", package.name, path)) { @@ -571,11 +558,7 @@ async fn download( })?; print!("Processing {}/{} ... ", package.name, path); fastn_core::utils::write( - &lib.config - .config - .build_dir() - .join("-") - .join(package.name.as_str()), + &lib.config.build_dir().join("-").join(package.name.as_str()), path, data.as_slice(), ) @@ -585,7 +568,7 @@ async fn download( doc_id: lib.document_id.to_string(), line_number: 0, })?; - lib.config.downloaded_assets.insert( + lib.downloaded_assets.insert( format!("{}/{}", package.name, path), format!("-/{}/{}", package.name, path), ); diff --git a/fastn-core/src/library/fastn_dot_ftd.rs b/fastn-core/src/library/fastn_dot_ftd.rs index b12c74200c..f74f03c514 100644 --- a/fastn-core/src/library/fastn_dot_ftd.rs +++ b/fastn-core/src/library/fastn_dot_ftd.rs @@ -826,7 +826,7 @@ pub(crate) async fn get2(lib: &fastn_core::Library2<'_>) -> String { pub(crate) async fn get2022(lib: &fastn_core::Library2022<'_>) -> String { let lib = fastn_core::Library { - config: lib.config.clone(), + config: lib.clone(), markdown: lib.markdown.clone(), document_id: lib.document_id.clone(), translated_data: lib.translated_data.clone(), diff --git a/fastn-core/src/library2022/mod.rs b/fastn-core/src/library2022/mod.rs index 8521d4b132..a2ae8c1f92 100644 --- a/fastn-core/src/library2022/mod.rs +++ b/fastn-core/src/library2022/mod.rs @@ -13,17 +13,7 @@ impl KeyValueData { } } -#[derive(Debug)] -pub struct Library2022<'m> { - pub config: &'m mut fastn_core::RequestConfig<'m>, - /// If the current module being parsed is a markdown file, `.markdown` contains the name and - /// content of that file - pub markdown: Option<(String, String)>, - pub document_id: String, - pub translated_data: fastn_core::TranslationData, - pub base_url: String, - pub module_package_map: std::collections::BTreeMap, -} +pub type Library2022<'m> = fastn_core::RequestConfig<'m>; impl Library2022<'_> { pub async fn get_with_result( @@ -52,7 +42,6 @@ impl Library2022<'_> { })?; self.config - .config .all_packages .borrow() .get(current_package_name) @@ -153,7 +142,7 @@ impl Library2022<'_> { lib: &mut fastn_core::Library2022<'_>, ) -> Option<(String, usize)> { lib.push_package_under_process(name, package).await.ok()?; - let packages = lib.config.config.all_packages.borrow(); + let packages = lib.config.all_packages.borrow(); let package = packages.get(package.name.as_str()).unwrap_or(package); // Explicit check for the current package. let name = format!("{}/", name.trim_end_matches('/')); @@ -162,11 +151,7 @@ impl Library2022<'_> { } let new_name = name.replacen(package.name.as_str(), "", 1); let (file_path, data) = package - .resolve_by_id( - new_name.as_str(), - None, - lib.config.config.package.name.as_str(), - ) + .resolve_by_id(new_name.as_str(), None, lib.config.package.name.as_str()) .await .ok()?; if !file_path.ends_with(".ftd") { @@ -191,7 +176,6 @@ impl Library2022<'_> { package.name.to_string(), ); if self - .config .config .all_packages .borrow() @@ -200,19 +184,15 @@ impl Library2022<'_> { return Ok(()); } - let package = self - .config - .config - .resolve_package(package) - .await - .map_err(|_| ftd::ftd2021::p1::Error::ParseError { + let package = self.config.resolve_package(package).await.map_err(|_| { + ftd::ftd2021::p1::Error::ParseError { message: format!("Cannot resolve the package: {}", package.name), doc_id: self.document_id.to_string(), line_number: 0, - })?; + } + })?; self.config - .config .all_packages .borrow_mut() .insert(package.name.to_string(), package); @@ -243,69 +223,52 @@ impl Library2022<'_> { "figma-cs-token-old" => { processor::figma_tokens::process_figma_tokens_old(value, kind, doc) } - "http" => processor::http::process(value, kind, doc, self.config).await, + "http" => processor::http::process(value, kind, doc, self).await, "tutor" => fastn_core::tutor::process(value, kind, doc).await, "toc" => processor::toc::process(value, kind, doc), - "get-data" => processor::get_data::process(value, kind, doc, self.config), - "sitemap" => processor::sitemap::process(value, kind, doc, self.config), - "full-sitemap" => { - processor::sitemap::full_sitemap_process(value, kind, doc, self.config) - } - "request-data" => processor::request_data::process(value, kind, doc, self.config), + "get-data" => processor::get_data::process(value, kind, doc, self), + "sitemap" => processor::sitemap::process(value, kind, doc, self), + "full-sitemap" => processor::sitemap::full_sitemap_process(value, kind, doc, self), + "request-data" => processor::request_data::process(value, kind, doc, self), "document-readers" => processor::document::process_readers( value, kind, doc, - self.config, + self, self.document_id.as_str(), ), "document-writers" => processor::document::process_writers( value, kind, doc, - self.config, + self, self.document_id.as_str(), ), - "user-groups" => processor::user_group::process(value, kind, doc, self.config), - "user-group-by-id" => { - processor::user_group::process_by_id(value, kind, doc, self.config) - } - "get-identities" => { - processor::user_group::get_identities(value, kind, doc, self.config) - } - "document-id" => processor::document::document_id(value, kind, doc, self.config), - "document-full-id" => { - processor::document::document_full_id(value, kind, doc, self.config) - } - "document-suffix" => { - processor::document::document_suffix(value, kind, doc, self.config) - } - "document-name" => { - processor::document::document_name(value, kind, doc, self.config).await - } - "fetch-file" => processor::fetch_file::fetch_files(value, kind, doc, self.config).await, - "user-details" => processor::user_details::process(value, kind, doc, self.config), - "fastn-apps" => processor::apps::process(value, kind, doc, self.config), - "is-reader" => processor::user_group::is_reader(value, kind, doc, self.config).await, - "sql" => processor::sql::process(value, kind, doc, self.config).await, + "user-groups" => processor::user_group::process(value, kind, doc, self), + "user-group-by-id" => processor::user_group::process_by_id(value, kind, doc, self), + "get-identities" => processor::user_group::get_identities(value, kind, doc, self), + "document-id" => processor::document::document_id(value, kind, doc, self), + "document-full-id" => processor::document::document_full_id(value, kind, doc, self), + "document-suffix" => processor::document::document_suffix(value, kind, doc, self), + "document-name" => processor::document::document_name(value, kind, doc, self).await, + "fetch-file" => processor::fetch_file::fetch_files(value, kind, doc, self).await, + "user-details" => processor::user_details::process(value, kind, doc, self), + "fastn-apps" => processor::apps::process(value, kind, doc, self), + "is-reader" => processor::user_group::is_reader(value, kind, doc, self).await, + "sql" => processor::sql::process(value, kind, doc, self).await, "package-query" => { processor::sqlite::process( value, kind, doc, - self.config, + self, &fastn_core::library2022::processor::sql::get_db_config()?, ) .await } "pg" => processor::pg::process(value, kind, doc).await, - "package-tree" => { - processor::package_tree::process(value, kind, doc, self.config.config).await - } - "query" => { - processor::query::process(value, kind, doc, self.config, self.document_id.as_str()) - .await - } + "package-tree" => processor::package_tree::process(value, kind, doc, self.config).await, + "query" => processor::query::process(value, kind, doc, self).await, t => Err(ftd::interpreter::Error::ParseError { doc_id: self.document_id.to_string(), line_number, diff --git a/fastn-core/src/library2022/processor/query.rs b/fastn-core/src/library2022/processor/query.rs index a1589a5b6f..bec69b00d8 100644 --- a/fastn-core/src/library2022/processor/query.rs +++ b/fastn-core/src/library2022/processor/query.rs @@ -3,7 +3,6 @@ pub async fn process( kind: ftd::interpreter::Kind, doc: &ftd::interpreter::TDoc<'_>, req_config: &mut fastn_core::RequestConfig<'_>, - document_id: &str, ) -> ftd::interpreter::Result { // TODO: document key should be optional @@ -14,7 +13,7 @@ pub async fn process( let path = headers .get_optional_string_by_key("file", doc.name, value.line_number())? - .unwrap_or_else(|| document_id.to_string()); + .unwrap_or_else(|| req_config.document_id.to_string()); let stage = headers .get_optional_string_by_key("stage", doc.name, value.line_number())? @@ -25,14 +24,14 @@ pub async fn process( .await .map_err(|e| ftd::interpreter::Error::ParseError { message: format!("Cannot get path: {} {:?}", path.as_str(), e), - doc_id: document_id.to_string(), + doc_id: req_config.document_id.to_string(), line_number: value.line_number(), })?; doc.from_json( &fastn_core::commands::query::get_ftd_json(&file, stage.as_str()).map_err(|e| { ftd::interpreter::Error::ParseError { message: format!("Cannot resolve json for path: {} {:?}", path.as_str(), e), - doc_id: document_id.to_string(), + doc_id: req_config.document_id.to_string(), line_number: value.line_number(), } })?, diff --git a/fastn-core/src/package/package_doc.rs b/fastn-core/src/package/package_doc.rs index 60a772ce42..7539527892 100644 --- a/fastn-core/src/package/package_doc.rs +++ b/fastn-core/src/package/package_doc.rs @@ -403,14 +403,8 @@ pub(crate) async fn read_ftd_2022<'o, 'm: 'o>( .unwrap_or(&config.config.package) .to_owned(); - let mut lib = fastn_core::Library2022 { - config, - markdown: None, - document_id: main.id.clone(), - translated_data: Default::default(), - base_url: base_url.to_string(), - module_package_map: Default::default(), - }; + config.document_id = main.id.clone(); + config.base_url = base_url.to_string(); // Get Prefix Body => [AutoImports + Actual Doc content] let mut doc_content = @@ -422,7 +416,7 @@ pub(crate) async fn read_ftd_2022<'o, 'm: 'o>( let main_ftd_doc = match fastn_core::doc::interpret_helper( main.id_with_package().as_str(), doc_content.as_str(), - &mut lib, + config, base_url, download_assets, line_number, @@ -478,14 +472,8 @@ pub(crate) async fn read_ftd_2023<'o, 'm: 'o>( .unwrap_or(&config.config.package) .to_owned(); - let mut lib = fastn_core::Library2022 { - config, - markdown: None, - document_id: main.id.clone(), - translated_data: Default::default(), - base_url: base_url.to_string(), - module_package_map: Default::default(), - }; + config.document_id = main.id.clone(); + config.base_url = base_url.to_string(); // Get Prefix Body => [AutoImports + Actual Doc content] let mut doc_content = @@ -497,7 +485,7 @@ pub(crate) async fn read_ftd_2023<'o, 'm: 'o>( let main_ftd_doc = match fastn_core::doc::interpret_helper( main.id_with_package().as_str(), doc_content.as_str(), - &mut lib, + config, base_url, download_assets, line_number,