linting YAML front matter in a Markdown file #4066
Replies: 4 comments 1 reply
-
A couple of thoughts.. The markdownlint configuration to ignore front matter mentions that the and it links to the Jekyll documentation describing front matter. Commonmark and GFM specifications are silent with regards to front matter; as a result, it's not directly, explicitly, specifically clear if front matter may only be YAML or if it may be some other format (JSON, TOML). In 2015, moonwave99 asked on Stack Overflow Is there a YAML front matter standard / validator? to which joemailer responded:
[...]
Therefore, I'm thinking:
point: it doesn't look very straight-forward to determine if a given front matter block is YAML, JSON, or TOML It's entirely possible that a single repository has Markdown files that have YAML, JSON, and/or TOML front matter blocks so it may be problematic to specify a configuration variable that says to use a YAML linter on all Markdown front matter. Running Maybe instead of a singular front matter linter, there was one for each supported format (e.g., Perhaps it may be useful to encourage folks to use a filename specification like |
Beta Was this translation helpful? Give feedback.
-
I'm thinking of creating https://github.com/GSA-TTS/tts.gsa.gov/blob/staging/bin/yaml_lint_front_matter.bash Any thoughts? Is there a better or cleaner or more pythonic way you would suggest? |
Beta Was this translation helpful? Give feedback.
-
fmlint() {
for markdown_file in "$@" ; do
temp_file="$(mktemp)"
cp -f "$markdown_file" "$temp_file"
sed -Eze 's/((^|\n)---.*?---(\n|$)).*/\1/' < "$temp_file" | yamllint --options-go-here - | sed -Ee "s|$temp_file|$markdown_file|g"
rm -f "$temp_file"
done
} |
Beta Was this translation helpful? Give feedback.
-
@nvuillam I thought I responded to the comment about Is the suggestion that I use Perhaps my understanding of Please forgive this mess -- it's not well considered. if Or does For what it's worth, the project I'm trying to lint includes Markdown files that are simply Markdown (i.e., they have no YAML front matter), some are basically YAML files with a Ideally, the output generated from doing the linting would have the original filenames (i.e., |
Beta Was this translation helpful? Give feedback.
-
Suppose there's a Markdown file named
people.md
and its contents is:There's an error in the YAML (the
Develop
line should be indented two more spaces).Because the filename (people.md) has a
.md
extension,MARKDOWN_MARKDOWNLINT
would lint the file. However, DavidAnson/markdownlint defaults to ignoring front matter; if told not to ignore it, it would likely throw errors when the YAML (or JSON or TOML) content doesn't meet Markdown linter rules.tl;dr: markdownlint won't lint the YAML and the YAML error will go unnoticed.
If, adrienverge/yamllint were passed the file (i.e., if YAML_YAMLLINT matched
.md
files), it, too, would fail when the Markdown content didn't validate as proper YAML. Insert the same logic for JSON and TOML.So, it would be nice if there was a mechanism to extract the front matter from a Markdown file and allow the proper linter to lint that content.
I wrote a script to read STDIN, grab what's between the first and second
---
, and write it to STDOUT. My thinking was something like:.md
filemktemp
)yamllint
sed
to rewrite the filename of the linter from the temporary file back to the original so the errors correspond to the real file and not the temporary oneHere's a link to a Bash script I put together just to see how difficult it would be to extract the front matter. The script doesn't do any linting or handle any files -- it just reads from STDIN and writes to STDOUT. The link'll probably break if/when the PR with which it's associated is merged:
https://raw.githubusercontent.com/GSA-TTS/tts.gsa.gov/11c59be4a36d4b43a551338994d7baf2be04c634/bin/extract_front_matter.bash
So.. would something like this (i.e., a tool to lint front matter from Markdown files) be something that MegaLinter would want to include? Any thoughts?
Beta Was this translation helpful? Give feedback.
All reactions