E411 Search plugin crashes for `h[1-6]` contained in other elements · Issue #4824 · squidfunk/mkdocs-material · GitHub
[go: up one dir, main page]

Skip to content

Search plugin crashes for h[1-6] contained in other elements #4824

@jeffreytolar

Description

@jeffreytolar

Context

Our CI recently pulled in the newly released 9.0.1 this week; the build for one of our sites started failing afterwards.

Bug description

We have some old (non-ideal) markdown that crashes the search plugin during indexing:

$ ../bin/mkdocs build
INFO     -  Cleaning site directory
INFO     -  Building documentation to directory: /private/tmp/mkdocs/test/site
ERROR    -  Error building page 'index.md': list index out of range
Traceback (most recent call last):
  File "/tmp/mkdocs/test/../bin/mkdocs", line 8, in <module>
    sys.exit(cli())
  File "/private/tmp/mkdocs/lib/python3.9/site-packages/click/core.py", line 1130, in __call__
    return self.main(*args, **kwargs)
  File "/private/tmp/mkdocs/lib/python3.9/site-packages/click/core.py", line 1055, in main
    rv = self.invoke(ctx)
  File "/private/tmp/mkdocs/lib/python3.9/site-packages/click/core.py", line 1657, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/private/tmp/mkdocs/lib/python3.9/site-packages/click/core.py", line 1404, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/private/tmp/mkdocs/lib/python3.9/site-packages/click/core.py", line 760, in invoke
    return __callback(*args, **kwargs)
  File "/private/tmp/mkdocs/lib/python3.9/site-packages/mkdocs/__main__.py", line 250, in build_command
    build.build(cfg, dirty=not clean)
  File "/private/tmp/mkdocs/lib/python3.9/site-packages/mkdocs/commands/build.py", line 329, in build
    _build_page(file.page, config, doc_files, nav, env, dirty)
  File "/private/tmp/mkdocs/lib/python3.9/site-packages/mkdocs/commands/build.py", line 226, in _build_page
    context = config.plugins.run_event(
  File "/private/tmp/mkdocs/lib/python3.9/site-packages/mkdocs/plugins.py", line 520, in run_event
    result = method(item, **kwargs)
  File "/private/tmp/mkdocs/lib/python3.9/site-packages/material/plugins/search/plugin.py", line 90, in on_page_context
    self.search_index.add_entry_from_context(page)
  File "/private/tmp/mkdocs/lib/python3.9/site-packages/material/plugins/search/plugin.py", line 143, in add_entry_from_context
    parser.feed(page.content)
  File "/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/html/parser.py", line 110, in feed
    self.goahead(0)
  File "/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/html/parser.py", line 172, in goahead
    k = self.parse_endtag(i)
  File "/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/html/parser.py", line 420, in parse_endtag
    self.handle_endtag(elem)
  File "/private/tmp/mkdocs/lib/python3.9/site-packages/material/plugins/search/plugin.py", line 417, in handle_endtag
    elif data[-1].isspace() and data[-2] == f"<{tag}>":
IndexError: list index out of range

The markdown that results in this is a section header in a list:

- # Section header in a list

... disabling the search plugin, that snippet results in this HTML:

<ul>
<li>
<h1 id="section-header-in-a-list">Section header in a list</h1>
</li>
</ul>

It's entirely possible that this is "invalid" markdown - however, this did "work" (not crash) with previous releases (including 8.5.11).

We have no real need for that that hierarchy (I suspect it was originally a typo), so I've updated our site to remove the bullet points. It took some digging (aided by breakpoint() :)) to narrow down which part of the file was triggering the crash.


Changing

elif data[-1].isspace() and data[-2] == f"<{tag}>":
to include a check for len(data) >= 2 results in a successful build, although I'm not sure the generated search index makes sense:

$ jq . site/search/search_index.json
{
  "config": {
    "lang": [
      "en"
    ],
    "separator": "[\\s\\-]+",
    "pipeline": [
      "stopWordFilter"
    ]
  },
  "docs": [
    {
      "location": "",
      "title": "Welcome to MkDocs",
      "text": "<ul> <li>"
    },
    {
      "location": "#section-header-in-a-list",
      "title": "Section header in a list",
      "text": "</li> </ul>"
    }
  ]
}

Related links

Reproduction

example.zip

Steps to reproduce

Run mkdocs build with the attached zip (probably need to remove info from the mkdocs.yml)

Browser

No response

Before submitting

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugIssue reports a bugresolvedIssue is resolved, yet unreleased if open

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0