mirror of
https://github.com/hwchase17/langchain.git
synced 2026-02-22 07:05:36 +00:00
```python
import json
import re
from pathlib import Path
def parse_markdown_to_sidebar(markdown_content):
lines = markdown_content.splitlines()
sidebar = []
current_category = None
current_subcategory = None
for line in lines:
if line.startswith('### '):
# Subcategory
if current_subcategory is not None:
current_category['items'].append(current_subcategory)
subcategory_title = line.strip('# ').strip()
current_subcategory = {
"type": "category",
"label": subcategory_title,
"collapsed": True,
"items": [],
"link": {"type": "generated-index"}
}
elif line.startswith('## '):
# Category
if current_category is not None:
if current_subcategory is not None:
current_category['items'].append(current_subcategory)
current_subcategory = None
sidebar.append(current_category)
category_title = line.strip('# ').strip()
current_category = {
"type": "category",
"label": category_title,
"collapsed": True,
"items": [],
"link": {"type": "generated-index"}
}
elif line.startswith('- ['):
# Link
match = re.match(r'- \[(.*?)\]\((.*?)\)', line)
if match:
title, link = match.groups()
link = link.replace('/docs/', '') # Remove '/docs/' prefix
if current_subcategory is not None:
current_subcategory['items'].append(link)
elif current_category is not None:
current_category['items'].append(link)
# Add the last category and subcategory if they exist
if current_subcategory is not None:
current_category['items'].append(current_subcategory)
if current_category is not None:
sidebar.append(current_category)
return sidebar
def generate_sidebar_json(file_path):
with open(file_path, 'r') as md_file:
markdown_content = md_file.read()
sidebar = parse_markdown_to_sidebar(markdown_content)
sidebar_json = json.dumps({"items": sidebar}, indent=2)
return sidebar_json
```
LangChain Documentation
For more information on contributing to our documentation, see the Documentation Contributing Guide