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
@@ -1,4 +1,5 @@
/*
* Copyright 2025 znai maintainers
* Copyright 2019 TWO SIGMA OPEN SOURCE, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -16,6 +17,10 @@

package org.testingisdocumenting.znai.search;

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

import java.util.Objects;

/**
Expand All @@ -24,15 +29,37 @@
public class GlobalSearchEntry {
private String url;
private String fullTitle;
private String pageTitle;
private String chapterTitle;
private String pageSectionTitle;
private SearchText text;

public GlobalSearchEntry() {
}

public GlobalSearchEntry(String url, String fullTitle, String text) {
public GlobalSearchEntry(DocMeta docMeta, TocItem tocItem, PageMarkdownSection section, String url) {
this.url = url;
this.fullTitle = fullTitle;
this.text = SearchScore.STANDARD.text(text);
this.fullTitle = buildFullTitle(tocItem, section, docMeta);
this.pageTitle = tocItem.getPageTitle();
this.chapterTitle = tocItem.getChapterTitle();
this.pageSectionTitle = section.title();
this.text = SearchScore.STANDARD.text(section.markdown());
}

private String buildFullTitle(TocItem tocItem, PageMarkdownSection section, DocMeta docMeta) {
if (tocItem.isIndex()) {
return docMeta.getTitle() + (section.title().isEmpty() ? "" : ": " + section.title());
}

String pageSectionPart = section.title().isEmpty() ?
"" :
", " + section.title();

String chapterPart = tocItem.getChapterTitle().isEmpty() ?
"" :
" [" + tocItem.getChapterTitle() + "]";

return docMeta.getTitle() + ": " + tocItem.getPageTitle() + pageSectionPart + chapterPart;
}

public String getUrl() {
Expand All @@ -51,6 +78,30 @@ public void setFullTitle(String fullTitle) {
this.fullTitle = fullTitle;
}

public String getPageTitle() {
return pageTitle;
}

public void setPageTitle(String pageTitle) {
this.pageTitle = pageTitle;
}

public String getChapterTitle() {
return chapterTitle;
}

public void setChapterTitle(String chapterTitle) {
this.chapterTitle = chapterTitle;
}

public String getPageSectionTitle() {
return pageSectionTitle;
}

public void setPageSectionTitle(String pageSectionTitle) {
this.pageSectionTitle = pageSectionTitle;
}

public SearchText getText() {
return text;
}
Expand All @@ -71,11 +122,14 @@ public boolean equals(Object o) {

GlobalSearchEntry that = (GlobalSearchEntry) o;
return Objects.equals(url, that.url) &&
Objects.equals(fullTitle, that.fullTitle);
Objects.equals(fullTitle, that.fullTitle) &&
Objects.equals(pageTitle, that.pageTitle) &&
Objects.equals(chapterTitle, that.chapterTitle) &&
Objects.equals(pageSectionTitle, that.pageSectionTitle);
}

@Override
public int hashCode() {
return Objects.hash(url, fullTitle);
return Objects.hash(url, fullTitle, pageTitle, chapterTitle, pageSectionTitle);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,49 +18,93 @@
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.structure.TocItem

class GlobalSearchEntriesTest {
@Test
void "should generate XML document"() {
def docMeta = new DocMeta([title: 'Test Doc'])
def tocItem1 = new TocItem('chapter1', 'page1', 'md')
tocItem1.setPageTitleIfNoTocOverridePresent('page 1')
tocItem1.setChapterTitle('chapter 1')
def section1 = new PageMarkdownSection('section1', 'section 1', 'text 1')

def tocItem2 = new TocItem('chapter2', 'page2', 'md')
tocItem2.setPageTitleIfNoTocOverridePresent('page 2')
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('/doc-id/title1', 'full title 1', 'text 1'),
new GlobalSearchEntry('/doc-id/title2', 'full title 2', 'text 2')])
new GlobalSearchEntry(docMeta, tocItem1, section1, '/doc-id/title1'),
new GlobalSearchEntry(docMeta, tocItem2, section2, '/doc-id/title2'),
new GlobalSearchEntry(docMeta, tocItemIndex, sectionIndex, '/doc-id')])

println entries.toXml()
entries.toXml().should == '<znai>\n' +
' <entry>\n' +
' <url>/doc-id/title1</url>\n' +
' <fullTitle>full title 1</fullTitle>\n' +
' <fullTitle>Test Doc: page 1, section 1 [chapter 1]</fullTitle>\n' +
' <pageTitle>page 1</pageTitle>\n' +
' <chapterTitle>chapter 1</chapterTitle>\n' +
' <pageSectionTitle>section 1</pageSectionTitle>\n' +
' <text>\n' +
' <score>STANDARD</score>\n' +
' <text>text 1</text>\n' +
' </text>\n' +
' </entry>\n' +
' <entry>\n' +
' <url>/doc-id/title2</url>\n' +
' <fullTitle>full title 2</fullTitle>\n' +
' <fullTitle>Test Doc: page 2, section 2 [chapter 2]</fullTitle>\n' +
' <pageTitle>page 2</pageTitle>\n' +
' <chapterTitle>chapter 2</chapterTitle>\n' +
' <pageSectionTitle>section 2</pageSectionTitle>\n' +
' <text>\n' +
' <score>STANDARD</score>\n' +
' <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 handle ansi sequences"() {
def docMeta = new DocMeta([title: 'title', type: 'Guide'])
def tocItem = new TocItem('chapter', 'page', 'md')
tocItem.setPageTitleIfNoTocOverridePresent('page')
tocItem.setChapterTitle('chapter')
def section = new PageMarkdownSection('section', 'section',
"\u001B[1mwebtau:\u001B[m000\u001B" +
"[1m>\u001B[m http.get(\"https://jsonplaceholder.typicode.com/todos/1\")" +
" \u001B[33m> (\u001B[32m342ms\u001B[33m)\u001B[0m")

def entries = new GlobalSearchEntries()
entries.addAll([
new GlobalSearchEntry('/doc-id/title', 'title',
"\u001B[1mwebtau:\u001B[m000\u001B" +
"[1m>\u001B[m http.get(\"https://jsonplaceholder.typicode.com/todos/1\")" +
" \u001B[33m> (\u001B[32m342ms\u001B[33m)\u001B[0m")])
new GlobalSearchEntry(docMeta, tocItem, section, '/doc-id/title')])

entries.toXml().should == '<znai>\n' +
' <entry>\n' +
' <url>/doc-id/title</url>\n' +
' <fullTitle>title</fullTitle>\n' +
' <fullTitle>title: page, section [chapter]</fullTitle>\n' +
' <pageTitle>page</pageTitle>\n' +
' <chapterTitle>chapter</chapterTitle>\n' +
' <pageSectionTitle>section</pageSectionTitle>\n' +
' <text>\n' +
' <score>STANDARD</score>\n' +
' <text>webtau:000> http.get("https://jsonplaceholder.typicode.com/todos/1") > (342ms)</text>\n' +
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* Add: `search-entries.xml` add extra fields
Original file line number Diff line number Diff line change
Expand Up @@ -590,11 +590,11 @@ 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(
searchEntryUrl(tocItem, section),
searchEntryTitle(tocItem, section),
section.markdown()))
.map(section -> new GlobalSearchEntry(
docMeta,
tocItem,
section,
searchEntryUrl(tocItem, section)))
.collect(toList());

globalSearchEntries.addAll(siteSearchEntries);
Expand All @@ -609,23 +609,6 @@ private String searchEntryUrl(TocItem tocItem, PageMarkdownSection section) {
return docStructure.createUrl(null, docUrl);
}

private String searchEntryTitle(TocItem tocItem, PageMarkdownSection section) {
if (tocItem.isIndex()) {
return docMeta.getTitle() + " " + (section.title().isEmpty() ?
docMeta.getType() : section.title());
}

String pageSectionPart = section.title().isEmpty() ?
"" :
", " + section.title();

String chapterPart = tocItem.getChapterTitle().isEmpty() ?
"" :
" [" + tocItem.getChapterTitle() + "]";

return docMeta.getTitle() + ": " + tocItem.getPageTitle() + pageSectionPart + chapterPart;
}

// 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