diff --git a/docs/usage/configuration/general.md b/docs/usage/configuration/general.md index 973658c1..c01ef1ad 100644 --- a/docs/usage/configuration/general.md +++ b/docs/usage/configuration/general.md @@ -494,3 +494,62 @@ def some_function():
Docstring of the function.
//// /// + +[](){#option-llm_friendly_source} +## `llm_friendly_source` + +- **:octicons-package-24: Type [`bool`][] :material-equal: `False`{ title="default value" }** + + +When [`show_source`](#show_source) is enabled, render source code in a format more compatible with LLM-focused tools. This removes line numbers and uses simple markdown code blocks instead of complex HTML tables. + +This is particularly useful when using tools like mkdocs-llmstxt that convert documentation to LLM-friendly text formats. + +```yaml title="in mkdocs.yml (global configuration)" +plugins: +- mkdocstrings: + handlers: + python: + options: + show_source: true + llm_friendly_source: true +``` + +```md title="or in docs/some_page.md (local configuration)" +::: path.to.object + options: + show_source: true + llm_friendly_source: true +``` + +/// admonition | Preview + type: preview + +//// tab | LLM-friendly source +some_function()Docstring of the function.
+ +///// details | Source code in `package/module.py` + type: quote + +```python +def some_function(): + ... +``` +///// +//// + +//// tab | Traditional source (with line numbers) +some_function()Docstring of the function.
+ +///// details | Source code in `package/module.py` + type: quote + +```python linenums="1" +def some_function(): + ... +``` +///// +//// +/// diff --git a/src/mkdocstrings_handlers/python/_internal/config.py b/src/mkdocstrings_handlers/python/_internal/config.py index c0f9b79e..c7dd599b 100644 --- a/src/mkdocstrings_handlers/python/_internal/config.py +++ b/src/mkdocstrings_handlers/python/_internal/config.py @@ -841,6 +841,14 @@ class PythonInputOptions: ), ] = True + llm_friendly_source: Annotated[ + bool, + _Field( + group="general", + description="When show_source is enabled, render source code in a format more compatible with LLM-focused tools (no line numbers, simple markdown code blocks).", + ), + ] = False + show_submodules: Annotated[ bool, _Field( diff --git a/src/mkdocstrings_handlers/python/templates/material/_base/class.html.jinja b/src/mkdocstrings_handlers/python/templates/material/_base/class.html.jinja index 8a54dd1b..09e73ffc 100644 --- a/src/mkdocstrings_handlers/python/templates/material/_base/class.html.jinja +++ b/src/mkdocstrings_handlers/python/templates/material/_base/class.html.jinja @@ -200,7 +200,13 @@ Context: {{ init.relative_filepath }} {%- endif -%} - {{ init.source|highlight(language="python", linestart=init.lineno or 0, linenums=True) }} + {% if config.llm_friendly_source %} +{{ init.source }}
+ {{ class.source }}
+ {{ function.source }}
+ ' in llm_friendly_html
+ assert "linenums" not in llm_friendly_html
+ assert "lineno" not in llm_friendly_html
+
+
def test_give_precedence_to_user_paths() -> None:
"""Assert user paths take precedence over default paths."""
last_sys_path = sys.path[-1]