deno_std icon indicating copy to clipboard operation
deno_std copied to clipboard

front_matter returns false possitives

Open oscarotero opened this issue 3 years ago • 2 comments

I have the following code to extract front matter data from files:

import { extract, test } from "./deps/front_matter.ts";

if (test(content)) {
    const { attrs = {}, body } = extract<Data>(content);
    attrs.content = body;

    return attrs;
}

The test function returns true with Markdown files with separators like ---. For example.

# Project title

---

This is a description of the project

- **Author:** The DPM Authors
- **Version:** 0.1.0
- **License:** MIT

---

MIT License

And this makes the script to fail:

TypeError: Failed to extract front matter
    at extract (https://deno.land/[email protected]/encoding/front_matter.ts:56:9)

To consider that a file content has front matter, the triple dash separator must be at the begining of the content. In this case there's a title before, so the test function should return false.

Steps to Reproduce

  1. Create a file with markdown content and two separators ---.
  2. Run the test function with this content.
  3. The function returns true.
  • OS: MacOS 12
  • deno version: 1.25
  • std version: 0.153.0

oscarotero avatar Sep 01 '22 15:09 oscarotero

The bug is actuallly happening, I'm already working on fixing it!

luk3skyw4lker avatar Sep 03 '22 01:09 luk3skyw4lker

I have added a exclusive validation for the first line for multiline texts like yours, I think that should do the trick

luk3skyw4lker avatar Sep 03 '22 01:09 luk3skyw4lker

closed by #2801

kt3k avatar Oct 28 '22 08:10 kt3k