diff --git a/myst_parser/mocking.py b/myst_parser/mocking.py index b65888e7..b59c747a 100644 --- a/myst_parser/mocking.py +++ b/myst_parser/mocking.py @@ -516,7 +516,12 @@ def run(self) -> list[nodes.Element]: ) self.renderer.nested_render_text( file_content, - startline + 1, + # ``nested_render_text`` adds this to the 0-based token maps, and + # ``_render_tokens`` applies the 0-based to 1-based conversion + # afterwards. So the offset of the first included line is + # ``startline``, already 0-based; adding one here reported every + # line in the file one too low. + startline, heading_offset=self.options.get("heading-offset", 0), ) finally: diff --git a/tests/test_renderers/fixtures/mock_include_errors.md b/tests/test_renderers/fixtures/mock_include_errors.md index 6e443cb6..13820b8e 100644 --- a/tests/test_renderers/fixtures/mock_include_errors.md +++ b/tests/test_renderers/fixtures/mock_include_errors.md @@ -19,5 +19,48 @@ Error in include file: ```{include} bad.md ``` . -tmpdir/bad.md:2: (WARNING/2) Unknown interpreted text role "a". [myst.role_unknown] +tmpdir/bad.md:1: (WARNING/2) Unknown interpreted text role "a". [myst.role_unknown] +. + +Error line in include file: +. +```{include} bad_line3.md +``` +. +tmpdir/bad_line3.md:3: (WARNING/2) Unknown interpreted text role "a". [myst.role_unknown] +. + +Error line in include file, after content in the parent: +. +para + +```{include} bad_line3.md +``` +. +tmpdir/bad_line3.md:3: (WARNING/2) Unknown interpreted text role "a". [myst.role_unknown] +. + +Error line in include file with start-line: +. +```{include} bad_skipped.md +:start-line: 2 +``` +. +tmpdir/bad_skipped.md:4: (WARNING/2) Unknown interpreted text role "a". [myst.role_unknown] +. + +Error line in include file with front matter: +. +```{include} bad_frontmatter.md +``` +. +tmpdir/bad_frontmatter.md:5: (WARNING/2) Unknown interpreted text role "a". [myst.role_unknown] +. + +Error line in nested include file: +. +```{include} bad_outer.md +``` +. +tmpdir/bad_inner.md:3: (WARNING/2) Unknown interpreted text role "a". [myst.role_unknown] . diff --git a/tests/test_renderers/test_include_directive.py b/tests/test_renderers/test_include_directive.py index 9f03d633..d8501b04 100644 --- a/tests/test_renderers/test_include_directive.py +++ b/tests/test_renderers/test_include_directive.py @@ -41,6 +41,15 @@ def test_errors(file_params, tmp_path, monkeypatch): monkeypatch.chdir(tmp_path) tmp_path.joinpath("bad.md").write_text("{a}`b`") + # The role sits on line 3, so a warning reported against any other line is + # the include's own line attribution being off. + tmp_path.joinpath("bad_line3.md").write_text("line one\n\n{a}`b`\n") + tmp_path.joinpath("bad_skipped.md").write_text("skipped\nskipped2\n\n{a}`b`\n") + tmp_path.joinpath("bad_frontmatter.md").write_text("---\nx: 1\n---\n\n{a}`b`\n") + tmp_path.joinpath("bad_inner.md").write_text("inner one\n\n{a}`b`\n") + tmp_path.joinpath("bad_outer.md").write_text( + "outer line\n\n```{include} bad_inner.md\n```\n" + ) report_stream = StringIO() publish_doctree(