Skip to content

Commit 87122ef

Browse files
committed
helper: split_markdown_front_matter
helper for istio and mozilla importers Signed-off-by: Hritik Vijay <hritikxx8@gmail.com>
1 parent 9ff0192 commit 87122ef

1 file changed

Lines changed: 28 additions & 1 deletion

File tree

vulnerabilities/helpers.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919
# for any legal advice.
2020
# VulnerableCode is a free software from nexB Inc. and others.
2121
# Visit https://github.com/nexB/vulnerablecode/ for support and download.
22-
2322
import json
2423
import re
24+
from typing import Tuple
2525

2626
import requests
2727
import toml
@@ -106,3 +106,30 @@ def contains_alpha(string):
106106
"""
107107

108108
return any([c.isalpha() for c in string])
109+
110+
111+
def split_markdown_front_matter(text: str) -> Tuple[str, str]:
112+
r"""
113+
Split text into markdown front matter and the markdown body
114+
Returns ("", text) for text with non existing front matter
115+
116+
>>> text='''---
117+
... title: DUMMY-SECURITY-2019-001
118+
... description: Incorrect access control.
119+
... cves: [CVE-2042-1337]
120+
... ---
121+
... # Markdown starts here
122+
... '''
123+
>>> split_markdown_front_matter(text)
124+
('title: DUMMY-SECURITY-2019-001\ndescription: Incorrect access control.\ncves: [CVE-2042-1337]\n', '\n# Markdown starts here\n')
125+
"""
126+
127+
front_matter = ""
128+
body = text
129+
text = text.replace("\r\n", "\n")
130+
linezero,_, text = text.partition("---\n")
131+
132+
if not linezero: # nothing before first ---
133+
front_matter,_, body = text.partition("---")
134+
135+
return front_matter, body

0 commit comments

Comments
 (0)