Skip to content

Commit

Permalink
È Index accents (#1476)
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanc1 authored Aug 21, 2024
1 parent 1a5f3f3 commit 60fc257
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 13 deletions.
6 changes: 6 additions & 0 deletions .changeset/gentle-buckets-cheat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"myst-directives": patch
"myst-transforms": patch
---

Ensure é ends up in the E index.
8 changes: 5 additions & 3 deletions docs/glossaries-and-terms.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ short_title: Glossaries, Terms, & Index Pages
You can define Terms and generate reference pages for them with Glossaries and Index Pages. This allows you to centralize definitions and pointers to where various items are mentioned throughout your documents.

:::{seealso} See our index and glossary page

- The [glossary for these docs](#glossary-page)
- The [index for these docs](#index-page)

:::

## Glossaries
Expand All @@ -23,7 +25,7 @@ term
: A term is a [word with a specialized meaning](https://en.wikipedia.org/wiki/Terminology).

index
: An [organized list of information in a publication](https://en.wikipedia.org/wiki/Index_(publishing)).
: An [organized list of information in a publication](<https://en.wikipedia.org/wiki/Index_(publishing)>).

index entry
: A word or phrase that has been marked for inclusion in the index with the `index` directive or role.
Expand Down Expand Up @@ -83,10 +85,10 @@ The existing syntax for `index` directives and `index` roles has been taken dire

You can define index entries with Directives like so:

````
```
:::{index} my first index item
:::
````
```

% This won't show up in the content
:::{index} my first index item
Expand Down
5 changes: 4 additions & 1 deletion docs/glossary.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ description: Glossary of terms used throughout the MyST ecosystem.
---

(glossary-page)=

## Glossary

:::{glossary}
Expand Down Expand Up @@ -66,7 +67,9 @@ and build a variety of outputs for interactivity and document publishing.
:::

(index-page)=

## Index

```{show-index}
```
```
10 changes: 9 additions & 1 deletion packages/myst-transforms/src/indices.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, test } from 'vitest';
import { indexIdentifierTransform } from './indices';
import { getNormalizedFirstLetter, indexIdentifierTransform } from './indices';

describe('Test indexEntry plugin', () => {
test('adds label/identifier/html_id to indexEntries', () => {
Expand All @@ -15,4 +15,12 @@ describe('Test indexEntry plugin', () => {
expect(mdast.children[1].identifier).toBeTruthy();
expect(mdast.children[1].html_id).toBeTruthy();
});
test('index letter', () => {
expect(getNormalizedFirstLetter('éxxx')).toBe('E');
expect(getNormalizedFirstLetter('öxxx')).toBe('O');
expect(getNormalizedFirstLetter('Ñxxx')).toBe('N');
expect(getNormalizedFirstLetter('ø')).toBe('Other');
expect(getNormalizedFirstLetter('100 Steps')).toBe('1');
expect(getNormalizedFirstLetter(' spaces! ')).toBe('S');
});
});
23 changes: 15 additions & 8 deletions packages/myst-transforms/src/indices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,18 @@ import { selectAll } from 'unist-util-select';
import type { IReferenceStateResolver, ReferenceState, Target } from './enumerate.js';
import type { VFile } from 'vfile';

const ALPHABET_UPPER = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
const ALLOWED_INDEX_CHARS = '123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';

/** Changes é to E etc. */
export function getNormalizedFirstLetter(entry: string): string {
if (!entry) return 'Other';
const char = entry
.trim()[0]
.normalize('NFD')
.replace(/\p{Diacritic}/gu, '')
.toUpperCase();
return ALLOWED_INDEX_CHARS.includes(char) ? char : 'Other';
}

/**
* Ensure all nodes with index entries have label/identifier/html_id
Expand Down Expand Up @@ -58,9 +69,7 @@ function organizeTargetEntries(indexTargets: Target[], vfile: VFile) {
> = {};
indexTargets.forEach(({ node }) => {
node.indexEntries?.forEach(({ entry, subEntry, emphasis }) => {
const letter = ALPHABET_UPPER.includes(entry[0]?.toUpperCase())
? entry[0].toUpperCase()
: 'Other';
const letter = getNormalizedFirstLetter(entry);
if (!entryDict[letter]) entryDict[letter] = {};
const indexTargetInfo: IndexTargetInfo = { node, emphasis };
if (!entryDict[letter][entry]) {
Expand Down Expand Up @@ -166,9 +175,7 @@ function resolveIndexPrefix(
value: ', ',
});
}
const xrefLetter = ALPHABET_UPPER.includes(value[0]?.toUpperCase())
? value[0].toLowerCase()
: 'other';
const xrefLetter = getNormalizedFirstLetter(value).toLowerCase();
const xref = {
type: 'crossReference',
identifier: `index-heading-${xrefLetter}`,
Expand Down Expand Up @@ -228,7 +235,7 @@ export function buildIndexTransform(
.map((entryItems) => [...Object.keys(entryItems)])
.flat();
const indexContent: GenericNode[] = [];
[...ALPHABET_UPPER, 'Other'].forEach((letter) => {
[...ALLOWED_INDEX_CHARS, 'Other'].forEach((letter) => {
if (!entryDict[letter]) return;
const term = {
type: 'definitionTerm',
Expand Down

0 comments on commit 60fc257

Please sign in to comment.