npm package: @philippnagel/docusaurus-next-terminology
Description: A terminology / dictionary add-on for Docusaurus
Author: Philipp Nagel
Latest version: 1.3.1

# Docusaurus Terminology

> [!WARNING]
> This package is now diverging from the original package.
> The core of the package is ESM only and tested with Docusaurus v3.
> This fork includes newer fixes.

[![Renovate enabled](https://img.shields.io/badge/renovate-enabled-brightgreen.svg)](https://renovatebot.com)

**docusaurus-terminology** is a node package for creating a
terminology structure in your [Docusaurus](https://docusaurus.io/)
project. This plugin allows you to use terms in your pages that
'stick out' of the surrounding text, while hovering over them makes a
popup appear with a short explanation of the term and clicking on the
term navigates the user to the page that documents the concept.

![Term in text example](static/terminology_example.gif)

You can also generate a glossary with the list of your terms.

## How It Works

This plugin, once it's installed in a Docusaurus project, parses docs
in two ways:

  1. Parses all `*.md(x)` files under `docs/` and replaces each pattern with an
  appropriate React component supporting a tooltip functionality (see below).
  2. Generates a glossary page with all terms corresponding to the
  `*.md(x)` files under `docs/terms/`.

## Compatibility

The plugin has been tested with Docusaurus v3.0 and above. 
The same default environment used by docusaurus satisfies the plugin requirements.

## Installation

To install the plugin to your Docusaurus repository, use the command:

```commandline
npm install @philippnagel/docusaurus-next-terminology
```

Then, you can add the plugin to `docusaurus.config.js` file of your repository:

```js
module.exports = {
  // ...
  plugins: [
    '@philippnagel/docusaurus-next-terminology'
  ]
}
```

Or, you can use it with extra options defined (with more examples in
the next sections): 

```js
  plugins: [
    [
      "@philippnagel/docusaurus-next-terminology",
      {
        //options
      }
    ]
  ]
```

## Usage

### Defining a Term

This plugin assumes that you follow a specific pattern. Each term
should have its own `.md(x)` file, inside the `./docs/terms`
directory, and it needs to consist of the following structure:

```markdown
---
id: term_name
title: Term page
hoverText: This hover text will appear in the documentation page that you reference this term
---

### Term explanation

content here
```

> Pay attention to the `hoverText` attribute, as it is important to provide this
> attribute (along with the default Docusaurus attributes), so the plugin can
> fetch the correct popup text to show when referencing a term.


### Use Patterns to Reference a Term

When writing docs inside `docs/*.md(x)` files, in order to refer to a term, 
you may use the following syntax:


```
%%term_text|term_name%%
```

where:
- `term_text`: The terminology text you want it to be visible in the
documentation page
- `term_name`: The value of the `id` attribute, which resides in the
header of the term file:
  > ```markdown
  > ---
  > id: term_name
  > ...
  > ---
  > ```

After successfully running the script, the above occurrence will be replaced by 
a reference (technically a React component) that  will render `term_text` as a 
link to the corresponding term page, which is in turn generated from the 
`term_name` attribute; furthermore, *hovering* over `term_text` displays a term 
summary, as extracted from the corresponding term page.

### Set Display Type Option

The plugin supports two display types for term references:

1. **Tooltip** (default): Shows a small popup when hovering over the term
2. **Popover**: Shows a larger popup when clicking on the term, which is better for longer descriptions

You can configure this in two ways:

#### 1. Global Default

Set the default display type for all terms in your `docusaurus.config.js`:

```js
plugins: [
  [
    '@philippnagel/docusaurus-next-terminology',
    {
      // ...other options
      defaultDisplayType: "popover" // or "tooltip" (default)
    }
  ]
]
```

#### 2. Per Term

Set the display type for individual terms in their markdown files:

```markdown
---
id: term_name
title: Term page
hoverText: This hover text will appear in the documentation page
displayType: popover
---

#### Term explanation
content here
```

The `displayType` attribute in the term file will override the global default.


### Example Usage

Say you want to reference a term that exists under the `./docs/terms/` directory,
e.g., `./docs/terms/party.md`. You can use the following syntax to reference
this term in your documentation page:

```markdown
Some content that wants to reference the %%Party|party%% term
```

When the script runs, this will be replaced as follows:

```html
Some content that wants to reference the <Term reference="party" popup="Popup text">Party</Term> term
```

which supports the functionality explained above.

And finally, all you will see in your compiled documentation page, will be:

```markdown
Some content that wants to reference the Party term
```

with the word **Party** containing the described functionality.

### Testing the Changes Locally

After writing terms and patterns in your `.md` files, you can always validate 
these changes, by running a dry-run command, in order to see compile errors
and a sample output of all the changes that will be made from the actual 
script. You can do that by running:

```commandline
npm docusaurus parse --dry-run
```

and you will see in the command line the expected output of the actual command.

### Generating the Terminology Documentation

When you are finished referencing terms and have written corresponding term 
pages, you can test this locally by running:

```commandline
npm docusaurus parse
```

This will replace all `%%term_text|term_name%%` occurrences with the React 
component supporting the required functionality.

### Generating the Glossary Page

If everything works well with the above procedure, you can then generate a
glossary page, by running:

```commandline
npm docusaurus glossary
```

This will generate a file in `./docs/glossary.md` where every term that has been
mentioned above will be populated in the `glossary.md` page.

## When to Generate the Terminology Docs

As the terminology plugin actually edits all markdown files, your Git repository 
will show changes in the `git diff` command. It is highly recommended to avoid 
committing the changes, as the plugin will no longer be able to detect
patterns that have been altered. 

Your best case scenario will be to use the scripts in CI, just before building 
and deploying the documentation.

The following example of a Gitlab CI job shows how to perform these steps in 
the CI environment:

```yaml
...

generate-docs:
	image: node:lts
	stage: build
	before_script:
		- yarn install
	script:
		- yarn docusaurus parse
		- yarn docusaurus glossary
		- yarn build
```
and then you can use the `build` directory to serve your documentation.

## Configuration Options

For using the plugin with the default options, you can provide just the plugin 
name in `docusaurus.config.js` file of your repository:

```js
  plugins: [
    '@philippnagel/docusaurus-next-terminology'
  ]
```

You can also use some of the following options specified by wrapping the name 
and an options object in an array inside your configuration:

|      Option          |                                                Description                                                |  Type  |    Default value   |
|:--------------------:|:---------------------------------------------------------------------------------------------------------:|:------:|:------------------:|
|     termsDir         |                                the directory used to collect the term files                               | string |    ./docs/terms    |
|  glossaryFilepath    |                            specify the directory and name of the glossary file                            | string | ./docs/glossary.md |
|  patternSeparator    | the special character used to separate `term_text` <br>and `term_name` in the replace pattern for parsing | string |         \|         |
|   noParseFiles       |                             array of files to be excluded from search/replace                             |  array |         []         |
|  noGlossaryFiles     |                         array of term files to not be listed on the glossary page                         |  array |         []         |
| glossaryTermPatterns |          array of `type` values, to choose category/ies of terms to be included in the glossary           |  array |         []         |

> `type`: optional attribute in the header of the Markdown files

**IMPORTANT NOTE**: All file paths need to be relative to the
project's root directory. If you want to exclude a file, you should
write `./docs/excude-me.md`. 

Example:

```js
plugins: [
  [
    '@philippnagel/docusaurus-next-terminology',
    {
      termsDir: './docs/terminology/',
      noParseFiles: ['./docs/terminology/agent.md', './docs/terminology/actor.md'],
      noGlossaryFiles: ['./docs/terminology/agent.md'],
      glossaryTermPatterns: ['concept']
    }
  ]
]
```

## To Do

- [ ] Include addtional options in the `Term` tag to control classes
- See [TODO.md](TODO.md) for more details

## Original Author

This plugin has been originally developed by Thanasis Katsadas <kathan@admin.grnet.gr> and the Greek Government. 
The original work is still available on [github.com/grnet/docusaurus-terminology](https://github.com/grnet/docusaurus-terminology).