Skip to content

Commit 2a2597a

Browse files
committed
Add split_markdown_front_matter()
Signed-off-by: Hritik Vijay <hritikxx8@gmail.com>
1 parent 916b2e5 commit 2a2597a

1 file changed

Lines changed: 38 additions & 0 deletions

File tree

vulnerabilities/helpers.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import json
2424
import re
25+
from typing import Iterable, List, Tuple
2526

2627
import requests
2728
import toml
@@ -78,4 +79,41 @@ def create_etag(data_src, url, etag_key):
7879
return True
7980

8081

82+
def split_markdown_front_matter(lines: Iterable) -> Tuple[str, str]:
83+
"""
84+
This function splits lines into markdown front matter and the markdown body
85+
and returns list of lines for both
86+
NOTE: lines is expected to be an iterable containing strings
87+
88+
for example :
89+
lines =
90+
---
91+
title: ISTIO-SECURITY-2019-001
92+
description: Incorrect access control.
93+
cves: [CVE-2019-12243]
94+
---
95+
# Markdown starts here
96+
97+
get_markdown_front_matter(lines) would return
98+
['title: ISTIO-SECURITY-2019-001','description: Incorrect access control.'
99+
,'cves: [CVE-2019-12243]'],
100+
["# Markdown starts here"]
101+
"""
102+
103+
fmlines = []
104+
mdlines = []
105+
splitter = mdlines
106+
107+
for index, line in enumerate(lines):
108+
line = line.strip()
109+
if index == 0 and line.startswith("---"):
110+
splitter = fmlines
111+
elif line.startswith("---"):
112+
splitter = mdlines
113+
else:
114+
splitter.append(line)
115+
116+
return "\n".join(fmlines), "\n".join(mdlines)
117+
118+
81119
is_cve = re.compile(r"CVE-\d+-\d+", re.IGNORECASE).match

0 commit comments

Comments
 (0)