Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Dan gitbook fix 1#1247

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
chillenberger merged 4 commits intomasterfromDan-gitbook-fix-1
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from1 commit
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
PrevPrevious commit
NextNext commit
add tests remove parse html on start
  • Loading branch information
@chillenberger
chillenberger committedDec 20, 2023
commit87b02b190508e447a9fed7d0c9a1c632989f24f5
1 change: 0 additions & 1 deletionpgml-dashboard/.gitignore
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -4,4 +4,3 @@ search_index
.DS_Store
.DS_Store/
node_modules
cms/
99 changes: 86 additions & 13 deletionspgml-dashboard/src/api/cms.rs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -151,17 +151,18 @@ impl Collection {
pub async fn get_content(
&self,
mut path: PathBuf,
doc_type: &str,
cluster: &Cluster,
origin: &Origin<'_>,
) -> Document {
) -> Result<ResponseOk, Status> {
info!("get_content: {} | {path:?}", self.name);

if origin.path().ends_with("/") {
path = path.join("README");
}

let path =PathBuf::from(format!("cms/{}/{}.json",doc_type,path.display()));
let path =self.root_dir.join(format!("{}.md", path.to_string_lossy()));

let content = std::fs::read_to_string(path).unwrap();
serde_json::from_str(&content).unwrap()
self.render(&path, cluster).await
}

/// Create an index of the Collection based on the SUMMARY.md from Gitbook.
Expand DownExpand Up@@ -256,7 +257,7 @@ impl Collection {
}

// Sets specified index as currently viewed.
pubfn open_index(&self, path: PathBuf) -> Vec<IndexLink> {
fn open_index(&self, path: PathBuf) -> Vec<IndexLink> {
self.index
.clone()
.iter_mut()
Expand All@@ -268,7 +269,9 @@ impl Collection {
.collect()
}

async fn render<'a>(&self, doc: Document, cluster: &Cluster) -> Result<ResponseOk, Status> {
// renders document in layout
async fn render<'a>(&self, path: &'a PathBuf, cluster: &Cluster) -> Result<ResponseOk, Status> {
let doc = Document::from_path(&path).await.unwrap();
let index = self.open_index(doc.path);

let user = if cluster.context.user.is_anonymous() {
Expand DownExpand Up@@ -334,8 +337,7 @@ async fn get_blog(
cluster: &Cluster,
origin: &Origin<'_>,
) -> Result<ResponseOk, Status> {
let doc = BLOG.get_content(path.clone(), "blog", origin).await;
BLOG.render(doc, cluster).await
BLOG.get_content(path, cluster, origin).await
}

#[get("/careers/<path..>", rank = 5)]
Expand All@@ -344,8 +346,7 @@ async fn get_careers(
cluster: &Cluster,
origin: &Origin<'_>,
) -> Result<ResponseOk, Status> {
let doc = CAREERS.get_content(path, "careers", origin).await;
CAREERS.render(doc, cluster).await
CAREERS.get_content(path, cluster, origin).await
}

#[get("/docs/<path..>", rank = 5)]
Expand All@@ -354,8 +355,7 @@ async fn get_docs(
cluster: &Cluster,
origin: &Origin<'_>,
) -> Result<ResponseOk, Status> {
let doc = DOCS.get_content(path.clone(), "docs", origin).await;
DOCS.render(doc, cluster).await
DOCS.get_content(path, cluster, origin).await
}

pub fn routes() -> Vec<Route> {
Expand All@@ -374,6 +374,10 @@ pub fn routes() -> Vec<Route> {
mod test {
use super::*;
use crate::utils::markdown::{options, MarkdownHeadings, SyntaxHighlighter};
use regex::Regex;
use rocket::http::{ContentType, Cookie, Status};
use rocket::local::asynchronous::Client;
use rocket::{Build, Rocket};

#[test]
fn test_syntax_highlighting() {
Expand DownExpand Up@@ -461,4 +465,73 @@ This is the end of the markdown
!html.contains(r#"<div class="overflow-auto w-100">"#) || !html.contains(r#"</div>"#)
);
}

async fn rocket() -> Rocket<Build> {
dotenv::dotenv().ok();
rocket::build()
.manage(crate::utils::markdown::SearchIndex::open().unwrap())
.mount("/", crate::api::cms::routes())
}

fn gitbook_test(html: String) -> Option<String> {
// all gitbook expresions should be removed, this catches {% %} nonsupported expressions.
let re = Regex::new(r"[{][%][^{]*[%][}]").unwrap();
let rsp = re.find(&html);
if rsp.is_some() {
return Some(rsp.unwrap().as_str().to_string());
}

// gitbook TeX block not supported yet
let re = Regex::new(r"(\$\$).*(\$\$)").unwrap();
let rsp = re.find(&html);
if rsp.is_some() {
return Some(rsp.unwrap().as_str().to_string());
}

None
}

// Ensure blogs render and there are no unparsed gitbook components.
#[sqlx::test]
async fn render_blogs_test() {
let client = Client::tracked(rocket().await).await.unwrap();
let blog: Collection = Collection::new("Blog", true);

for path in blog.index {
let req = client.get(path.clone().href);
let rsp = req.dispatch().await;
let body = rsp.into_string().await.unwrap();

let test = gitbook_test(body);

assert!(
test.is_none(),
"bad html parse in {:?}. This feature is not supported {:?}",
path.href,
test.unwrap()
)
}
}

// Ensure Docs render and ther are no unparsed gitbook compnents.
#[sqlx::test]
async fn render_guides_test() {
let client = Client::tracked(rocket().await).await.unwrap();
let docs: Collection = Collection::new("Docs", true);

for path in docs.index {
let req = client.get(path.clone().href);
let rsp = req.dispatch().await;
let body = rsp.into_string().await.unwrap();

let test = gitbook_test(body);

assert!(
test.is_none(),
"bad html parse in {:?}. This feature is not supported {:?}",
path.href,
test.unwrap()
)
}
}
}
2 changes: 0 additions & 2 deletionspgml-dashboard/src/main.rs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -100,8 +100,6 @@ async fn main() {

markdown::SearchIndex::build().await.unwrap();

markdown::CmsParse::build().await;

pgml_dashboard::migrate(guards::Cluster::default(None).pool())
.await
.unwrap();
Expand Down
53 changes: 0 additions & 53 deletionspgml-dashboard/src/utils/markdown.rs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -26,9 +26,6 @@ use tantivy::tokenizer::{LowerCaser, NgramTokenizer, TextAnalyzer};
use tantivy::{Index, IndexReader, SnippetGenerator};
use url::Url;

use tokio::fs::File;
use tokio::io::AsyncWriteExt;

use std::fmt;

pub struct MarkdownHeadings {
Expand DownExpand Up@@ -1614,56 +1611,6 @@ impl SearchIndex {
}
}

pub struct CmsParse {}

impl CmsParse {
pub async fn build() {
let docs = Self::documents();

for document in docs {
let path = document.clone().with_extension("json");
let path = path
.strip_prefix(config::cms_dir().display().to_string())
.expect(&format!("{:?} is not a prefic of path", config::cms_dir()));
let path = Path::new("cms").join(path);

let doc = crate::api::cms::Document::from_path(&document)
.await
.unwrap();
let data = serde_json::to_string(&doc).expect("Failed to convert document to json.");

let _ = tokio::fs::create_dir_all(path.parent().unwrap().display().to_string()).await;
let mut file = File::create(path.display().to_string())
.await
.expect("Failed to create document File.");

file.write_all(data.as_bytes())
.await
.expect("Failed to write file.");
}
}

pub fn documents() -> Vec<PathBuf> {
// TODO imrpove this .display().to_string()
let guides = glob::glob(&config::cms_dir().join("docs/**/*.md").display().to_string())
.expect("glob failed");
let blogs = glob::glob(&config::cms_dir().join("blog/**/*.md").display().to_string())
.expect("glob failed");
let careers = glob::glob(
&config::cms_dir()
.join("careers/**/*.md")
.display()
.to_string(),
)
.expect("glob failed");
guides
.chain(blogs)
.chain(careers)
.map(|path| path.expect("glob path failed"))
.collect()
}
}

#[cfg(test)]
mod test {
use super::*;
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp