MCPcopy Create free account
hub / github.com/Python-Markdown/markdown / get_frontmatter

Function get_frontmatter

tools/superfences_formaters.py:41–64  ·  view source on GitHub ↗

Get front matter from string.

(text)

Source from the content-addressed store, hash-verified

39
40
41def get_frontmatter(text):
42 """Get front matter from string."""
43
44 frontmatter = OrderedDict()
45
46 if text.startswith("---"):
47 m = re.search(r'^(-{3}\r?\n(?!\r?\n)(.*?)(?<=\n)(?:-{3}|\.{3})\r?\n)', text, re.DOTALL)
48 if m:
49 yaml_okay = True
50 try:
51 frontmatter = yaml_load(m.group(2))
52 if frontmatter is None:
53 frontmatter = OrderedDict()
54 # If we didn't get a dictionary, we don't want this as it isn't front matter.
55 assert isinstance(frontmatter, (dict, OrderedDict)), TypeError
56 except Exception:
57 # We had a parsing error. This is not the YAML we are looking for.
58 yaml_okay = False
59 frontmatter = OrderedDict()
60
61 if yaml_okay:
62 text = text[m.end(1):]
63
64 return frontmatter, text
65
66
67def md_render(src="", language="", class_name=None, options=None, md="", **kwargs):

Callers 1

md_renderFunction · 0.85

Calls 1

yaml_loadFunction · 0.85

Tested by

no test coverage detected