Skip to content

Commit

Permalink
fix: newline/blankline was sometimes missing after yaml header (#119)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret authored Aug 22, 2024
1 parent 5236994 commit f2eceb4
Show file tree
Hide file tree
Showing 9 changed files with 124 additions and 107 deletions.
10 changes: 2 additions & 8 deletions src/format_text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use super::configuration::Configuration;
use super::generation::file_has_ignore_file_directive;
use super::generation::generate;
use super::generation::parse_cmark_ast;
use super::generation::parse_yaml_header;
use super::generation::strip_metadata_header;
use super::generation::Context;

/// Formats a file.
Expand Down Expand Up @@ -81,14 +81,8 @@ enum ParseFileResult<'a> {
}

fn parse_source_file<'a>(file_text: &'a str, config: &Configuration) -> Result<ParseFileResult<'a>> {
let yaml_header = parse_yaml_header(file_text); // todo: improve... this is kind of hacked into here
let markdown_text = match &yaml_header {
Some(yaml_header) => &file_text[yaml_header.range.end..],
None => file_text,
};

// check for the presence of an dprint-ignore-file comment before parsing
if file_has_ignore_file_directive(markdown_text, &config.ignore_file_directive) {
if file_has_ignore_file_directive(strip_metadata_header(file_text), &config.ignore_file_directive) {
return Ok(ParseFileResult::IgnoreFile);
}

Expand Down
26 changes: 9 additions & 17 deletions src/generation/cmark/parse_cmark_ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ fn parse_start(start_tag: Tag, iterator: &mut EventIterator) -> Result<Node, Par
Tag::List(first_item_number) => parse_list(first_item_number, iterator).map(|x| x.into()),
Tag::Item => parse_item(iterator).map(|x| x.into()),
Tag::HtmlBlock => parse_html_block(iterator).map(|x| x.into()),
Tag::MetadataBlock(metadata_block_kind) => parse_metadata(metadata_block_kind, iterator),
Tag::MetadataBlock(metadata_block_kind) => parse_metadata(metadata_block_kind, iterator).map(|x| x.into()),
}
}

Expand Down Expand Up @@ -708,7 +708,7 @@ fn parse_item(iterator: &mut EventIterator) -> Result<Item, ParseError> {
})
}

fn parse_metadata(kind: MetadataBlockKind, iterator: &mut EventIterator) -> Result<Node, ParseError> {
fn parse_metadata(kind: MetadataBlockKind, iterator: &mut EventIterator) -> Result<MetadataBlock, ParseError> {
let start = iterator.get_last_range().start;
let mut text = String::new();
while let Some(event) = iterator.next() {
Expand All @@ -733,20 +733,12 @@ fn parse_metadata(kind: MetadataBlockKind, iterator: &mut EventIterator) -> Resu
}
}

Ok(match kind {
MetadataBlockKind::YamlStyle => Node::YamlHeader(YamlHeader {
range: Range {
start,
end: start + text.len(),
},
text,
}),
MetadataBlockKind::PlusesStyle => Node::PlusesHeader(PlusesHeader {
range: Range {
start,
end: start + text.len(),
},
text,
}),
Ok(MetadataBlock {
range: Range {
start,
end: start + text.len(),
},
kind,
text,
})
}
13 changes: 5 additions & 8 deletions src/generation/common/ast_nodes.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use pulldown_cmark::MetadataBlockKind;

use crate::generation::gen_types::Context;

pub type Range = std::ops::Range<usize>;
Expand All @@ -12,13 +14,9 @@ pub struct SourceFile {
pub children: Vec<Node>,
}

pub struct YamlHeader {
pub range: Range,
pub text: String,
}

pub struct PlusesHeader {
pub struct MetadataBlock {
pub range: Range,
pub kind: MetadataBlockKind,
pub text: String,
}

Expand Down Expand Up @@ -337,7 +335,6 @@ generate_node![
TableHead,
TableRow,
TableCell,
YamlHeader,
PlusesHeader,
MetadataBlock,
Math
];
36 changes: 21 additions & 15 deletions src/generation/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use dprint_core::formatting::conditions::*;
use dprint_core::formatting::ir_helpers::*;
use dprint_core::formatting::*;
use dprint_core_macros::sc;
use pulldown_cmark::MetadataBlockKind;
use std::borrow::Cow;
use std::rc::Rc;
use unicode_width::UnicodeWidthStr;
Expand Down Expand Up @@ -46,8 +47,7 @@ pub fn generate(node: &Node, context: &mut Context) -> PrintItems {
Node::TableHead(_) => unreachable!(),
Node::TableRow(_) => unreachable!(),
Node::TableCell(node) => gen_table_cell(node, context),
Node::YamlHeader(node) => gen_yaml_metadata(node, context),
Node::PlusesHeader(node) => gen_pluses_metadata(node, context),
Node::MetadataBlock(node) => gen_metadata_block(node, context),
Node::NotImplemented(_) => ir_helpers::gen_from_raw_string(node.text(context)),
}
}
Expand Down Expand Up @@ -115,6 +115,7 @@ fn gen_nodes(nodes: &[Node], context: &mut Context) -> PrintItems {
| Node::HorizontalRule(_)
| Node::List(_)
| Node::Table(_)
| Node::MetadataBlock(_)
| Node::BlockQuote(_) => {
items.extend(get_conditional_blank_line(node.range(), context));
}
Expand Down Expand Up @@ -187,7 +188,15 @@ fn gen_nodes(nodes: &[Node], context: &mut Context) -> PrintItems {
items.push_signal(Signal::NewLine);
}
}
_ => {}
Node::NotImplemented(_)
| Node::SourceFile(_)
| Node::Item(_)
| Node::TaskListMarker(_)
| Node::HardBreak(_)
| Node::TableHead(_)
| Node::TableRow(_)
| Node::TableCell(_)
| Node::Math(_) => {}
}
}
}
Expand Down Expand Up @@ -994,22 +1003,19 @@ fn gen_table_cell(table_cell: &TableCell, context: &mut Context) -> PrintItems {
gen_nodes(&table_cell.children, context)
}

