Skip to content

Commit dd580b7

Browse files
committed
Squash merge feature/59-continuous-code-refactoring into develop
1 parent b86455b commit dd580b7

File tree

3 files changed

+61
-24
lines changed

3 files changed

+61
-24
lines changed

docs/features/20211123215420.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ tags: [hyperlink, zettel, markdown]
88

99
Two syntaxes of links inside markdown documents are supported:
1010

11-
1. Markdown style: `[title](url)`
12-
2. Wiki style: `[[url]]`
11+
1. Markdown style: ```[title](url)```
12+
2. Wiki style: ```[[url]]```
1313

1414
When linking to a markdown document which is a zettel, wiki-style links will be converted to markdown-style and the title of the target zettel will be used.
1515

mkdocs_zettelkasten/plugin/adapters/backlinks_to_page.py

Lines changed: 46 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,68 @@
22

33
from mkdocs.structure.pages import Page
44

5+
from mkdocs_zettelkasten.plugin.entities.zettel import Zettel
56
from mkdocs_zettelkasten.plugin.services.zettel_service import ZettelService
67

78
logger = logging.getLogger(
89
__name__.replace("mkdocs_zettelkasten.plugin.", "mkdocs.plugins.zettelkasten.")
910
)
1011

1112

13+
def add_backlink_to_target(
14+
link: str,
15+
page: Page,
16+
zettel: Zettel,
17+
zettel_service: ZettelService,
18+
) -> None:
19+
"""
20+
Adds a backlink to the target zettel if the given zettel links to the page.
21+
22+
This function checks if the zettel corresponds to the current page. If so,
23+
it finds the target zettel referenced by the link and appends a backlink
24+
entry to its backlinks list.
25+
26+
Args:
27+
link (str): The link to the target zettel.
28+
page (Page): The current MkDocs page.
29+
zettel (Zettel): The zettel associated with the current page.
30+
zettel_service (ZettelService): Service to retrieve zettels.
31+
"""
32+
33+
if zettel.id != page.meta["zettel"].id:
34+
return
35+
36+
target_zettel = zettel_service.get_zettel_by_partial_path(link)
37+
38+
if not target_zettel:
39+
return
40+
41+
backlink = {
42+
"url": page.url,
43+
"title": page.title,
44+
}
45+
target_zettel.backlinks.append(backlink)
46+
logger.debug(
47+
"Found link from %s to %s. Adding it to %s's backlinks.",
48+
zettel.rel_path,
49+
target_zettel.rel_path,
50+
target_zettel.rel_path,
51+
)
52+
53+
1254
def adapt_backlinks_to_page(page: Page, zettel_service: ZettelService) -> None:
1355
"""
1456
Update backlinks for zettels that link to the current page.
1557
1658
:param page: The page whose backlinks should be updated.
59+
:type page: Page
1760
:param zettel_service: Service providing zettel and backlink information.
61+
:type zettel_service: ZettelService
1862
"""
63+
1964
if not page.meta["is_zettel"]:
2065
return
2166

22-
current_zettel_id = page.meta["zettel"].id
23-
2467
for link, source_zettels in zettel_service.backlinks.items():
2568
for zettel in source_zettels:
26-
if zettel.id != current_zettel_id:
27-
continue
28-
29-
target_zettel = zettel_service.get_zettel_by_partial_path(link)
30-
if not target_zettel:
31-
continue
32-
33-
backlink = {
34-
"url": page.url,
35-
"title": page.title,
36-
}
37-
target_zettel.backlinks.append(backlink)
38-
logger.debug(
39-
"Found link from %s to %s. Adding it to %s's backlinks.",
40-
zettel.rel_path,
41-
target_zettel.rel_path,
42-
target_zettel.rel_path,
43-
)
69+
add_backlink_to_target(link, page, zettel, zettel_service)

mkdocs_zettelkasten/plugin/adapters/page_links_to_zettels.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
import re
34
from typing import TYPE_CHECKING
45

56
if TYPE_CHECKING:
@@ -69,6 +70,16 @@ def process_match(m: Match) -> str:
6970

7071
return f"[{title}]({url})"
7172

72-
markdown = WIKI_LINK.sub(process_match, markdown)
73+
# Split markdown into alternating segments (non-code/code)
74+
code_block_pattern = re.compile(r"```", re.DOTALL)
75+
parts = code_block_pattern.split(markdown)
7376

74-
return MD_LINK.sub(process_match, markdown)
77+
processed_parts = []
78+
for i, original_part in enumerate(parts):
79+
processed_part = original_part
80+
if i % 2 == 0: # Process even-indexed non-code segments
81+
processed_part = WIKI_LINK.sub(process_match, processed_part)
82+
processed_part = MD_LINK.sub(process_match, processed_part)
83+
processed_parts.append(processed_part)
84+
85+
return "```".join(processed_parts)

0 commit comments

Comments
 (0)