Skip to content
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright 2026 znai maintainers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.testingisdocumenting.znai.search;

import org.testingisdocumenting.znai.core.DocMeta;
import org.testingisdocumenting.znai.markdown.PageMarkdownSection;
import org.testingisdocumenting.znai.structure.DocStructure;
import org.testingisdocumenting.znai.structure.DocUrl;
import org.testingisdocumenting.znai.structure.TocItem;

import java.util.List;

import static java.util.stream.Collectors.toList;

public class GlobalSearchEntriesBuilder {
private final DocMeta docMeta;
private final DocStructure docStructure;
private final GlobalSearchEntries globalSearchEntries;

public GlobalSearchEntriesBuilder(DocMeta docMeta, DocStructure docStructure) {
this.docMeta = docMeta;
this.docStructure = docStructure;
this.globalSearchEntries = new GlobalSearchEntries();
}

public GlobalSearchEntries getGlobalSearchEntries() {
return globalSearchEntries;
}

public void addSearchEntries(TocItem tocItem, List<PageMarkdownSection> sections) {
List<GlobalSearchEntry> entries = sections.stream()
.filter(section -> !(section.title().isEmpty() && section.markdown().trim().isEmpty()))
.map(section -> new GlobalSearchEntry(
docMeta,
tocItem,
section,
searchEntryUrl(tocItem, section)))
.collect(toList());

globalSearchEntries.addAll(entries);
}

String searchEntryUrl(TocItem tocItem, PageMarkdownSection section) {
if (tocItem.isIndex()) {
String anchorIdWithHash = section.id().isEmpty() ? "" : "#" + section.id();
return docStructure.fullUrl(anchorIdWithHash);
}

DocUrl docUrl = new DocUrl(tocItem.getDirName(), tocItem.getFileNameWithoutExtension(), section.id());
return docStructure.createUrl(null, docUrl);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package org.testingisdocumenting.znai.search
import org.junit.Test
import org.testingisdocumenting.znai.core.DocMeta
import org.testingisdocumenting.znai.markdown.PageMarkdownSection
import org.testingisdocumenting.znai.parser.TestDocStructure
import org.testingisdocumenting.znai.structure.TocItem

class GlobalSearchEntriesTest {
Expand All @@ -36,16 +37,11 @@ class GlobalSearchEntriesTest {
tocItem2.setChapterTitle('chapter 2')
def section2 = new PageMarkdownSection('section2', 'section 2', 'text 2')

def tocItemIndex = TocItem.createIndex()
def sectionIndex = new PageMarkdownSection('', '', 'index text')

def entries = new GlobalSearchEntries()
entries.addAll([
new GlobalSearchEntry(docMeta, tocItem1, section1, '/doc-id/title1'),
new GlobalSearchEntry(docMeta, tocItem2, section2, '/doc-id/title2'),
new GlobalSearchEntry(docMeta, tocItemIndex, sectionIndex, '/doc-id')])
new GlobalSearchEntry(docMeta, tocItem2, section2, '/doc-id/title2')])

println entries.toXml()
entries.toXml().should == '<znai>\n' +
' <entry>\n' +
' <url>/doc-id/title1</url>\n' +
Expand All @@ -69,20 +65,33 @@ class GlobalSearchEntriesTest {
' <text>text 2</text>\n' +
' </text>\n' +
' </entry>\n' +
' <entry>\n' +
' <url>/doc-id</url>\n' +
' <fullTitle>Test Doc</fullTitle>\n' +
' <pageTitle></pageTitle>\n' +
' <chapterTitle></chapterTitle>\n' +
' <pageSectionTitle></pageSectionTitle>\n' +
' <text>\n' +
' <score>STANDARD</score>\n' +
' <text>index text</text>\n' +
' </text>\n' +
' </entry>\n' +
'</znai>\n'
}

@Test
void "should generate index page search entry url without index in path"() {
def docMeta = new DocMeta([title: 'Test Doc'])
docMeta.setId('doc-id')
def docStructure = new TestDocStructure()

def builder = new GlobalSearchEntriesBuilder(docMeta, docStructure)

def tocItemIndex = TocItem.createIndex()
builder.addSearchEntries(tocItemIndex, [
new PageMarkdownSection('', '', 'index text'),
new PageMarkdownSection('intro', 'Introduction', 'intro text')])

def tocItem = new TocItem('chapter', 'page', 'md')
tocItem.setPageTitleIfNoTocOverridePresent('page')
tocItem.setChapterTitle('chapter')
builder.addSearchEntries(tocItem, [
new PageMarkdownSection('section', 'section', 'text')])

builder.searchEntryUrl(tocItemIndex, new PageMarkdownSection('', '', '')).should == '/test-doc/'
builder.searchEntryUrl(tocItemIndex, new PageMarkdownSection('intro', 'Introduction', '')).should == '/test-doc/#intro'
builder.searchEntryUrl(tocItem, new PageMarkdownSection('section', 'section', '')).should == '/test-doc/chapter/page#section'
}

@Test
void "should handle ansi sequences"() {
def docMeta = new DocMeta([title: 'title', type: 'Guide'])
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* Fix: `search-entries.xml` correctly render index page url
4 changes: 4 additions & 0 deletions znai-docs/znai/release-notes/2026.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 1.85.2

:include-markdowns: 1.85.2

# 1.85

:include-markdowns: 1.85
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import org.testingisdocumenting.znai.html.*;
import org.testingisdocumenting.znai.html.reactjs.HtmlReactJsPage;
import org.testingisdocumenting.znai.html.reactjs.ReactJsBundle;
import org.testingisdocumenting.znai.markdown.PageMarkdownSection;
import org.testingisdocumenting.znai.parser.MarkupParser;
import org.testingisdocumenting.znai.parser.MarkupParserResult;
import org.testingisdocumenting.znai.parser.commonmark.MarkdownParser;
Expand Down Expand Up @@ -74,7 +73,7 @@ public class WebSite implements Log {

private Map<TocItem, Page> pageByTocItem;
private Map<TocItem, MarkupParserResult> parserResultByTocItem;
private final GlobalSearchEntries globalSearchEntries;
private GlobalSearchEntriesBuilder globalSearchEntriesBuilder;
private final LocalSearchEntries localSearchEntries;

private TableOfContents toc;
Expand Down Expand Up @@ -130,7 +129,6 @@ private WebSite(Configuration siteConfig) {
searchIndexJavaScript = WebResource.moduleWithPath(SEARCH_INDEX_FILE_NAME);
auxiliaryFilesRegistry = new AuxiliaryFilesRegistry();
markupParsingConfiguration = MarkupParsingConfigurations.byName(cfg.documentationType);
globalSearchEntries = new GlobalSearchEntries();
localSearchEntries = new LocalSearchEntries();
auxiliaryFilesLastUpdateTime = new HashMap<>();

Expand Down Expand Up @@ -397,6 +395,7 @@ private void createTopLevelToc() {
private void createDocStructure() {
docStructure = new WebSiteDocStructure(componentsRegistry, docMeta, toc, markupParsingConfiguration);
componentsRegistry.setDocStructure(docStructure);
globalSearchEntriesBuilder = new GlobalSearchEntriesBuilder(docMeta, docStructure);
}

private void createResourceResolvers() {
Expand Down Expand Up @@ -590,24 +589,10 @@ private void updateTocItemWithPageMeta(TocItem tocItem, PageMeta pageMeta) {
}

private void updateSearchEntries(TocItem tocItem, MarkupParserResult parserResult) {
List<GlobalSearchEntry> siteSearchEntries = parserResult.markdown().sections().stream()
.filter(section -> !(section.title().isEmpty() && section.markdown().trim().isEmpty()))
.map(section -> new GlobalSearchEntry(
docMeta,
tocItem,
section,
searchEntryUrl(tocItem, section)))
.collect(toList());

globalSearchEntries.addAll(siteSearchEntries);
globalSearchEntriesBuilder.addSearchEntries(tocItem, parserResult.markdown().sections());
localSearchEntries.add(new PageLocalSearchEntries(tocItem, parserResult.searchEntries()));
}

private String searchEntryUrl(TocItem tocItem, PageMarkdownSection section) {
DocUrl docUrl = new DocUrl(tocItem.getDirName(), tocItem.getFileNameWithoutExtension(), section.id());
return docStructure.createUrl(null, docUrl);
}

// each markup file may refer other files like code snippets or diagrams
// we maintain dependency between them, so we know which one triggers what page refresh during preview mode
//
Expand Down Expand Up @@ -663,7 +648,7 @@ private void generateSearchIndex() {
String jsIndexScript = localSearchEntries.buildIndexScript();
deployer.deploy("search-index.js", jsIndexScript);

String xmlExternalIndex = globalSearchEntries.toXml();
String xmlExternalIndex = globalSearchEntriesBuilder.getGlobalSearchEntries().toXml();
deployer.deploy("search-entries.xml", xmlExternalIndex);
}

Expand Down