Skip to content

Commit f6086a9

Browse files
dev: fix recent notes framework
1 parent 6ab2d79 commit f6086a9

2 files changed

Lines changed: 34 additions & 24 deletions

File tree

src/mkdocs_note/plugin.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import os
44
import json
5-
from pathlib import Path
65

76
from mkdocs.structure.files import Files, File
87
from mkdocs.config.defaults import MkDocsConfig
@@ -150,20 +149,15 @@ def on_page_markdown(
150149
Returns:
151150
str: The markdown content.
152151
"""
153-
if self.config.recent_notes_config["enabled"]:
154-
if self.is_note_index_page(page.file):
155-
markdown = insert_recent_note_links(
156-
markdown=markdown,
157-
notes_list=self.notes_list,
158-
insert_num=self.config.recent_notes_config["insert_num"],
159-
replace_marker=self.config.recent_notes_config["insert_marker"],
160-
)
161-
else:
162-
log.warning("Recent notes are not supported on non-note index pages.")
163-
markdown = markdown
164-
else:
165-
log.debug("Recent notes insertion are disabled.")
166-
markdown = markdown
152+
# Only process recent notes on the note index page
153+
if self.config.recent_notes_config["enabled"] and self.is_note_index_page(page.file):
154+
markdown = insert_recent_note_links(
155+
markdown=markdown,
156+
notes_list=self.notes_list,
157+
insert_num=self.config.recent_notes_config["insert_num"],
158+
replace_marker=self.config.recent_notes_config["insert_marker"],
159+
)
160+
log.debug(f"Recent notes inserted into {page.file.src_uri}")
167161

168162
return markdown
169163

@@ -172,12 +166,14 @@ def is_note_index_page(self, f: File) -> bool:
172166
"""Check if the page is a note index page.
173167
174168
Args:
175-
page (Page): The page to check.
169+
f (File): The file to check.
176170
177171
Returns:
178172
bool: True if the page is a note index page, False otherwise.
179173
"""
180-
return f.src_uri == str(Path(self.config.notes_root) / "index.md")
174+
# src_uri is relative to docs_dir, so we just check for 'index.md'
175+
# when notes_root is the docs_dir itself
176+
return f.src_uri == "index.md"
181177

182178

183179
def insert_recent_note_links(
@@ -198,9 +194,13 @@ def insert_recent_note_links(
198194
str: The markdown content with recent note links inserted.
199195
"""
200196

201-
content = ""
197+
content = "<ul>\n"
202198
for f in notes_list[:insert_num]:
203199
title = extract_title(f)
204200
date = extract_date(f).strftime("%Y-%m-%d %H:%M:%S")
205-
content += f"- <div class='recent-notes'><a href='{f.page.abs_url}'>{title}</a><small>{date}</small></div>\n"
201+
# Use f.url (relative URL) or f.page.url if page is available
202+
url = f.page.url if hasattr(f, 'page') and f.page else f.url
203+
# No indentation to avoid Markdown treating it as code block
204+
content += f'<li><div style="display:flex; justify-content:space-between; align-items:center;"><a href="{url}">{title}</a><span style="font-size:0.8em; color:#888;">{date}</span></div></li>\n'
205+
content += "</ul>\n"
206206
return markdown.replace(replace_marker, content)

src/mkdocs_note/utils/scanner.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,33 @@ def scan_notes(files: Files, config) -> tuple[list[File], list[File]]:
2020
"""
2121
notes_dir = Path(config.notes_root) if isinstance(config.notes_root, str) else config.notes_root
2222
if not notes_dir.exists():
23+
logger.warning(f"Notes directory does not exist: {notes_dir}")
2324
return [], []
2425

2526
notes = []
2627
invalid_files = []
2728

2829
try:
2930
for f in files:
30-
path_name = f.src_uri.split("/")
31-
32-
if len(path_name) < 2 or path_name[1] != notes_dir:
31+
# Skip non-documentation pages
32+
if not f.is_documentation_page():
33+
continue
34+
35+
# Check if file is within notes_root by comparing absolute paths
36+
# f.abs_src_path is the absolute path to the source file
37+
try:
38+
file_path = Path(f.abs_src_path)
39+
# Check if the file is within the notes_root directory
40+
file_path.relative_to(notes_dir)
41+
except (ValueError, AttributeError):
42+
# File is not within notes_root
3343
continue
3444

35-
if f.is_documentation_page() and validate_frontmatter(f):
45+
# Validate frontmatter
46+
if validate_frontmatter(f):
3647
notes.append(f)
3748
else:
3849
invalid_files.append(f)
39-
continue
4050
except Exception as e:
4151
logger.error(f"Error scanning notes: {e}")
4252
raise e

0 commit comments

Comments
 (0)