Giter Club home page Giter Club logo

antora-lunr's Introduction

ℹ️ Information ℹ️

The next major version of this project will be based on the pipeline extension feature (introduced in Antora 3) and become an official extension of the Antora project. As a result, the repository has been moved to its new home.

If you are preparing to migrate to Antora 3, then you should be testing the extension instead of the integration in this repository. Once Antora 3 is released, this repository will go into maintenance mode for users who have not yet transitioned from Antora 2 to Antora 3. After some time, the repository will be archived.


Integration of Lunr in Antora

Build Status npm

Lunr provides a great search experience without the need for external, server-side, search services. It makes it possible to add an offline search engine in your Antora's documentation site.

Usage

Generate an index file

To integrate Lunr in Antora, we can either use a custom site generator pipeline that includes Lunr or modify your current site generator pipeline.

TIP: To make things easier, we provide a copy of the default site generator that in addition produces a Lunr index. Learn how to install and use this generator.

NOTE: The following instructions only apply if you are using the default pipeline. If you are using a custom pipeline, the logic remains the same but you will have to find yourself where the generateSite function should be added.

Antora provides a default pipeline named @antora/site-generator-default. Make sure that it's installed using the command npm list --depth 0. If you don't find the module in the result then it's probably installed globally. Add the -g flag and execute the command again:

npm list -g --depth 0
/usr/local/lib
├── @antora/[email protected]
├── @antora/[email protected]
└── [email protected]

As you can see in the example above, the module is installed globally in /usr/local/lib. The node_modules folder will be either at the root of your project or in your global libraries folder: /usr/local/lib/node_modules.

Once you've located the module, edit the file node_modules/@antora/site-generator-default/lib/generate-site.js adding after use strict:

const generateIndex = require('antora-lunr')

In the generateSite function add the following two lines after const siteFiles = mapSite(playbook, pages).concat(produceRedirects(playbook, contentCatalog)):

const index = generateIndex(playbook, pages, contentCatalog, env)
siteFiles.push(generateIndex.createIndexFile(index))

Install this module:

$ npm i antora-lunr

NOTE: If Antora is installed globally, you should also add this module globally using the -g flag:

$ npm i -g antora-lunr

When generating your documentation site again, an index file will be created at the root of your output directory, which depends on the value of output.dir in your playbook. For the default output dir, that will be build/site/search-index.js.

Enable the search component in the UI

Now that we have a search-index.js, we need to enable the search component in the UI.

Copy the supplemental_ui directory from the npm package node_modules/antora-lunr/supplemental_ui in your Antora playbook repository and configure a supplemental_files:

ui:
  bundle:
    url: https://gitlab.com/antora/antora-ui-default/-/jobs/artifacts/master/raw/build/ui-bundle.zip?job=bundle-stable
    snapshot: true
  supplemental_files: ./supplemental_ui

NOTE: For this to function correctly you must provide the site.url key in your playbook file. See the Antora docs on the playbook schema. If using the site locally (not serving from a web server) then you can set your site.url to a file:// reference, e.g. file:///home/documents/antora/website/public/.

TIP: If you are using serve HTTP server to view your site locally, set the site.url to http://localhost:5000.

Generate the site

Generate your documentation site with the following environment variables:

  • DOCSEARCH_ENABLED=true
  • DOCSEARCH_ENGINE=lunr

For instance, as a command line:

$ DOCSEARCH_ENABLED=true DOCSEARCH_ENGINE=lunr antora site.yml

Configuration

Index only the latest version

To index only the latest (released) version, set the following environment variable:

  • DOCSEARCH_INDEX_VERSION=latest

For instance, as a command line:

$ DOCSEARCH_ENABLED=true DOCSEARCH_ENGINE=lunr DOCSEARCH_INDEX_VERSION=latest antora site.yml

Support for other languages

