Skip to content

```contents renders the header id not header content #237

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
ajinkya-k opened this issue Mar 10, 2025 · 8 comments · May be fixed by #259
Open

```contents renders the header id not header content #237

ajinkya-k opened this issue Mar 10, 2025 · 8 comments · May be fixed by #259

Comments

@ajinkya-k
Copy link

If there is a heading such as

## [Internal structure of $\Lambda_\theta$ and $\bf Z$](@id internal-struct)

in one of the doc-pages, when this is called in a "```contents" block its rendered as follows

Image

Ideally, the content blocks should render the header content not the id like so:
Image

@ajinkya-k
Copy link
Author

Even when i removed the custom id (i.e just provided a regular markdown header like so: ## [Internal structure of $\Lambda_\theta$ and $\bf Z$](@id internal-struct)), the math did not render. It just spit out \Lambda_\theta in the output.

@asinghvi17
Copy link
Collaborator

We might be doing an mdflatten where things should go into the rendering pipeline...

@ajinkya-k
Copy link
Author

I am guessing here?

Documenter.MDFlatten.mdflatten(elements[idx])

@asinghvi17
Copy link
Collaborator

That is here:

function render(io::IO, ::MIME"text/plain", node::Documenter.MarkdownAST.Node, contents::Documenter.ContentsNode, page, doc; kwargs...)
current_path = nothing
for (count, path, anchor) in contents.elements
path = mdext(path)
header = anchor.object
anchor_frag = Documenter.anchor_fragment(anchor)
anchor_frag = vitepress_anchor(anchor_frag)
url = replace(string(path, anchor_frag), " " => "%20")
anchor_id = replace(anchor.id, "-" => " ")
link = Markdown.Link(anchor_id, url)
level = header.level
# Reset level to 1 if this is a new path
if path != current_path
level = 1
current_path = path
end
print(io, " "^(level - 1), "- ")
linkfix = ".md#"
println(io, replace(Markdown.plaininline(link), linkfix => "#"))
end
return println(io)
end

and Documenter does something different in LaTeXWriter:

https://github.com/JuliaDocs/Documenter.jl/blob/21dd8c91744abd4d5c906a9da5c8d1a9ba142d49/src/latex/LaTeXWriter.jl#L386-L428

so we should copy that.

@ajinkya-k
Copy link
Author

ajinkya-k commented May 14, 2025

@asinghvi17 I am assuming we replace the latex invocations with markdown, so like the following:

function render(io::IO, ::MIME"text/plain", node::Documenter.MarkdownAST.Node, contents::Documenter.ContentsNode, page, doc; kwargs...)
    # Having an empty itemize block in LaTeX throws an error, so we bail early
    # in that situation:
    isempty(contents.elements) && (_println(io); return)

    depth = 1
    _println(io, "") # add empty line
    for (count, path, anchor) in contents.elements
        @assert length(anchor.node.children) == 1
        header = first(anchor.node.children)
        level = header.element.level
        # Filter out header levels smaller than the requested mindepth
        level = level - contents.mindepth + 1
        level < 1 && continue
        # If we're changing depth, we need to make sure we always print the
        # correct number of \begin{itemize} and \end{itemize} statements.
        if level > depth
            for k in 1:(level - depth)
                # if we jump by more than one level deeper we need to put empty
                # \items in -- otherwise LaTeX will complain
                (k >= 2) && _println(io, "- ")
                _println(io, "\\t- ") # todo: fix number of tabs
                depth += 1
            end
        elseif level < depth
            for _ in 1:(depth - level)
                _println(io, "")
                depth -= 1
            end
        end
        # Print the corresponding \item statement
        id = _hash(Documenter.anchor_label(anchor))
        _print(io, "[", id, "](")
        latex(io, header.children)
        _println(io, ")")
    end
    # print any remaining missing \end{itemize} statements
    for _ in 1:depth # todo: remove this loop?
        _println(io, "")
    end
    _println(io)
    return   
end

@asinghvi17
Copy link
Collaborator

asinghvi17 commented May 14, 2025

Yeah that looks reasonable, but we should use two spaces instead of tabs. Feel free to PR, thanks!!

@ajinkya-k
Copy link
Author

Yeah that looks reasonable, but we should use two spaces instead of tabs. Feel free to PR, thanks!!

thanks! Will PR in a few mins. Is the convention one space per depth? or two spaces per depth?

@asinghvi17
Copy link
Collaborator

asinghvi17 commented May 14, 2025

Two spaces I believe (to align with - <text>)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants