Extract title, sections, and links from markdown.
(text: str)
| 27 | |
| 28 | |
| 29 | def parse_markdown(text: str) -> dict: |
| 30 | """Extract title, sections, and links from markdown.""" |
| 31 | lines = text.splitlines() |
| 32 | title = "" |
| 33 | sections = [] |
| 34 | links = [] |
| 35 | |
| 36 | for line in lines: |
| 37 | if line.startswith("# ") and not title: |
| 38 | title = line[2:].strip() |
| 39 | elif line.startswith("## "): |
| 40 | sections.append(line[3:].strip()) |
| 41 | elif "](http" in line: |
| 42 | start = line.index("](") + 2 |
| 43 | end = line.index(")", start) |
| 44 | links.append(line[start:end]) |
| 45 | |
| 46 | return {"title": title, "sections": sections, "links": links, "format": "markdown"} |
| 47 | |
| 48 | |
| 49 | def parse_json(text: str) -> dict: |