By default, Lunr support only English language. You can add support for the following other languages:

  • Arabic (ar)
  • Chinese (zh)
  • Danish (da)
  • Dutch (nl)
  • Finnish (fi)
  • French (fr)
  • German (de)
  • Hindi (hi)
  • Hungarian (hu)
  • Italian (it)
  • Japanese (ja)
  • Norwegian (no)
  • Portuguese (pt)
  • Romanian (ro)
  • Russian (ru)
  • Spanish (es)
  • Swedish (sv)
  • Thai (th)
  • Turkish (tr)
  • Vietnamese (vi)

IMPORTANT: To use Chinese language, you need to install nodejieba dependency:

$ npm i nodejieba

To use one or more languages, set the DOCSEARCH_LANGS environment variable with all desired language codes (comma separated):

  • DOCSEARCH_LANGS=en,fr

For instance, as a command line:

$ DOCSEARCH_ENABLED=true DOCSEARCH_ENGINE=lunr DOCSEARCH_LANGS=en,fr antora site.yml

Testing this module

In the root of the repository, run npm t.

Who's using it

Here's a list of projects who are using Antora Lunr.
To add your project to this list, please edit this page!

antora-lunr's People

Contributors

ahus1 avatar citizenken avatar danyill avatar dependabot-preview[bot] avatar dependabot[bot] avatar djencks avatar ggrossetie avatar jabby avatar myii avatar rayoffiah avatar thor avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

antora-lunr's Issues

Mention the dedicated site generator in the README

Hello,

Thanks for this work!

After I followed the instructions in the README and started to customise the default site generator, a Google search revealed the NPM site generator to me.

This should be mentioned as one, if not the preferred way to test the project.
The setup of some of the least techy users could be messed up with the local customisation.

Potential improvements to make to the `README`

@Mogztter Thanks, I followed the README and got lunr integrated on my Antora 2.0 development setup. There were a few places where I found that the instructions could be clearer (or be updated).

https://github.com/Mogztter/antora-lunr/blob/ead8207ac0e441848ae032c02006b32f240e7c8a/README.adoc#L56

  • This is now site/search_index.json.

https://github.com/Mogztter/antora-lunr/blob/ead8207ac0e441848ae032c02006b32f240e7c8a/README.adoc#L62-L68

  • I got the impression that I should edit the url, instead of just replacing it with the one you gave.
    • Meaning, edit it to work from my fork!
  • Something like "Use the following URL..." would be clearer.

https://github.com/Mogztter/antora-lunr/blob/ead8207ac0e441848ae032c02006b32f240e7c8a/README.adoc#L70

Gitlab integration

Hi this is not an issue. Apologies for that.

I got this working locally but I have no idea how to integrate on gitlab.

Could you please have a look and help me?

Repo
Playbook
Pipeline

Allow pages to be excluded from indexing

The indexer should skip pages which are marked as "noindex". For example, consider a page which has the following meta tag:

<meta name="robots" content="noindex">

Another option is to honor exclusions in robot.txt (or perhaps it should be both).

Here's what Google has to say about noindex: https://support.google.com/webmasters/answer/93710?hl=en

Here are some other ideas around inclusion/exclusion to consider: https://swiftype.com/documentation/site-search/crawler-configuration/content-inclusion-exclusion

Remove header from generate-index

Hello,

