Skip to content

Commit

Permalink
fix(fm): fix frontmatter formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
fiji-flo committed Oct 2, 2024
1 parent 4c5158e commit a40907a
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 4 deletions.
63 changes: 61 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions crates/rari-doc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ indexmap.workspace = true

serde_yaml_ng = "0.10"
yaml-rust = "0.4"
pretty_yaml = "0.5"
yaml_parser = "0.2"
percent-encoding = "2"
pest = "2"
pest_derive = "2"
Expand Down
2 changes: 2 additions & 0 deletions crates/rari-doc/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ pub enum DocError {
NoFrontmatter,
#[error("Invalid frontmatter: {0}")]
InvalidFrontmatter(#[from] serde_yaml_ng::Error),
#[error("Invalid frontmatter to format: {0}")]
InvalidFrontmatterToFmt(#[from] yaml_parser::SyntaxError),
#[error(transparent)]
EnvError(#[from] EnvError),
#[error(transparent)]
Expand Down
22 changes: 20 additions & 2 deletions crates/rari-doc/src/pages/types/doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::io::{BufWriter, Write};
use std::path::{Path, PathBuf};
use std::sync::Arc;

use pretty_yaml::config::{FormatOptions, LanguageOptions};
use rari_md::m2h;
use rari_types::fm_types::{FeatureStatus, PageType};
use rari_types::locale::Locale;
Expand Down Expand Up @@ -341,17 +342,34 @@ fn write_doc(doc: &Doc) -> Result<(), DocError> {
if let Some(parent) = file_path.parent() {
std::fs::create_dir_all(parent)?;
}
let fm_str = fm_to_string(&frontmatter)?;

let file = fs::File::create(&file_path)?;
let mut buffer = BufWriter::new(file);
buffer.write_all(b"---\n")?;
serde_yaml_ng::to_writer(&mut buffer, &frontmatter)?;
buffer.write_all(fm_str.as_bytes())?;
buffer.write_all(b"---\n")?;

buffer.write_all(doc.raw[content_start..].as_bytes())?;

Ok(())
}

fn fm_to_string(fm: &FrontMatter) -> Result<String, DocError> {
let fm_str = serde_yaml_ng::to_string(fm)?;
Ok(pretty_yaml::format_text(
&fm_str,
&FormatOptions {
language: LanguageOptions {
quotes: pretty_yaml::config::Quotes::PreferDouble,
indent_block_sequence_in_map: true,
..Default::default()
},
..Default::default()
},
)?)
}

pub fn render_md_to_html(input: &str, locale: Locale) -> Result<String, DocError> {
let html = m2h(input, locale)?;
Ok(html)
Expand Down Expand Up @@ -397,7 +415,7 @@ mod tests {
let meta = serde_yaml_ng::from_str::<FrontMatter>(fm).unwrap();
assert_eq!(meta.browser_compat.len(), 2);

assert_eq!(fm, serde_yaml_ng::to_string(&meta).unwrap());
assert_eq!(fm, fm_to_string(&meta).unwrap());

let fm = r#"
browser-compat: foo
Expand Down

0 comments on commit a40907a

Please sign in to comment.