fn gen_pluses_metadata(node: &PlusesHeader, context: &mut Context) -> PrintItems {
gen_metadata(&node.text, "+++", context)
}

fn gen_yaml_metadata(node: &YamlHeader, context: &mut Context) -> PrintItems {
gen_metadata(&node.text, "---", context)
}

fn gen_metadata(text: &str, delimiter: &str, _: &mut Context) -> PrintItems {
fn gen_metadata_block(node: &MetadataBlock, _context: &mut Context) -> PrintItems {
let mut items = PrintItems::new();

items.push_string(delimiter.into());
let delimiter = match node.kind {
MetadataBlockKind::YamlStyle => sc!("---"),
MetadataBlockKind::PlusesStyle => sc!("+++"),
};

items.push_sc(&delimiter);
items.push_signal(Signal::NewLine);
items.extend(ir_helpers::gen_from_raw_string_trim_line_ends(text.trim_end()));
items.extend(ir_helpers::gen_from_raw_string_trim_line_ends(node.text.trim_end()));
items.push_signal(Signal::NewLine);
items.push_string(delimiter.into());
items.push_sc(&delimiter);

items
}
Expand Down
67 changes: 67 additions & 0 deletions src/generation/metadata.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
use crate::generation::common::CharScanner;

pub fn strip_metadata_header(text: &str) -> &str {
// todo(dsherret): use pulldown_cmark to parse this
let mut scanner = CharScanner::new(0, text);

let delimiter = if scanner.is_next_text("---") {
scanner.move_text("---");
"---"
} else if scanner.is_next_text("+++") {
scanner.move_text("+++");
"+++"
} else {
return text;
};

if !scanner.move_new_line() {
return text;
}

while scanner.has_next() {
if scanner.is_next_text(&delimiter) {
scanner.move_text(&delimiter);
if scanner.move_new_line() || scanner.pos() == scanner.end() {
let range = std::ops::Range {
start: 0,
end: scanner.pos(),
};
return &text[range.end..];
}
}

scanner.move_next_line();
}

text
}

#[cfg(test)]
mod test {
use super::*;
#[test]
fn it_should_strip_yaml_header() {
let result = strip_metadata_header(
r#"---
a: b
---
Test"#,
);

assert_eq!(result, "\n\nTest");
}

#[test]
fn it_should_strip_plus_plus_plus_header() {
let result = strip_metadata_header(
r#"+++
a: b
+++
Test"#,
);

assert_eq!(result, "\n\nTest");
}
}
4 changes: 2 additions & 2 deletions src/generation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ mod cmark;
pub mod common;
mod gen_types;
mod generate;
mod metadata;
mod utils;
mod yaml;

pub use cmark::*;
pub use gen_types::*;
pub use generate::*;
pub use metadata::*;
pub use utils::*;
pub use yaml::*;
3 changes: 0 additions & 3 deletions src/generation/yaml/mod.rs

This file was deleted.

54 changes: 0 additions & 54 deletions src/generation/yaml/parse_yaml_header.rs

This file was deleted.

18 changes: 18 additions & 0 deletions tests/specs/YamlHeader/YamlHeaderFollowedByHtml.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
~~ deno: true ~~
!! should format !!
---
title: Testing
---

<blockquote class="md-pullquote" style="border: 0">

Everything should be made as simple as possible, but not simpler.

[expect]
---
title: Testing
---

<blockquote class="md-pullquote" style="border: 0">

Everything should be made as simple as possible, but not simpler.

0 comments on commit f2eceb4

Please sign in to comment.