I would like to remove h6 titles from lunr generated search-index. Is that in any way possible? I've tweaked the generate-index.js and removed the h6 entry from the following line (line 64):
$('h2,h3,h4,h5', article).each(function () { but that doesn't seem to work.

Any suggestions.

Support for pages with multiple versions

Hi,
thank you for easy setup and use of this project.
Would you have an idea how to get a improved search results overview in case multiple versions of the documentation is being built? Would there be an option to group search results based on the version?

Thank you.

Generation of a new version in npm?

Hello,

we are interested in what is new in the master branch, especially the fact that it is possible to have an empty URL in the playbook, and that it will then create relative URLs: https://github.com/Mogztter/antora-lunr/blob/master/lib/generate-index.js#L23

Indeed, we find it to be a very interesting purpose to have a website that can be hosted at several URLs (for testing, for UAT, for prod), without having to create separate bundles for each environment.

Do you think the project is in a stable enough state that a new version could be produced and released to npm?

If the site URL is set, only use the pathname segment

If the site URL is set, only prepend the pathname segment (i.e., context path) to the entries instead of the full value. For example, if the site.url value is https://example.org/docs, only use /docs. If the site URL does not have a pathname, the entries should begin with / (but not //).

Only using the pathname makes the index portable as the site is promoted across parallel environments (testing, acceptance, production) or hosted via multiple domains (to achieve high availability).

In brief, the domain segment doesn't matter. What matters (for the browser to assemble the correct URL) is the pathname that comes after the domain.

Using the pathname where possible is now standard practice in Antora. Antora will only use the full value of the site.url if an absolute URL is required (such as for the canonical URLs or in sitemap entries).

Partial word search

As of now, antora-lunr does not support partial word search. Lunr supports using wildcards, so implemntation could be as simple as adding text = "*" + text + "*" in function searchIndex.

Version bump of lunr.js to 2.3.8

Current version is 2.3.3, the latest is 2.3.8 which offers some bugfixes around fuzzy matching. Happy to put together a PR for this...

Use relative paths, not the site url

Perhaps I'm fooling myself, but it seems really easy to make the search completely portable/relocatable. Commit 94185cb in my plugin-377 branch has the changes, but there's also a bunch of lint fixes to search.js

The crucial lines are, in generate-index.js: near line 99

         component: page.src.component,
         version: page.src.version,
         name: page.src.stem,
-        url: urlPath + page.pub.url,
+        url: page.pub.url,
         titles: titles // TODO get title id to be able to use fragment identifier
       }

and in search.js: near line 112

     var documentHit = document.createElement('div')
     documentHit.classList.add('search-result-document-hit')
     var documentHitLink = document.createElement('a')
-    documentHitLink.href = item.ref
+    var rootPath = window.antora.basePath
+    documentHitLink.href = rootPath + item.ref
     documentHit.appendChild(documentHitLink)
     hits.forEach(function (hit) {
       documentHitLink.appendChild(hit)

Is there some reason to use absolute urls in the search?

Provide this library as an Antora 3 pipeline extension

Currently, this library is available as a custom site generator (through https://github.com/Mogztter/antora-site-generator-lunr).

With the introduction of the pipeline extension feature in Antora 3, we can simplify the integration.
The pipeline extension provides a lightweight event-based system that we can leverage to generate a search index.

I think we should register our extension on beforePublish:

module.exports.register = (pipeline, { config }) => {
  pipeline
    .on('beforePublish', ({ playbook, contentCatalog }) => {
      // generate a search index
    })
}

Additionally, extensions can be configured: https://docs.antora.org/antora/3.0/extend/pipeline/configure-extension/ so we could potentially address the following issues:

Extend the README with additional info

I had some problems following the instructions, specifically:
1.antora-lunr should be installed globally if antora is also installed globally (npm install -g antora-lunr)
2. Extend the bottom note by saying the search will not work due to the CORS issue if you view your site locally without having a http server.

Thanks for the work so far!

How to support other languages?

Hello @Mogztter

Thanks for your work on antora-lunr and other projects I used.

I quickly integrate antora-lunr through antora-site-generator-lunr on my website. https://jabby-techs.fr

As you can see, my website is in french and by default lunr only support english. There are some language plugins available to support french and other languages. See https://lunrjs.com/guides/language_support.html

I would like to know if there is a way to add by some configuration those languages into antora-lunr. The only way I found is to create my own generate-site function but I think it's better to improve this project.

Linter errors when used with Antora default UI

I was unable to build my fork of the Antora default UI after merging in lunr support until I fixed 480 eslint errors in js/vendor/lunr.js, 3 in js/vendor/search.js, and 3 css lint errors in css/search.css.

Search URLs ignoring site.url?

Is it possible that the generated search URLs are ignoring the Antora site.yml file's site.url setting? I'm getting only relative URLs generated no matter what I have site.url set to.

Thanks for this great project, BTW, and for the recent update.

Document how to only index the latest version

I would like to be able to index only the latest component version (but not the master/unstable branch) for two reasons:

  • Reduce index size. Many versions which are similar will quickly generate a large index (I'm not sure how well this is compressed) and I'd like to see what difference it makes if I index a single version.
  • Improve UI display. The present results display with antora-lunr is a bit repetitive and just the latest version is an easy workaround for a difficult UI problem (I guess results on the same page across multiple versions should be squashed into a version tag which then itself becomes repetitive).

I think the solution is to iterate over pages and find the latest version and only index that.

As I understand the code (which is not very well) at this point in the generation, the virtual files are already in html and so either we need a reverse lookup to extract the version for the html or we need to find this information within the web page. I thought there might be a data- attribute or a meta attribute with this information but it's not obvious to me that this currently exists.

It may also be useful to extract the prerelease information for some purposes.

site.url is missing in the generated search index

The search functionality stopped working for me since 0.7.1. It looks like the site.url which I set in the antora-playbook.yml isn't passed to the search module. Here is my test: Antora Demo Site. I built the site with DOCSEARCH_ENABLED=true DOCSEARCH_ENGINE=lunr DOCSEARCH_INDEX_VERSION=latest NODE_PATH="$(npm -g root)" antora --generator antora-site-generator-lunr antora-playbook.yml. When I search for a word and click on one of the variants it gives me a URL like this: file:///component-b/2.0/module-one/overview.html -- i.e. without the site.url part which I set in the playbook to file:///Users/pavela/tmp/docs-site/build/site.

I'm running the latest versions of everything:

$ npm ls -g --depth 0
/Users/pavela/.nvm/versions/node/v12.17.0/lib
├── @antora/[email protected]
├── @antora/[email protected]
├── [email protected]
├── [email protected]
└── [email protected]

Last time it worked properly was about a month ago (I didn't test since then). My suspicion is that it might be caused by this commit: 364a957

Appreciate your help.

No search-index.js generated

I've configured lunr as described in the readme and it doesn't work propperly (I'm running it in Windows).
Lunr should generate searc-index.js, but all I get is search_index.json.gz.
Am I doing something wrong?

The footer-scripts.hbs from supplemental-ui requires updated

I followed the documentation here (using the antora-site-generator-lunr) and manage to have a working website with the search bar! Very well explained! Kudos!

However, all the pages were containing duplicated ToC side panel!

In order to fixed it, I only have to remove the following line from footer-scripts.hbs:

<script src="{{uiRootPath}}/js/site.js"></script>

The browser debugging mode shows that the site.js is still loaded.. probably because of a newer ui-bundle.zip from antora-ui-default?

Readme update: Building site on Windows

For anyone building on Windows like me, can we add a sidebar or the like noting the following:

Terminal / Command Prompt / PowerShell

To use PowerShell, the user needs to change the execution policy (as an Administrator) with

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope LocalMachine

Alternatively, use cmd.exe.

Setting Environment Variables

Environment variables must be set apart from the antora call. As so:

C:\docs>set DOCSEARCH_ENABLED=true
C:\docs>set DOCSEARCH_ENGINE=lunr
C:\docs>antora antora-playbook.yml

The search box doesn't show up

After following the instructions in README to build the official Antora doc the site could be generated successfully without errors and the search-index.js file was generated successfully in the root directory, but I didn't see the search box in the UI. I don't understand this line in README "Copy the supplemental_ui directory in your Antora playbook repository and configure a supplemental_files". The supplemental_ui directory is already there. What place should I copy it to?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.