Sphinx 添加并引用渲染的 Markdown 文件

问题描述 投票:0回答:0

假设我有以下示例项目

root
|   readme_sphinx_link.md
|
+---docs
|       example.png
|       readme.md
|       
+---docs_sphinx
|   |   make.bat
|   |   Makefile
|   |   
|   \---source
|       |   conf.py
|       |   index.rst
|       |   
|       +---_autosummary
|       |       example_pkg.main.Dummy.rst
|       |       example_pkg.main.rst
|       |       example_pkg.rst
|       |       readme_include.md
|       |       
|       \---_templates
|               custom-attribute-template.rst
|               custom-class-template.rst
|               custom-module-template.rst
|               
\---example_pkg
    |   main.py
    |   __init__.py

我想使用模块 main.py 创建 example_pkg 的 sphinx 文档。在使用 sphinx 时,我想嵌入位于 docs 中的 markdown,以便在创建的 html 中可用。为了实现这一点,我尝试使用 myst-parser 和 sphinx 设置,使用自动摘要,基于 https://stackoverflow.com/a/62613202

此外,我尝试遵循此处的常见问题解答

https://myst-parser.readthedocs.io/en/latest/faq/index.html#include-a-file-from-outside-the-docs-folder-like-readme-md

使 markdown 在 main.py 中可用

主.py

'''
Example main with example class
'''
class Dummy:
    '''
    .. include:: readme_include.md
        :parser: myst_parser.sphinx_
    '''
    def __init__(self):
        '''
        Class constructor
        '''
        pass

如文档字符串中所示,我包含并解析 readme_include.md (在 _autosummary 中),如下所示

自述文件_include v1

```{include} ../../../readme_sphinx_link.md
:relative-docs: docs/
:relative-images:
```

但是当运行 sphinx 时,我收到以下警告并且没有创建链接

D:\..\..\root\readme_sphinx_link.md:5: WARNING: Unknown source document 'D:/../../root/docs/readme' [myst.xref_missing]

但是使用这个的时候

自述文件_包含 v2

```{include} ../../../docs/readme.md
:relative-docs: docs/
:relative-images:
```

没有错误,自述文件已解析并显示在 html 中。但我想要一个指向呈现的自述文件的链接(如我提到的常见问题解答中所示),但我无法实现。也许我理解错了,甚至不可能做我想做的事。我也很欣赏任何其他提示来引用 sphinx 中渲染的 markdown 文件

最后但并非最不重要的readme.md的内容

# Header for readme

This is just a dummy readme for demonstration purposes

Here we see a dummy picture

![example](./example.png)

和 readme_sphinx_link.md

See the following link

[Click me for markdown](docs/readme.md)

以及创建的 sphinx html 的两个版本,其中蓝色标记是使用 readme_include v1 创建的,红色标记是使用 readme_include v2

创建的

python python-3.x markdown sphinx myst
© www.soinside.com 2019 - 2024. All rights reserved.