Skip to content

Commit

Permalink
Fix toml parse error line number (kind of) (#1138)
Browse files Browse the repository at this point in the history
- Minor change to how frontmatter is detected so that toml issues
correct line numbers
- split_content now returns (&str, &str)
  • Loading branch information
Lucretiel committed Aug 18, 2020
1 parent 05646ab commit 159ce0f
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 9 deletions.
14 changes: 7 additions & 7 deletions components/front_matter/src/lib.rs
Expand Up @@ -13,7 +13,7 @@ pub use section::SectionFrontMatter;

lazy_static! {
static ref PAGE_RE: Regex =
Regex::new(r"^[[:space:]]*\+\+\+\r?\n((?s).*?(?-s))\+\+\+\r?\n?((?s).*(?-s))$").unwrap();
Regex::new(r"^[[:space:]]*\+\+\+(\r?\n(?s).*?(?-s))\+\+\+\r?\n?((?s).*(?-s))$").unwrap();
}

#[derive(Debug, Copy, Clone, PartialEq, Serialize, Deserialize)]
Expand All @@ -37,7 +37,7 @@ pub enum InsertAnchor {

/// Split a file between the front matter and its content
/// Will return an error if the front matter wasn't found
fn split_content(file_path: &Path, content: &str) -> Result<(String, String)> {
fn split_content<'c>(file_path: &Path, content: &'c str) -> Result<(&'c str, &'c str)> {
if !PAGE_RE.is_match(content) {
bail!(
"Couldn't find front matter in `{}`. Did you forget to add `+++`?",
Expand All @@ -50,15 +50,15 @@ fn split_content(file_path: &Path, content: &str) -> Result<(String, String)> {
// caps[0] is the full match
// caps[1] => front matter
// caps[2] => content
Ok((caps[1].to_string(), caps[2].to_string()))
Ok((caps.get(1).unwrap().as_str(), caps.get(2).unwrap().as_str()))
}

/// Split a file between the front matter and its content.
/// Returns a parsed `SectionFrontMatter` and the rest of the content
pub fn split_section_content(
pub fn split_section_content<'c>(
file_path: &Path,
content: &str,
) -> Result<(SectionFrontMatter, String)> {
content: &'c str,
) -> Result<(SectionFrontMatter, &'c str)> {
let (front_matter, content) = split_content(file_path, content)?;
let meta = SectionFrontMatter::parse(&front_matter).map_err(|e| {
Error::chain(
Expand All @@ -71,7 +71,7 @@ pub fn split_section_content(

/// Split a file between the front matter and its content
/// Returns a parsed `PageFrontMatter` and the rest of the content
pub fn split_page_content(file_path: &Path, content: &str) -> Result<(PageFrontMatter, String)> {
pub fn split_page_content<'c>(file_path: &Path, content: &'c str) -> Result<(PageFrontMatter, &'c str)> {
let (front_matter, content) = split_content(file_path, content)?;
let meta = PageFrontMatter::parse(&front_matter).map_err(|e| {
Error::chain(
Expand Down
2 changes: 1 addition & 1 deletion components/library/src/content/page.rs
Expand Up @@ -136,7 +136,7 @@ impl Page {

page.lang = page.file.find_language(config)?;

page.raw_content = content;
page.raw_content = content.to_string();
let (word_count, reading_time) = get_reading_analytics(&page.raw_content);
page.word_count = Some(word_count);
page.reading_time = Some(reading_time);
Expand Down
2 changes: 1 addition & 1 deletion components/library/src/content/section.rs
Expand Up @@ -107,7 +107,7 @@ impl Section {
let (meta, content) = split_section_content(file_path, content)?;
let mut section = Section::new(file_path, meta, base_path);
section.lang = section.file.find_language(config)?;
section.raw_content = content;
section.raw_content = content.to_string();
let (word_count, reading_time) = get_reading_analytics(&section.raw_content);
section.word_count = Some(word_count);
section.reading_time = Some(reading_time);
Expand Down

0 comments on commit 159ce0f

Please sign in to comment.