Skip to content

Commit

Permalink
Feat/soft error (#9)
Browse files Browse the repository at this point in the history
* fix:Corrected version tagging

Signed-off-by: Chris Butler <chris@thebutlers.me>

* feat: Softened errors associated with poorly formed yaml

Signed-off-by: Chris Butler <chris@thebutlers.me>

* fix: Update version tag

Signed-off-by: Chris Butler <chris@thebutlers.me>
  • Loading branch information
butler54 authored Mar 15, 2021
1 parent 5f262ac commit 2ed18ba
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
3 changes: 2 additions & 1 deletion mdformat_frontmatter/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@

from .plugin import render_token, update_mdit # noqa: F401

__version__ = "0.1.2"
__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

0 comments on commit 2ed18ba

Please sign in to comment.