Skip to content

Commit d1bb379

Browse files
YuriiMotovtiangolo
andauthored
✨ Support title option (#39)
Co-authored-by: Sebastián Ramírez <tiangolo@gmail.com>
1 parent 5203da3 commit d1bb379

File tree

3 files changed

+299
-5
lines changed

3 files changed

+299
-5
lines changed

src/markdown_include_variants/_main.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -279,15 +279,18 @@ def run(self, lines: list[str]) -> list[str]:
279279
configs_str = line_match.group(2).strip()
280280
include_lines: list[tuple[int, int]] = []
281281
highlight_lines: list[tuple[int, int]] = []
282+
title: Union[str, None] = None
282283
for config_match in re.finditer(re_config, configs_str):
283284
if config_match:
284285
name = config_match.group(1)
285-
ranges_str = config_match.group(2)
286-
ranges = parse_lines_index(ranges_str)
286+
value_str = config_match.group(2)
287287
if name == "ln":
288-
include_lines.extend(ranges)
288+
include_lines.extend(parse_lines_index(value_str))
289289
elif name == "hl":
290-
highlight_lines.extend(ranges)
290+
highlight_lines.extend(parse_lines_index(value_str))
291+
elif name == "title":
292+
assert value_str.startswith('"') and value_str.endswith('"')
293+
title = value_str[1:-1]
291294
use_hl_lines: list[tuple[int, int]] = []
292295
if highlight_lines:
293296
if include_lines:
@@ -356,6 +359,8 @@ def run(self, lines: list[str]) -> list[str]:
356359
first_line = "```python"
357360
if use_hl_lines:
358361
first_line += f' hl_lines="{use_hl_lines_str}"'
362+
if title:
363+
first_line += f' title="{title}"'
359364

360365
block_lines.extend(
361366
[
@@ -373,6 +378,8 @@ def run(self, lines: list[str]) -> list[str]:
373378
if highlight_lines:
374379
hl_lines_full_str = generate_hl_string(highlight_lines)
375380
first_line_full += f' hl_lines="{hl_lines_full_str}"'
381+
if title:
382+
first_line_full += f' title="{title}"'
376383
block_lines.extend(
377384
[
378385
"///// details | 👀 Full file preview",
@@ -390,6 +397,9 @@ def run(self, lines: list[str]) -> list[str]:
390397
]
391398
)
392399
if len(sorted_variants):
400+
first_line_variant = "```python"
401+
if title:
402+
first_line_variant += f' title="{title}"'
393403
block_lines.extend(
394404
[
395405
"///// details | 🤓 Other versions and variants",
@@ -413,7 +423,7 @@ def run(self, lines: list[str]) -> list[str]:
413423

414424
block_lines.extend(
415425
[
416-
"```python",
426+
first_line_variant,
417427
f"{{!{variant.path}!}}",
418428
"```",
419429
"////",

0 commit comments

Comments
 (0)