-
Notifications
You must be signed in to change notification settings - Fork 171
📚 Add directive to show list of abbreviations across pages #2985
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
haroldcampbell
wants to merge
4
commits into
jupyter-book:main
Choose a base branch
from
haroldcampbell:feat-abbrev-list
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
bbe33ee
Add AST placeholder and tests for abbreviations directive
haroldcampbell 454178d
Add abbreviations list transform
haroldcampbell 51a1605
Add project-wide abbreviations directive build support
haroldcampbell 6b3ee26
Add documentation for abbreviations directive
haroldcampbell File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -46,3 +46,6 @@ yalc.lock | |
|
|
||
| # vim swap files | ||
| *.swp | ||
|
|
||
| # local logs | ||
| logs | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| import { describe, expect, test } from 'vitest'; | ||
| import type { DirectiveData } from 'myst-common'; | ||
| import { abbreviationsDirective } from './abbreviations.js'; | ||
| import { defaultDirectives } from './index.js'; | ||
|
|
||
| function run(data: Partial<DirectiveData> = {}) { | ||
| return abbreviationsDirective.run!({ | ||
| name: 'abbreviations', | ||
| node: {} as any, | ||
| options: {}, | ||
| ...data, | ||
| }); | ||
| } | ||
|
|
||
| function log(arg: any) { | ||
| console.dir(arg, { depth: null }); | ||
| return arg | ||
| } | ||
| describe('abbreviations directive', () => { | ||
| test('creates a placeholder node', () => { | ||
| expect(run()).toEqual([{ type: 'abbreviations', children: [] }]); | ||
| }); | ||
|
|
||
| test('wraps argument content in a heading', () => { | ||
| expect(run({ arg: [{ type: 'text', value: 'Abbreviations' }] })).toEqual([ | ||
| { | ||
| type: 'abbreviations', | ||
| children: [ | ||
| { | ||
| type: 'heading', | ||
| depth: 2, | ||
| enumerated: false, | ||
| children: [{ type: 'text', value: 'Abbreviations' }], | ||
| }, | ||
| ], | ||
| }, | ||
| ]); | ||
| }); | ||
|
|
||
| test('preserves heading arguments', () => { | ||
| const heading = { | ||
| type: 'heading', | ||
| depth: 3, | ||
| children: [{ type: 'text', value: 'Terms' }], | ||
| }; | ||
| expect(run({ arg: [heading] })).toEqual([{ type: 'abbreviations', children: [heading] }]); | ||
| }); | ||
|
|
||
| test('preserves common directive options', () => { | ||
| expect( | ||
| run({ | ||
| options: { | ||
| class: 'compact', | ||
| label: 'abbreviations-list', | ||
| }, | ||
| }), | ||
| ).toEqual([ | ||
| { | ||
| type: 'abbreviations', | ||
| children: [], | ||
| class: 'compact', | ||
| label: 'abbreviations-list', | ||
| identifier: 'abbreviations-list', | ||
| }, | ||
| ]); | ||
| }); | ||
|
|
||
| test('is registered by default', () => { | ||
| expect(defaultDirectives).toContain(abbreviationsDirective); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| import type { DirectiveData, DirectiveSpec, GenericNode } from 'myst-common'; | ||
| import { addCommonDirectiveOptions, commonDirectiveOptions } from './utils.js'; | ||
|
|
||
| export const abbreviationsDirective: DirectiveSpec = { | ||
| name: 'abbreviations', | ||
| doc: 'Inserts a list of known abbreviations in the page.', | ||
| arg: { | ||
| type: 'myst', | ||
| doc: 'Heading to be included with the abbreviations list', | ||
| }, | ||
| options: { | ||
| ...commonDirectiveOptions('abbreviations'), | ||
| }, | ||
| run(data: DirectiveData): GenericNode[] { | ||
| const children: GenericNode[] = []; | ||
| if (data.arg) { | ||
| const parsedArg = data.arg as GenericNode[]; | ||
| if (parsedArg[0]?.type === 'heading') { | ||
| children.push(...parsedArg); | ||
| } else { | ||
| children.push({ | ||
| type: 'heading', | ||
| depth: 2, | ||
| enumerated: false, | ||
| children: parsedArg, | ||
| }); | ||
| } | ||
| } | ||
| const abbreviations = { type: 'abbreviations', children }; | ||
| addCommonDirectiveOptions(data, abbreviations); | ||
| return [abbreviations]; | ||
| }, | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| import { describe, expect, test } from 'vitest'; | ||
| import fs from 'node:fs'; | ||
| import path from 'node:path'; | ||
| import yaml from 'js-yaml'; | ||
| import { abbreviationsListTransform } from '../src'; | ||
| import { abbreviationListChildren } from '../src/abbreviations'; | ||
|
|
||
| type TestFile = { | ||
| cases: TestCase[]; | ||
| }; | ||
| type TestCase = { | ||
| title: string; | ||
| before: any; | ||
| after: any; | ||
| opts?: { | ||
| abbreviations?: Record<string, string | null>; | ||
| }; | ||
| }; | ||
|
|
||
| const fixtures = path.join('tests', 'abbreviations-list.yml'); | ||
| const testYaml = fs.readFileSync(fixtures).toString(); | ||
| const cases = (yaml.load(testYaml) as TestFile).cases; | ||
|
|
||
| describe('abbreviationListChildren', () => { | ||
| test('returns no children without abbreviations', () => { | ||
| expect(abbreviationListChildren()).toEqual([]); | ||
| expect(abbreviationListChildren({})).toEqual([]); | ||
| }); | ||
|
|
||
| test('creates a sorted definition list and omits null values', () => { | ||
| expect( | ||
| abbreviationListChildren({ | ||
| MyST: 'Markedly Structured Text', | ||
| SHRILL: null, | ||
| API: 'Application Programming Interface', | ||
| }), | ||
| ).toEqual([ | ||
| { | ||
| type: 'definitionList', | ||
| children: [ | ||
| { | ||
| type: 'definitionTerm', | ||
| children: [{ type: 'text', value: 'API' }], | ||
| }, | ||
| { | ||
| type: 'definitionDescription', | ||
| children: [ | ||
| { | ||
| type: 'paragraph', | ||
| children: [{ type: 'text', value: 'Application Programming Interface' }], | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| type: 'definitionTerm', | ||
| children: [{ type: 'text', value: 'MyST' }], | ||
| }, | ||
| { | ||
| type: 'definitionDescription', | ||
| children: [ | ||
| { | ||
| type: 'paragraph', | ||
| children: [{ type: 'text', value: 'Markedly Structured Text' }], | ||
| }, | ||
| ], | ||
| }, | ||
| ], | ||
| }, | ||
| ]); | ||
| }); | ||
| }); | ||
|
|
||
| describe('abbreviations list', () => { | ||
| test.each(cases.map((c): [string, TestCase] => [c.title, c]))( | ||
| '%s', | ||
| (_, { before, after, opts }) => { | ||
| abbreviationsListTransform(before, opts); | ||
| expect(yaml.dump(before)).toEqual(yaml.dump(after)); | ||
| }, | ||
| ); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should check this — I think there's a race condition here implying that we need another sync point.
This is also true for the ToC transform I think.
Actually.... it might be fine — we read mdast only to get the custom placeholder node, which is not modified after referencing.