Skip to content

Commit

Permalink
Correct errors associated with build process.
Browse files Browse the repository at this point in the history
Signed-off-by: Chris Butler <chris@thebutlers.me>
  • Loading branch information
butler54 committed Mar 15, 2021
2 parents 6409b13 + 2ed18ba commit bfee00d
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 12 deletions.
26 changes: 21 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,32 @@
[![codecov.io][cov-badge]][cov-link]
[![PyPI version][pypi-badge]][pypi-link]

An [mdformat](https://github.com/executablebooks/mdformat) plugin for ensuring that yaml frontmatter is respected. As we are unable
to tell the intent of a user if the yaml is incorrectly formatted it will trigger an exception.
An [mdformat](https://github.com/executablebooks/mdformat) plugin for ensuring that yaml `front-matter` is respected.
Many tools (such as [jekyll](https://github.com/jekyll/jekyll)) use yaml front matter for automation purposes.
`mdformat-frontmatter` only supports yaml. For example:

This package was built from the [template](https://github.com/executablebooks/mdformat-plugin) provided by [executable books](https://github.com/executablebooks) and customized (specifically to separate content PR's from the release cycle).
```markdown

---
test: yaml
---
# This looks okay
For some markdown code.
```

Frontmatter can only be at the first line or two of the code.
```markdown
# This is not
---
test: yaml
---
```
Note: that at this stage this plugin is not that sophisticated. The principle objective is to allow properly formed yaml header blocks to pass through.
Incorrectly formed blocks may result in strange behaviour.

## Development

This package dirver [flit](https://flit.readthedocs.io) as the build engine, and [tox](https://tox.readthedocs.io) for test automation.
This package was built from the [template](https://github.com/executablebooks/mdformat-plugin) provided by [executable books](https://github.com/executablebooks) and customized (specifically to separate content PR's from the release cycle).
This package driver [flit](https://flit.readthedocs.io) as the build engine, and [tox](https://tox.readthedocs.io) for test automation.

To install these development dependencies:

Expand Down
2 changes: 1 addition & 1 deletion mdformat_frontmatter/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@

from .plugin import render_token, update_mdit # noqa: F401

__version__ = "0.1.1"
__version__ = "0.1.3"
16 changes: 11 additions & 5 deletions mdformat_frontmatter/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

from markdown_it import MarkdownIt
from markdown_it.token import Token
import mdformat.renderer
from mdformat.renderer import MDRenderer
from mdit_py_plugins.front_matter import front_matter_plugin
from yaml import dump, load
from yaml import YAMLError, dump, load

try:
from yaml import CDumper as Dumper
Expand Down Expand Up @@ -32,9 +33,14 @@ def render_token(
token = tokens[index]
if token.type != "front_matter":
return None
index
# Safety check - parse and dump yaml to ensure it is correctly formatted
yamled = load(token.content, Loader=Loader)
serialized = dump(yamled, Dumper=Dumper)
content = token.markup + "\n" + serialized + token.markup + "\n"
try:
yamled = load(token.content, Loader=Loader)
serialized = dump(yamled, Dumper=Dumper)
content = token.markup + "\n" + serialized + token.markup + "\n"
except YAMLError:
mdformat.renderer.LOGGER.warning("Invalid YAML in a front matter block")

content = token.markup + "\n" + token.content + token.markup + "\n"

return content, index
33 changes: 33 additions & 0 deletions tests/fixtures.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,36 @@ Some *markdown*

* c
.

Test yaml header
.
---
some: yaml
---
# Now some markdown
And some more.
.
---
some: yaml
---
# Now some markdown

And some more.
.

Test yaml reformat
.
---
some: yaml

---
# Now some markdown
And some more.
.
---
some: yaml
---
# Now some markdown

And some more.
.
5 changes: 4 additions & 1 deletion tests/pre-commit-test.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
---
test: yaml
---
# Test file

add your syntax here
This file

0 comments on commit bfee00d

Please sign in to comment.