diff --git a/deeptutor/services/github_source/sync.py b/deeptutor/services/github_source/sync.py index deb87aca8..dc7610f99 100644 --- a/deeptutor/services/github_source/sync.py +++ b/deeptutor/services/github_source/sync.py @@ -1,4 +1,4 @@ -"""Core sync logic: pull Markdown from a GitHub repo into a KB's raw/ dir.""" +"""Core sync logic: pull Markdown and MDX from a GitHub repo into a KB's raw/ dir.""" from __future__ import annotations @@ -18,7 +18,8 @@ from deeptutor.services.github_source.client import ( logger = logging.getLogger(__name__) SYNC_INTERVAL_HOURS = 24 -MARKDOWN_EXTENSIONS = (".md", ".markdown") +# MDX is Markdown plus JSX. Keep the component tags and index the file as text. +MARKDOWN_EXTENSIONS = (".md", ".markdown", ".mdx") @dataclass diff --git a/deeptutor/utils/file_types.py b/deeptutor/utils/file_types.py index 0480e5a67..393b12c01 100644 --- a/deeptutor/utils/file_types.py +++ b/deeptutor/utils/file_types.py @@ -62,6 +62,7 @@ class FileTypeRouter: ".log", ".md", ".markdown", + ".mdx", ".rst", ".asciidoc", # Data / config diff --git a/tests/services/rag/test_file_routing.py b/tests/services/rag/test_file_routing.py index 8847a9524..dc8979e29 100644 --- a/tests/services/rag/test_file_routing.py +++ b/tests/services/rag/test_file_routing.py @@ -21,6 +21,8 @@ class TestExtensionClassification: ("DOC.PDF", DocumentType.PDF), # case-insensitive ("notes.md", DocumentType.TEXT), ("readme.MARKDOWN", DocumentType.TEXT), + ("guide.mdx", DocumentType.TEXT), + ("GUIDE.MDX", DocumentType.TEXT), ("data.json", DocumentType.TEXT), ("references.bib", DocumentType.TEXT), ("REFERENCES.BIB", DocumentType.TEXT), diff --git a/tests/services/test_github_source_mdx.py b/tests/services/test_github_source_mdx.py new file mode 100644 index 000000000..d18f1acfb --- /dev/null +++ b/tests/services/test_github_source_mdx.py @@ -0,0 +1,129 @@ +"""GitHub sources must download ``.mdx`` docs, and re-index must see them. + +Issue #2552: a source for ``lvgl/lvgl`` at ``docs/`` with glob ``*.mdx`` +finished ``last_sync_status: success`` and ``files_synced: 0``. The sync +filter kept only ``.md`` and ``.markdown``, so nothing landed in ``raw/``. +Re-index then raised ``ValueError`` because that directory had no supported +source files. +""" + +from __future__ import annotations + +import asyncio +from dataclasses import dataclass +from pathlib import Path + +from deeptutor.services.github_source import sync as sync_module +from deeptutor.services.github_source.client import FileChange +from deeptutor.services.rag.file_routing import DocumentType, FileTypeRouter + + +@dataclass +class _Entry: + path: str + + +class _Client: + def __init__(self, entries: list[_Entry]) -> None: + self._entries = entries + self.downloaded: list[str] = [] + + async def get_tree(self, repo, branch, *, path_prefix="", glob="*"): + return self._entries + + async def download_file(self, repo, path, sha): + self.downloaded.append(path) + return b"# Button\n\n\n" + + +def test_filter_keeps_mdx_that_matches_the_glob_and_drops_other_types() -> None: + entries = [ + _Entry("docs/widgets/button.mdx"), + _Entry("docs/intro.md"), + _Entry("docs/assets/logo.png"), + _Entry("README.mdx"), + ] + + kept = sync_module._filter_markdown_entries(entries, "docs/", "*.mdx") + + assert [entry.path for entry in kept] == ["docs/widgets/button.mdx"] + + +def test_default_markdown_glob_still_skips_mdx() -> None: + entries = [_Entry("docs/intro.md"), _Entry("docs/widgets/button.mdx")] + + kept = sync_module._filter_markdown_entries(entries, "docs/", "*.md") + + assert [entry.path for entry in kept] == ["docs/intro.md"] + + +def test_incremental_filter_keeps_mdx_changes() -> None: + changes = [ + FileChange("docs/widgets/button.mdx", "modified", "abc"), + FileChange("docs/intro.mdx", "removed", "def"), + FileChange("docs/assets/logo.png", "modified", "ghi"), + ] + + kept = sync_module._filter_markdown_changes(changes, "docs/", "*.mdx") + + assert [(change.path, change.status) for change in kept] == [ + ("docs/widgets/button.mdx", "modified"), + ("docs/intro.mdx", "removed"), + ] + + +def test_full_sync_writes_mdx_into_raw(tmp_path: Path, monkeypatch) -> None: + raw_dir = tmp_path / "LVGL" / "raw" + raw_dir.mkdir(parents=True) + indexed: list[str] = [] + + async def _record_index(_kb_name, files, _base_dir): + indexed.extend(files) + + monkeypatch.setattr(sync_module, "_index_files", _record_index) + client = _Client( + [ + _Entry("docs/widgets/button.mdx"), + _Entry("docs/assets/logo.png"), + _Entry("other/readme.mdx"), + ] + ) + + result = asyncio.run( + sync_module._full_sync( + client, + "LVGL", + raw_dir, + "lvgl/lvgl", + "v9.6.0", + "docs/", + "*.mdx", + "deadbeef", + str(tmp_path), + ) + ) + + written = raw_dir / "widgets" / "button.mdx" + assert result.ok is True + assert result.files_added == 1 + assert client.downloaded == ["docs/widgets/button.mdx"] + assert written.read_bytes() == b"# Button\n\n\n" + assert indexed == [str(written)] + assert not (raw_dir / "assets" / "logo.png").exists() + + +def test_mdx_only_raw_directory_is_reindexable(tmp_path: Path) -> None: + raw_dir = tmp_path / "LVGL" / "raw" / "widgets" + raw_dir.mkdir(parents=True) + source = raw_dir / "button.mdx" + source.write_text("# Button\n\n\n", encoding="utf-8") + (raw_dir / "logo.bin").write_bytes(b"\x00\x01") + + discovered = FileTypeRouter.collect_supported_files(tmp_path / "LVGL" / "raw", recursive=True) + classification = FileTypeRouter.classify_files([str(source)]) + + assert [path.name for path in discovered] == ["button.mdx"] + assert FileTypeRouter.get_document_type("guide.MDX") is DocumentType.TEXT + assert ".mdx" in FileTypeRouter.get_supported_extensions() + assert classification.text_files == [str(source)] + assert classification.unsupported == [] diff --git a/tests/utils/test_document_extractor.py b/tests/utils/test_document_extractor.py index 494858123..035636f61 100644 --- a/tests/utils/test_document_extractor.py +++ b/tests/utils/test_document_extractor.py @@ -154,6 +154,7 @@ class TestIsDocumentExtension: # Any extension in FileTypeRouter.TEXT_EXTENSIONS should be supported. assert is_document_extension("notes.txt") assert is_document_extension("readme.md") + assert is_document_extension("guide.mdx") assert is_document_extension("module.py") assert is_document_extension("config.yaml") assert is_document_extension("data.json") diff --git a/web/lib/doc-attachments.ts b/web/lib/doc-attachments.ts index fc7a3bb6c..aed3c86fc 100644 --- a/web/lib/doc-attachments.ts +++ b/web/lib/doc-attachments.ts @@ -39,6 +39,7 @@ export const TEXT_LIKE_EXTS = [ ".log", ".md", ".markdown", + ".mdx", ".rst", ".asciidoc", ".html", @@ -362,6 +363,7 @@ const JSON_EXTS = new Set([".json", ".jsonc", ".json5"]); const MARKUP_EXTS = new Set([ ".md", ".markdown", + ".mdx", ".rst", ".asciidoc", ".html", diff --git a/web/tests/doc-attachments.test.ts b/web/tests/doc-attachments.test.ts index 59d1117f7..fc9a82f92 100644 --- a/web/tests/doc-attachments.test.ts +++ b/web/tests/doc-attachments.test.ts @@ -42,6 +42,7 @@ test("classifyFile: doc via extension fallback when MIME empty", () => { test("classifyFile: accepts text & code", () => { assert.equal(classifyFile(makeFile("notes.txt", "text/plain")), "doc"); assert.equal(classifyFile(makeFile("README.md", "text/markdown")), "doc"); + assert.equal(classifyFile(makeFile("guide.mdx")), "doc"); assert.equal(classifyFile(makeFile("main.py", "text/x-python")), "doc"); // Empty MIME (common for code files) still accepted via extension assert.equal(classifyFile(makeFile("script.js")), "doc"); @@ -114,6 +115,7 @@ test("docIconFor: json/config/data/markup categories", () => { assert.ok(docIconFor("run.sh").tint.includes("slate")); assert.ok(docIconFor("table.csv").tint.includes("emerald")); assert.ok(docIconFor("doc.md").tint.includes("sky")); + assert.ok(docIconFor("guide.mdx").tint.includes("sky")); assert.ok(docIconFor("style.css").tint.includes("pink")); });