Skip to content

added SearchRecord custom functions for at-blocks #2672

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
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 99 additions & 11 deletions src/html/HTMLWriter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,91 @@
return SearchRecord(ctx, navnode; text = mdflatten(node))
end


# For @example blocks
function SearchRecord(ctx::HTMLContext, navnode::Documenter.NavNode, node::MarkdownAST.Node{Nothing}, element::Documenter.MultiOutput)
output_text = ""

first_child = true
for child in node.children
if first_child
first_child = false
continue
end

if isa(child.element, Documenter.MultiOutputElement) && isa(child.element.element, Dict)
for mime in [MIME"text/plain"(), MIME"text/markdown"(), MIME"text/html"()]
if haskey(child.element.element, mime)
output_text *= string(child.element.element[mime]) * " "
break
end
end

Check warning on line 734 in src/html/HTMLWriter.jl

View check run for this annotation

Codecov / codecov/patch

src/html/HTMLWriter.jl#L734

Added line #L734 was not covered by tests
else
output_text *= mdflatten(child) * " "
end
end
return SearchRecord(ctx, navnode; text = output_text)
end

# For @repl blocks
function SearchRecord(ctx::HTMLContext, navnode::Documenter.NavNode, node::MarkdownAST.Node{Nothing}, element::Documenter.MultiCodeBlock)
# Extract only output lines (even-numbered children)
output_text = ""

# Process each child, taking only even-numbered ones (outputs)
child_index = 0
for child in node.children
child_index += 1
if child_index % 2 == 0 # Even indices are outputs
output_text *= mdflatten(child) * " "
end
end
return SearchRecord(ctx, navnode; text = output_text)
end

# For @eval blocks
function SearchRecord(ctx, navnode, node::Node, element::Documenter.EvalNode)
# For @eval blocks, we only want to index the result, not the source code
if isnothing(element.result)
return SearchRecord(ctx, navnode; text = "")
else
return SearchRecord(ctx, navnode; text = mdflatten(element.result))
end
end

function SearchRecord(ctx, navnode, node::Node, element::Documenter.MultiOutputElement)
if element.element isa Dict && haskey(element.element, :source)
return SearchRecord(ctx, navnode; text = "")

Check warning on line 770 in src/html/HTMLWriter.jl

View check run for this annotation

Codecov / codecov/patch

src/html/HTMLWriter.jl#L768-L770

Added lines #L768 - L770 were not covered by tests
end

return SearchRecord(ctx, navnode, node, element.element)

Check warning on line 773 in src/html/HTMLWriter.jl

View check run for this annotation

Codecov / codecov/patch

src/html/HTMLWriter.jl#L773

Added line #L773 was not covered by tests
end

"""
Filter function to prevent duplicate search entries.
"""
function should_index_for_search(navnode, node, element)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should be sufficient to just check the type, no need to introspect into the objects? So something as simple as this should be enough probably:

exclude_from_search_index(element) = typeof(element) in (Documenter.MultiOutputElement, Documenter.MultiCodeBlock, Documenter.EvalNode)

if element isa Documenter.MultiOutputElement &&
element.element isa Dict &&
haskey(element.element, :source)
return false

Check warning on line 783 in src/html/HTMLWriter.jl

View check run for this annotation

Codecov / codecov/patch

src/html/HTMLWriter.jl#L783

Added line #L783 was not covered by tests
end

if element isa Documenter.MultiCodeBlock &&
node.parent !== nothing &&
iseven(findfirst(child -> child === node, collect(node.parent.children)))
return false
end

if element isa Documenter.EvalNode &&
node.parent !== nothing &&
node === first(node.parent.children)
return false

Check warning on line 795 in src/html/HTMLWriter.jl

View check run for this annotation

Codecov / codecov/patch

src/html/HTMLWriter.jl#L795

Added line #L795 was not covered by tests
end

return true
end

function JSON.lower(rec::SearchRecord)
# Replace any backslashes in links, if building the docs on Windows
src = replace(rec.src, '\\' => '/')
Expand Down Expand Up @@ -1680,8 +1765,10 @@
function domify(dctx::DCtx)
ctx, navnode = dctx.ctx, dctx.navnode
return map(getpage(ctx, navnode).mdast.children) do node
rec = SearchRecord(ctx, navnode, node, node.element)
push!(ctx.search_index, rec)
if should_index_for_search(navnode, node, node.element)
rec = SearchRecord(ctx, navnode, node, node.element)
push!(ctx.search_index, rec)
end
domify(dctx, node, node.element)
end
end
Expand Down Expand Up @@ -1773,15 +1860,16 @@
@tags a code article header span

# push to search index
rec = SearchRecord(
ctx, navnode;
fragment = Documenter.anchor_fragment(docsnode.anchor),
title = string(docsnode.object.binding),
category = Documenter.doccat(docsnode.object),
text = mdflatten(mdast_node)
)
push!(ctx.search_index, rec)

if should_index_for_search(navnode, docsnode, docsnode.object)
rec = SearchRecord(
ctx, navnode,
fragment = Documenter.anchor_fragment(docsnode.anchor),
title = string(docsnode.object.binding),
category = Documenter.doccat(docsnode.object),
text = mdflatten(mdast_node)
)
push!(ctx.search_index, rec)
end
return article[".docstring"](
header(
a[".docstring-article-toggle-button.fa-solid.fa-chevron-down", :href => "javascript:;", :title => "Collapse docstring"],
Expand Down
Loading