Skip to content

Commit 538e951

Browse files
committed
helper: split_markdown_front_matter
helper for istio and mozilla importers Signed-off-by: Hritik Vijay <hritikxx8@gmail.com>
1 parent d041a2b commit 538e951

1 file changed

Lines changed: 28 additions & 0 deletions

File tree

vulnerabilities/helpers.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import re
2727
from typing import Optional
2828
from typing import List
29+
from typing import Tuple
2930

3031
import requests
3132
import toml
@@ -164,3 +165,30 @@ def __lt__(self, other):
164165
)
165166

166167
return affected_package_with_patched_package_objects
168+
169+
170+
def split_markdown_front_matter(text: str) -> Tuple[str, str]:
171+
r"""
172+
Split text into markdown front matter and the markdown body
173+
Returns ("", text) for text with non existing front matter
174+
175+
>>> text='''---
176+
... title: DUMMY-SECURITY-2019-001
177+
... description: Incorrect access control.
178+
... cves: [CVE-2042-1337]
179+
... ---
180+
... # Markdown starts here
181+
... '''
182+
>>> split_markdown_front_matter(text)
183+
('title: DUMMY-SECURITY-2019-001\ndescription: Incorrect access control.\ncves: [CVE-2042-1337]\n', '\n# Markdown starts here\n')
184+
"""
185+
186+
front_matter = ""
187+
body = text
188+
text = text.replace("\r\n", "\n")
189+
linezero,_, text = text.partition("---\n")
190+
191+
if not linezero: # nothing before first ---
192+
front_matter,_, body = text.partition("---")
193+
194+
return front_matter, body

0 commit comments

Comments
 (0)