Skip to content

Commit ba933b1

Browse files
committed
feat: add metadata
1 parent 1e47de8 commit ba933b1

File tree

2 files changed

+25
-56
lines changed

2 files changed

+25
-56
lines changed

docs/lc/1.md

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
---
22
comments: true
3+
difficulty: 简单
4+
edit_url: https://github.com/doocs/leetcode/edit/main/solution/0000-0099/0001.Two%20Sum/README.md
35
tags:
4-
- 数组
5-
- 哈希表
6-
- 排序
7-
- 暴力
8-
- 模拟
9-
- 枚举
10-
- 双指针
11-
edit_url: "https://github.com/doocs/leetcode/edit/main/solution/0000-0099/0001.Two%20Sum/README.md"
6+
- 数组
7+
- 哈希表
128
---
139

1410
# [1. 两数之和](https://leetcode.cn/problems/two-sum)

main.py

Lines changed: 21 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,23 @@
1-
import json
21
import os
32
import re
4-
import requests
3+
import yaml
54
from collections import defaultdict
65

76

8-
def load_ratings():
9-
res = {}
10-
if os.path.exists("rating.json"):
11-
with open("rating.json", "r", encoding="utf-8") as f:
12-
ratings = json.loads(f.read())
13-
for item in ratings:
14-
res[str(item["ID"])] = item
15-
16-
url = "https://zerotrac.github.io/leetcode_problem_rating/data.json"
17-
try:
18-
resp = requests.get(url)
19-
if resp.status_code == 200:
20-
ratings = resp.json()
21-
for item in ratings:
22-
res[str(item["ID"])] = item
23-
except Exception as e:
24-
print(f"Failed to fetch ratings: {e}")
25-
return res
26-
27-
28-
rating_dict = load_ratings()
7+
def extract_metadata(content: str):
8+
# 检查是否包含 YAML front matter
9+
if content.startswith("---\n"):
10+
# 使用正则表达式匹配 YAML front matter
11+
match = re.match(r"^---\n(.*?)\n---\n", content, re.DOTALL)
12+
if match:
13+
yaml_content = match.group(1)
14+
# 解析 YAML 内容
15+
metadata = yaml.safe_load(yaml_content)
16+
# 获取剩余的 Markdown 内容
17+
remaining_content = content[match.end() :]
18+
return metadata, remaining_content
19+
# 如果没有 metadata,返回空字典和原始内容
20+
return {}, content
2921

3022

3123
for contest_file in ["docs/contest.md", "docs-en/contest.md"]:
@@ -108,10 +100,9 @@ def get_paths(dirs: str, m: int):
108100
for p in sorted(get_paths(dir, m)):
109101
# example:
110102
# p = 'solution/0000-0099/0003.Longest Substring Without Repeating Characters/README.md'
111-
edit_url = f"https://github.com/doocs/leetcode/edit/main/{p}"
112103
with open(p, "r", encoding="utf-8") as f:
113104
content = f.read()
114-
105+
metadata, content = extract_metadata(content)
115106
# [中文文档](/lcci/01.01.Is%20Unique/README.md)
116107
# 正则匹配 [中文文档](xxx) 并且移除
117108
content = re.sub(r"\[中文文档]\((.*?)\)", "", content)
@@ -144,10 +135,6 @@ def get_paths(dirs: str, m: int):
144135
elif num.endswith("- I"):
145136
num = num[:-3] + ".1"
146137
num = ".".join([x.strip(" ").lstrip("0") for x in num.split(".")])
147-
rat = -1
148-
if target_dir == "lc" and num in rating_dict:
149-
rat = int(rating_dict[num]["Rating"])
150-
print(f"Rating: {num} {rat}")
151138
is_en = "README_EN" in p
152139
if is_en:
153140
navdata_en[dir].append(f" - {num}. {name}: {target_dir}/{num}.md")
@@ -179,26 +166,12 @@ def get_paths(dirs: str, m: int):
179166
os.makedirs(docs_dir)
180167
new_path = os.path.join(docs_dir, f"{num}.md")
181168

182-
# 获取 tags
183-
match = re.search(r"<!-- tags:(.*?) -->", content)
184-
tag_headers = ""
185-
if match:
186-
tags = match.group(1).split(",")
187-
if tags and tags != [""]:
188-
tag_headers = "tags:\n"
189-
tag_headers += "".join([f" - {tag}\n" for tag in tags])
190-
tag_headers += "\n"
191-
192-
# 开启评论
193-
"""
194-
---
195-
comments: true
196-
---
197-
"""
198-
content = (
199-
f"---\ncomments: true\nedit_url: {edit_url}\n{tag_headers}---\n\n"
200-
+ content
169+
yaml_metadata = yaml.dump(
170+
metadata, default_flow_style=False, allow_unicode=True
201171
)
172+
print(metadata)
173+
metadata_section = f"---\n{yaml_metadata}---\n\n"
174+
content = metadata_section + content
202175
with open(new_path, "w", encoding="utf-8") as f:
203176
f.write(content)
204177

0 commit comments

Comments
 (0)