diff --git a/.changeset/quiet-bananas-sin.md b/.changeset/quiet-bananas-sin.md new file mode 100644 index 000000000..36cb0d357 --- /dev/null +++ b/.changeset/quiet-bananas-sin.md @@ -0,0 +1,6 @@ +--- +'myst-cli': patch +'myst-transforms': patch +--- + +Add transform for gated directives diff --git a/.changeset/short-wolves-warn.md b/.changeset/short-wolves-warn.md new file mode 100644 index 000000000..80bff50b1 --- /dev/null +++ b/.changeset/short-wolves-warn.md @@ -0,0 +1,5 @@ +--- +'myst-transforms': patch +--- + +Unnest links from cross references diff --git a/.changeset/sweet-rocks-lie.md b/.changeset/sweet-rocks-lie.md new file mode 100644 index 000000000..c52ed70aa --- /dev/null +++ b/.changeset/sweet-rocks-lie.md @@ -0,0 +1,7 @@ +--- +'myst-cli': patch +'myst-ext-exercise': patch +'myst-transforms': patch +--- + +Initial support for sphinx-exercise diff --git a/docs/_toc.yml b/docs/_toc.yml index 0271c84e5..b34109d11 100644 --- a/docs/_toc.yml +++ b/docs/_toc.yml @@ -20,6 +20,7 @@ parts: - file: external-references - file: citations - file: proofs-and-theorems + - file: exercises - file: blocks - file: diagrams - file: dropdowns-cards-and-tabs diff --git a/docs/exercises.md b/docs/exercises.md new file mode 100644 index 000000000..c27c7e498 --- /dev/null +++ b/docs/exercises.md @@ -0,0 +1,343 @@ +--- +title: Exercises and Solutions +short_title: Exercises +description: MyST supports adding exercises and solutions which can cross-reference each-other and include Jupyter Notebook outputs. +thumbnail: ./thumbnails/exercise.png +--- + +There are two directives available to add exercises and solutions to your documents: (1) an `exercise` directive; and (2) a `solution` directive. The exercises are enumerated by default and can take in an optional title argument as well as be "gated" around Jupyter Notebook cells. + +:::{note} Same as Sphinx Exercise 🎉 +:class: dropdown + +The implementation and documentation for exercises and solutions is based on [Sphinx Exercise](https://ebp-sphinx-exercise.readthedocs.io), the syntax can be used interchangeably. We have reused the examples in that extension here to show off the various parts of the MyST extension. + +Changes to the original extension include being able to click on the exercise label (e.g. "Exercise 1"), and having a link to that exercise anchor. We have also updated the styles from both Sphinx and JupyterBook to be more distinct from admonitions. + +You can also reference exercises with any cross-reference syntax (including the `{ref}` and `{numref}` roles). We recommend the markdown link syntax. +::: + +## Exercise Directive + +**Example** + +```{exercise} +:label: my-exercise + +Recall that $n!$ is read as "$n$ factorial" and defined as +$n! = n \times (n - 1) \times \cdots \times 2 \times 1$. + +There are functions to compute this in various modules, but let's +write our own version as an exercise. + +In particular, write a function `factorial` such that `factorial(n)` returns $n!$ +for any positive integer $n$. +``` + +**MyST Syntax** + +````markdown +```{exercise} +:label: my-exercise + +Recall that $n!$ is read as "$n$ factorial" and defined as +$n! = n \times (n - 1) \times \cdots \times 2 \times 1$. + +There are functions to compute this in various modules, but let's +write our own version as an exercise. + +In particular, write a function `factorial` such that `factorial(n)` returns $n!$ +for any positive integer $n$. +``` +```` + +_Source:_ [QuantEcon](https://python-programming.quantecon.org/functions.html#Exercise-1) + +The following options for exercise and solution directives are supported: + +- `label`: text + + A unique identifier for your exercise that you can use to reference it with a Markdown link or `{ref}` and `{numref}` roles. Cannot contain spaces or special characters. + +- `class`: text + + Value of the exercise’s class attribute which can be used to add custom CSS or JavaScript. This can also be the optional `dropdown` class to initially hide the exercise. + +- `nonumber`: flag (empty) + + Turns off exercise auto numbering. + +- `hidden` : flag (empty) + + Removes the directive from the final output. + +## Solution Directive + +A solution directive can be included using the `solution` pattern. It takes in the label of the directive it wants to link to as a required argument. Unlike the `exercise` directive, the solution directive is not enumerable as it inherits numbering directly from the linked exercise. The argument for a solution is the label of the linked exercise, which is required. + +**Example** + +````{solution} my-exercise +:label: my-solution + +Here's one solution. + +```{code-block} python +def factorial(n): + k = 1 + for i in range(n): + k = k * (i + 1) + return k + +factorial(4) +``` +```` + +**MyST Syntax** + +`````markdown +````{solution} my-exercise +:label: my-solution + +Here's one solution. + +```{code-block} python +def factorial(n): + k = 1 + for i in range(n): + k = k * (i + 1) + return k + +factorial(4) +``` +```` +````` + +_Source:_ [QuantEcon](https://python-programming.quantecon.org/functions.html#Exercise-1) + +The following options are also supported: + +- `label` : text + + A unique identifier for your solution that you can use to reference it with `{ref}`. Cannot contain spaces or special characters. + +- `class` : text + + Value of the solution’s class attribute which can be used to add custom CSS or JavaScript. + +- `hidden` : flag (empty) + + Removes the directive from the final output. + +## Referencing Exercises & Solutions + +You can refer to an exercise using the standard link syntax: + +- `[](#my-exercise)`, creates [](#my-exercise) +- `[{name}](#nfactorial)`[^note] creates [{name}](#nfactorial) +- `[{number}](#my-exercise)` creates [{number}](#my-exercise) +- `[See Exercise](#my-exercise)` creates [See Exercise](#my-exercise) + +:::{tip} Compatibility with Sphinx Exercise +:class: dropdown + +You can also refer to an exercise using the `{ref}` role like `` {ref}`my-exercise` ``, which will display the title of the exercise directive. In the event that directive does not have a title, the title will be the default "Exercise" or "Exercise {number}" like so: {ref}`my-exercise`. + +Enumerable directives can also be referenced through the `numref` role like `` {numref}`my-exercise` ``, which will display the number of the exercise directive. Referencing the above directive will display {numref}`my-exercise`. In this case it displays the same result as the `{ref}` role as `exerise` notes are (by default) enumerated. + +Furthermore, `numref` can take in three additional placeholders for more customized titles: + +1. _%s_ +2. _{number}_ which get replaced by the exercise number, and +3. _{name}_ by the exercise title.[^note] + +For example,\ +`` {numref}`My custom {number} and {name}` ``. + +[^note]: If the exercise directive does not have a title, the `label` will be used instead. + +::: + +### Referencing Solutions + +You can refer to a solution directly as well using a Markdown link or using the `{ref}` role like: `` {ref}`my-solution` `` the output of which depends on the attributes of the linked directive. If the linked directive is enumerable, the role will replace the solution reference with the linked directive type and its appropriate number like so: {ref}`my-solution`. + +In the event that the directive being referenced is unenumerable, the reference will display its title: {ref}`nfactorial-solution`. + +:::{note} Named Exercise & Solution +:class: dropdown simple +:icon: false + +```{exercise} $n!$ Factorial +:label: nfactorial +:nonumber: + +Write a function `factorial` such that `factorial(int n)` returns $n!$ +for any positive integer $n$. +``` + +````{solution} nfactorial +:label: nfactorial-solution + +Here's a solution in Java. + +```{code-block} java +static int factorial(int n){ + if (n == 0) + return 1; + else { + return(n * factorial(n-1)); + } +} +```` + +::: + +If the title of the linked directive being reference does not exist, it will default to {ref}`nfactorial-notitle-solution`. + +:::{note} Unnumbered Exercise & Solution +:class: dropdown simple +:icon: false + +```{exercise} +:label: nfactorial-notitle +:nonumber: + +Write a function `factorial` such that `factorial(int n)` returns $n!$ +for any positive integer $n$. +``` + +````{solution} nfactorial-notitle +:label: nfactorial-notitle-solution + +Here's a solution in Java. + +```{code-block} java +static int factorial(int n){ + if (n == 0) + return 1; + else { + return(n * factorial(n-1)); + } +} +```` + +::: + +## Alternative Gated Syntax + +To be able to be viewed as Jupyter Notebooks (e.g. in [JupyterLab MyST](./quickstart-jupyter-lab-myst.md)) `code-cell` directives must be at the root level of the document for them to be executed. This maintains direct compatibility with the `jupyter notebook` and enables tools like `jupytext` to convert between `myst` and `ipynb` files. + +As a result **executable** `code-cell` directives cannot be nested inside of exercises or solution directives. + +The solution to this is to use the **gated syntax**. + +```{note} +This syntax can also be a convenient way of surrounding blocks of text that may include other directives that you wish +to include in an exercise or solution admonition. +``` + +**Basic Syntax** + +````markdown +```{exercise-start} +:label: ex1 +``` + +```{code-cell} +# Some setup code that needs executing +``` + +and maybe you wish to add a figure + +```{figure} https://source.unsplash.com/random/400x200?beach,ocean + +``` + +```{exercise-end} + +``` +```` + +```{exercise-start} +:label: ex1 +``` + +```{code-cell} +# Some setup code that needs executing +``` + +and maybe you wish to add a figure + +```{figure} https://source.unsplash.com/random/400x200?beach,ocean + +``` + +```{exercise-end} + +``` + +This can also be completed for solutions with `solution-start` and `solution-end` directives. The `solution-start` and `exercise-start` directives have the same options as original directive. + +```{warning} Mismatched Start & End +:class: dropdown +If there are missing `-start` and `-end` directives, this will cause an extension error, +alongside feedback to diagnose the issue in document structure. +``` + +## Hiding Directive Content + +To visually hide the content, simply add `:class: dropdown` as a directive option, similar to an admonition. + +**Example** + +```{exercise} +:class: dropdown + +Recall that $n!$ is read as "$n$ factorial" and defined as +$n! = n \times (n - 1) \times \cdots \times 2 \times 1$. + +There are functions to compute this in various modules, but let's +write our own version as an exercise. + +In particular, write a function `factorial` such that `factorial(n)` returns $n!$ +for any positive integer $n$. +``` + +**MyST Syntax**: + +````markdown +```{exercise} +:class: dropdown + +Recall that $n!$ is read as "$n$ factorial" and defined as +$n! = n \times (n - 1) \times \cdots \times 2 \times 1$. + +There are functions to compute this in various modules, but let's +write our own version as an exercise. + +In particular, write a function `factorial` such that `factorial(n)` returns $n!$ +for any positive integer $n$. +``` +```` + +### Remove Directives + +Any specific directive can be hidden by introducing the `:hidden:` option. For example, the following example will not be displayed + +````markdown +```{exercise} +:hidden: + +This is a hidden exercise directive. +``` +```` + +```{exercise} +:hidden: + +This is a hidden exercise directive. +``` + +% TODO: Remove All Solutions +% TODO: Custom CSS diff --git a/docs/proofs-and-theorems.md b/docs/proofs-and-theorems.md index 4d98f90f1..d727ffe86 100644 --- a/docs/proofs-and-theorems.md +++ b/docs/proofs-and-theorems.md @@ -42,7 +42,7 @@ The following options for proof directives are supported: - `label`: text - A unique identifier for your theorem that you can use to reference it with `{prf:ref}`. Cannot contain spaces or special characters. + A unique identifier for your theorem that you can use to reference it with a Markdown link or the `{prf:ref}` role. Cannot contain spaces or special characters. - `class`: text diff --git a/docs/thumbnails/exercise.png b/docs/thumbnails/exercise.png new file mode 100644 index 000000000..514973e24 Binary files /dev/null and b/docs/thumbnails/exercise.png differ diff --git a/package-lock.json b/package-lock.json index f3bb168d2..b5dfb34b8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9170,6 +9170,10 @@ "resolved": "packages/myst-ext-card", "link": true }, + "node_modules/myst-ext-exercise": { + "resolved": "packages/myst-ext-exercise", + "link": true + }, "node_modules/myst-ext-grid": { "resolved": "packages/myst-ext-grid", "link": true @@ -13201,6 +13205,7 @@ "myst-common": "^0.0.17", "myst-config": "^0.0.15", "myst-ext-card": "^0.0.7", + "myst-ext-exercise": "^0.0.1", "myst-ext-grid": "^0.0.7", "myst-ext-proof": "^0.0.2", "myst-ext-reactive": "^0.0.7", @@ -13515,6 +13520,25 @@ "typescript": "latest" } }, + "packages/myst-ext-exercise": { + "version": "0.0.1", + "license": "MIT", + "dependencies": { + "myst-common": "^0.0.17" + }, + "devDependencies": { + "@types/jest": "^28.1.6", + "eslint": "^8.21.0", + "eslint-config-curvenote": "latest", + "jest": "28.1.3", + "myst-parser": "^0.0.30", + "npm-run-all": "^4.1.5", + "prettier": "latest", + "rimraf": "^3.0.2", + "ts-jest": "^28.0.7", + "typescript": "latest" + } + }, "packages/myst-ext-grid": { "version": "0.0.7", "license": "MIT", @@ -20405,6 +20429,7 @@ "myst-common": "^0.0.17", "myst-config": "^0.0.15", "myst-ext-card": "^0.0.7", + "myst-ext-exercise": "^0.0.1", "myst-ext-grid": "^0.0.7", "myst-ext-proof": "^0.0.2", "myst-ext-reactive": "^0.0.7", @@ -20624,6 +20649,22 @@ "typescript": "latest" } }, + "myst-ext-exercise": { + "version": "file:packages/myst-ext-exercise", + "requires": { + "@types/jest": "^28.1.6", + "eslint": "^8.21.0", + "eslint-config-curvenote": "latest", + "jest": "28.1.3", + "myst-common": "^0.0.17", + "myst-parser": "^0.0.30", + "npm-run-all": "^4.1.5", + "prettier": "latest", + "rimraf": "^3.0.2", + "ts-jest": "^28.0.7", + "typescript": "latest" + } + }, "myst-ext-grid": { "version": "file:packages/myst-ext-grid", "requires": { diff --git a/packages/myst-cli/package.json b/packages/myst-cli/package.json index 277e73a3f..61a1e5fe0 100644 --- a/packages/myst-cli/package.json +++ b/packages/myst-cli/package.json @@ -77,6 +77,7 @@ "myst-config": "^0.0.15", "myst-ext-card": "^0.0.7", "myst-ext-grid": "^0.0.7", + "myst-ext-exercise": "^0.0.1", "myst-ext-proof": "^0.0.2", "myst-ext-reactive": "^0.0.7", "myst-ext-tabs": "^0.0.7", diff --git a/packages/myst-cli/src/process/mdast.ts b/packages/myst-cli/src/process/mdast.ts index 416e1d04c..137c74e78 100644 --- a/packages/myst-cli/src/process/mdast.ts +++ b/packages/myst-cli/src/process/mdast.ts @@ -21,6 +21,7 @@ import { GithubTransformer, RRIDTransformer, DOITransformer, + joinGatesPlugin, } from 'myst-transforms'; import { unified } from 'unified'; import { VFile } from 'vfile'; @@ -136,6 +137,7 @@ export async function transformMdast( .use(htmlPlugin, { htmlHandlers }) .use(mathPlugin, { macros: frontmatter.math }) .use(enumerateTargetsPlugin, { state }) // This should be after math + .use(joinGatesPlugin) .run(mdast, vfile); // Run the link transformations that can be done without knowledge of other files diff --git a/packages/myst-cli/src/process/myst.ts b/packages/myst-cli/src/process/myst.ts index f850a694e..0f6a87bd0 100644 --- a/packages/myst-cli/src/process/myst.ts +++ b/packages/myst-cli/src/process/myst.ts @@ -3,6 +3,7 @@ import { mystParse } from 'myst-parser'; import { cardDirective } from 'myst-ext-card'; import { gridDirective } from 'myst-ext-grid'; import { proofDirective } from 'myst-ext-proof'; +import { exerciseDirectives } from 'myst-ext-exercise'; import { reactiveDirective, reactiveRole } from 'myst-ext-reactive'; import { tabDirectives } from 'myst-ext-tabs'; import { VFile } from 'vfile'; @@ -14,7 +15,14 @@ export function parseMyst(session: ISession, content: string, file: string): Roo vfile.path = file; const parsed = mystParse(content, { markdownit: { linkify: true }, - directives: [cardDirective, gridDirective, reactiveDirective, proofDirective, ...tabDirectives], + directives: [ + cardDirective, + gridDirective, + reactiveDirective, + proofDirective, + ...exerciseDirectives, + ...tabDirectives, + ], roles: [reactiveRole], vfile, }); diff --git a/packages/myst-ext-exercise/.eslintrc.js b/packages/myst-ext-exercise/.eslintrc.js new file mode 100644 index 000000000..76787609a --- /dev/null +++ b/packages/myst-ext-exercise/.eslintrc.js @@ -0,0 +1,4 @@ +module.exports = { + root: true, + extends: ['curvenote'], +}; diff --git a/packages/myst-ext-exercise/CHANGELOG.md b/packages/myst-ext-exercise/CHANGELOG.md new file mode 100644 index 000000000..0f84ff905 --- /dev/null +++ b/packages/myst-ext-exercise/CHANGELOG.md @@ -0,0 +1 @@ +# myst-ext-exercise diff --git a/packages/myst-ext-exercise/README.md b/packages/myst-ext-exercise/README.md new file mode 100644 index 000000000..c1a0bbc2a --- /dev/null +++ b/packages/myst-ext-exercise/README.md @@ -0,0 +1,3 @@ +# myst-ext-exercise + +`mystjs` extension for `exercise` directive diff --git a/packages/myst-ext-exercise/jest.config.js b/packages/myst-ext-exercise/jest.config.js new file mode 100644 index 000000000..816f544aa --- /dev/null +++ b/packages/myst-ext-exercise/jest.config.js @@ -0,0 +1,23 @@ +module.exports = { + rootDir: '../../', + preset: 'ts-jest/presets/js-with-ts', + testMatch: ['/packages/myst-ext-exercise/**/?(*.)+(spec|test).+(ts|tsx|js)'], + transform: { + '^.+\\.(ts|tsx)$': 'ts-jest', + }, + testTimeout: 10000, + moduleNameMapper: { + '#(.*)': '/node_modules/$1', // https://github.com/chalk/chalk/issues/532 + }, + globals: { + 'ts-jest': { + tsconfig: './tsconfig.test.json', + }, + }, + verbose: true, + testEnvironment: 'node', + transformIgnorePatterns: [ + '/node_modules/(?!(vfile|formdata-polyfill|chalk|fetch-blob|vfile-message|unified|bail|trough|zwitch|unist-|hast-|html-|rehype-|mdast-|micromark-|trim-|web-namespaces|property-information|space-separated-tokens|comma-separated-tokens|get-port|stringify-entities|character-entities-html4|ccount|array-iterate))', + ], + testPathIgnorePatterns: ['/node_modules/', '/.yalc/', '/dist/'], +}; diff --git a/packages/myst-ext-exercise/package.json b/packages/myst-ext-exercise/package.json new file mode 100644 index 000000000..d5d29f7f4 --- /dev/null +++ b/packages/myst-ext-exercise/package.json @@ -0,0 +1,57 @@ +{ + "name": "myst-ext-exercise", + "version": "0.0.1", + "sideEffects": false, + "license": "MIT", + "description": "MyST extension for exercise", + "author": "Rowan Cockett ", + "homepage": "https://github.com/executablebooks/mystjs/tree/main/packages/myst-ext-exercise", + "main": "./dist/cjs/index.js", + "module": "./dist/esm/index.js", + "types": "./dist/types/index.d.ts", + "files": [ + "dist" + ], + "exports": { + ".": { + "import": "./dist/esm/index.js", + "require": "./dist/cjs/index.js" + } + }, + "publishConfig": { + "access": "public" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/executablebooks/mystjs.git" + }, + "scripts": { + "clean": "rimraf dist", + "build:esm": "tsc --project ./tsconfig.json --module es2015 --outDir dist/esm", + "build:cjs": "tsc --project ./tsconfig.json --module commonjs --outDir dist/cjs", + "declarations": "tsc --project ./tsconfig.json --declaration --emitDeclarationOnly --declarationMap --outDir dist/types", + "build": "npm-run-all -l clean -p build:cjs build:esm declarations", + "lint": "eslint \"src/**/!(*.spec).ts\" -c ./.eslintrc.js", + "lint:format": "npx prettier --check \"src/**/*.ts\"", + "test": "jest", + "test:watch": "jest --watchAll" + }, + "bugs": { + "url": "https://github.com/executablebooks/mystjs/issues" + }, + "dependencies": { + "myst-common": "^0.0.17" + }, + "devDependencies": { + "@types/jest": "^28.1.6", + "eslint": "^8.21.0", + "eslint-config-curvenote": "latest", + "jest": "28.1.3", + "myst-parser": "^0.0.30", + "npm-run-all": "^4.1.5", + "prettier": "latest", + "rimraf": "^3.0.2", + "ts-jest": "^28.0.7", + "typescript": "latest" + } +} diff --git a/packages/myst-ext-exercise/src/exercise.ts b/packages/myst-ext-exercise/src/exercise.ts new file mode 100644 index 000000000..278d3776a --- /dev/null +++ b/packages/myst-ext-exercise/src/exercise.ts @@ -0,0 +1,127 @@ +import type { DirectiveSpec, DirectiveData, GenericNode } from 'myst-common'; +import { createId, normalizeLabel, ParseTypesEnum } from 'myst-common'; + +export const exerciseDirective: DirectiveSpec = { + name: 'exercise', + alias: ['exercise-start'], + arg: { + type: ParseTypesEnum.parsed, + }, + options: { + label: { + type: ParseTypesEnum.string, + }, + class: { + type: ParseTypesEnum.string, + }, + nonumber: { + type: ParseTypesEnum.boolean, + }, + hidden: { + type: ParseTypesEnum.boolean, + }, + }, + body: { + type: ParseTypesEnum.parsed, + }, + run(data: DirectiveData): GenericNode[] { + const children: GenericNode[] = []; + if (data.arg) { + children.push({ + type: 'admonitionTitle', + children: data.arg as GenericNode[], + }); + } + if (data.body) { + children.push(...(data.body as GenericNode[])); + } + const nonumber = (data.options?.nonumber as boolean) ?? false; + // Numbered, unlabeled exercises still need a label + const backupLabel = nonumber ? undefined : `exercise-${createId()}`; + const rawLabel = (data.options?.label as string) || backupLabel; + const { label, identifier } = normalizeLabel(rawLabel) || {}; + const exercise: GenericNode = { + type: 'exercise', + label, + identifier, + class: data.options?.class as string, + hidden: data.options?.hidden as boolean, + enumerated: !nonumber, + children: children as any[], + }; + if (data.name.endsWith('-start')) { + exercise.gate = 'start'; + } + return [exercise]; + }, +}; + +export const solutionDirective: DirectiveSpec = { + name: 'solution', + alias: ['solution-start'], + arg: { + type: ParseTypesEnum.string, + required: true, + }, + options: { + label: { + type: ParseTypesEnum.string, + }, + class: { + type: ParseTypesEnum.string, + }, + hidden: { + type: ParseTypesEnum.boolean, + }, + }, + body: { + type: ParseTypesEnum.parsed, + }, + run(data: DirectiveData): GenericNode[] { + const children: GenericNode[] = []; + if (data.arg) { + const { label, identifier } = normalizeLabel(data.arg as string) || {}; + children.push({ + type: 'admonitionTitle', + children: [ + { type: 'text', value: 'Solution to ' }, + { type: 'crossReference', label, identifier }, + ], + }); + } + if (data.body) { + children.push(...(data.body as GenericNode[])); + } + const rawLabel = data.options?.label as string; + const { label, identifier } = normalizeLabel(rawLabel) || {}; + const solution: GenericNode = { + type: 'solution', + label, + identifier, + class: data.options?.class as string, + hidden: data.options?.hidden as boolean, + children: children as any[], + }; + if (data.name.endsWith('-start')) { + solution.gate = 'start'; + } + return [solution]; + }, +}; + +export const solutionEndDirective: DirectiveSpec = { + name: 'solution-end', + run: () => [{ type: 'solution', gate: 'end' }], +}; + +export const exerciseEndDirective: DirectiveSpec = { + name: 'exercise-end', + run: () => [{ type: 'exercise', gate: 'end' }], +}; + +export const exerciseDirectives = [ + exerciseDirective, + exerciseEndDirective, + solutionDirective, + solutionEndDirective, +]; diff --git a/packages/myst-ext-exercise/src/index.ts b/packages/myst-ext-exercise/src/index.ts new file mode 100644 index 000000000..bea8a9d3e --- /dev/null +++ b/packages/myst-ext-exercise/src/index.ts @@ -0,0 +1 @@ +export { exerciseDirective, solutionDirective, exerciseDirectives } from './exercise'; diff --git a/packages/myst-ext-exercise/tests/exercise.spec.ts b/packages/myst-ext-exercise/tests/exercise.spec.ts new file mode 100644 index 000000000..92cb91e96 --- /dev/null +++ b/packages/myst-ext-exercise/tests/exercise.spec.ts @@ -0,0 +1,74 @@ +import { mystParse } from 'myst-parser'; +import { exerciseDirective } from 'myst-ext-exercise'; + +describe('exercise directive', () => { + it('exercise directive parses', async () => { + const content = '```{exercise} Exercise Title\n:label: ex-1\nExercise content\n```'; + const expected = { + type: 'root', + children: [ + { + type: 'mystDirective', + name: 'exercise', + options: { + label: 'ex-1', + }, + args: 'Exercise Title', + value: 'Exercise content', + position: { + start: { + line: 0, + column: 0, + }, + end: { + line: 4, + column: 0, + }, + }, + children: [ + { + type: 'exercise', + enumerated: true, + identifier: 'ex-1', + label: 'ex-1', + children: [ + { + type: 'admonitionTitle', + children: [ + { + type: 'text', + value: 'Exercise Title', + }, + ], + }, + { + type: 'paragraph', + children: [ + { + type: 'text', + value: 'Exercise content', + }, + ], + position: { + end: { + column: 0, + line: 3, + }, + start: { + column: 0, + line: 2, + }, + }, + }, + ], + }, + ], + }, + ], + }; + const output = mystParse(content, { + directives: [exerciseDirective], + }); + expect(output).toEqual(expected); + }); +}); diff --git a/packages/myst-ext-exercise/tsconfig.json b/packages/myst-ext-exercise/tsconfig.json new file mode 100644 index 000000000..8268fe093 --- /dev/null +++ b/packages/myst-ext-exercise/tsconfig.json @@ -0,0 +1,33 @@ +{ + "compilerOptions": { + "target": "es6", + // module is overridden from the build:esm/build:cjs scripts + "module": "es2015", + "jsx": "react-jsx", + "lib": ["es2020"], + "esModuleInterop": true, + "noImplicitAny": true, + "strict": true, + "moduleResolution": "node", + "sourceMap": false, + // outDir is overridden from the build:esm/build:cjs scripts + "outDir": "dist/types", + "baseUrl": "src", + "paths": { + "*": ["node_modules/*"] + }, + // Type roots allows it to be included in a workspace + "typeRoots": [ + "./types", + "./node_modules/@types", + "../../node_modules/@types", + "../../../node_modules/@types" + ], + "resolveJsonModule": true, + // Ignore node_modules, etc. + "skipLibCheck": true, + "forceConsistentCasingInFileNames": true + }, + "include": ["src/**/*"], + "exclude": ["tests/**/*"] +} diff --git a/packages/myst-ext-exercise/tsconfig.test.json b/packages/myst-ext-exercise/tsconfig.test.json new file mode 100644 index 000000000..bafe01bbd --- /dev/null +++ b/packages/myst-ext-exercise/tsconfig.test.json @@ -0,0 +1,8 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "allowJs": true, + "target": "es6" + }, + "exclude": [] +} diff --git a/packages/myst-transforms/src/enumerate.ts b/packages/myst-transforms/src/enumerate.ts index 822e8a623..6dc59466e 100644 --- a/packages/myst-transforms/src/enumerate.ts +++ b/packages/myst-transforms/src/enumerate.ts @@ -5,7 +5,15 @@ import type { Container, CrossReference, Heading, Link, Math, Paragraph } from ' import { visit } from 'unist-util-visit'; import { select, selectAll } from 'unist-util-select'; import { findAndReplace } from 'mdast-util-find-and-replace'; -import { createHtmlId, fileWarn, normalizeLabel, setTextAsChild, copyNode } from 'myst-common'; +import type { GenericNode } from 'myst-common'; +import { + createHtmlId, + fileWarn, + normalizeLabel, + setTextAsChild, + copyNode, + liftChildren, +} from 'myst-common'; const TRANSFORM_NAME = 'myst-transforms:enumerate'; @@ -49,6 +57,20 @@ function getDefaultNumberedReferenceLabel(kind: TargetKind | string) { } } +function getDefaultNamedReferenceLabel(kind: TargetKind | string, hasTitle: boolean) { + const domain = kind.includes(':') ? kind.split(':')[1] : kind; + const name = `${domain.slice(0, 1).toUpperCase()}${domain.slice(1)}`; + switch (kind) { + // TODO: These need to be moved to the directive definition in an extension + case 'proof': + case 'exercise': + return hasTitle ? `${name} ({name})` : name; + default: + if (hasTitle) return '{name}'; + return name; + } +} + export enum ReferenceKind { ref = 'ref', numref = 'numref', @@ -366,7 +388,7 @@ export class ReferenceState implements IReferenceState { } const template = target.node.enumerator ? getDefaultNumberedReferenceLabel(target.kind) - : '{name}'; + : getDefaultNamedReferenceLabel(target.kind, !!title); fillReferenceEnumerators(this.file, node, template, target.node.enumerator, title); } node.resolved = true; @@ -449,7 +471,7 @@ export class MultiPageReferenceState implements IReferenceState { export const enumerateTargetsTransform = (tree: Root, opts: StateOptions) => { opts.state.initializeNumberedHeadingDepths(tree); - const nodes = selectAll('container,math,heading,proof,[identifier]', tree) as ( + const nodes = selectAll('container,math,heading,proof,[identifier],[enumerated=true]', tree) as ( | TargetNodes | IdentifierNodes )[]; @@ -548,6 +570,22 @@ export const resolveReferenceLinksTransform = (tree: Root, opts: StateOptions) = }); }; +/** Cross references cannot contain links, but should retain their content */ +function unnestCrossReferencesTransform(tree: Root) { + const xrefs = selectAll('crossReference', tree) as GenericNode[]; + xrefs.forEach((xref) => { + const children = xref.children as any; + if (!children) return; + const subtree = { type: 'root', children: copyNode(children) } as any; + const nested = select('crossReference,link', subtree); + if (!nested) return; + liftChildren(subtree, 'link'); + liftChildren(subtree, 'crossReference'); + xref.children = subtree.children; + }); + return tree.children as PhrasingContent[]; +} + export const resolveCrossReferencesTransform = (tree: Root, opts: StateOptions) => { visit(tree, 'crossReference', (node: CrossReference) => { opts.state.resolveReferenceContent(node); @@ -558,6 +596,7 @@ export const resolveReferencesTransform = (tree: Root, file: VFile, opts: StateO resolveReferenceLinksTransform(tree, opts); resolveCrossReferencesTransform(tree, opts); addContainerCaptionNumbersTransform(tree, file, opts); + unnestCrossReferencesTransform(tree); }; export const resolveReferencesPlugin: Plugin<[StateOptions], Root, Root> = diff --git a/packages/myst-transforms/src/index.ts b/packages/myst-transforms/src/index.ts index 8299c38b7..e8fc02c8f 100644 --- a/packages/myst-transforms/src/index.ts +++ b/packages/myst-transforms/src/index.ts @@ -39,6 +39,7 @@ export { headingLabelPlugin, headingLabelTransform, } from './targets'; +export { joinGatesPlugin, joinGatesTransform } from './joinGates'; // Enumeration export type { IReferenceState, NumberingOptions, TargetKind, ReferenceKind } from './enumerate'; diff --git a/packages/myst-transforms/src/joinGates.spec.ts b/packages/myst-transforms/src/joinGates.spec.ts new file mode 100644 index 000000000..364791c56 --- /dev/null +++ b/packages/myst-transforms/src/joinGates.spec.ts @@ -0,0 +1,56 @@ +import { unified } from 'unified'; +import { VFile } from 'vfile'; +import { joinGatesPlugin } from './joinGates'; + +describe('Test gate directive joining', () => { + test('Test basic gate', () => { + const file = new VFile(); + const open = { type: 'node', gate: 'start' } as any; + const paragraph = { type: 'paragraph' } as any; + const close = { type: 'node', gate: 'end' } as any; + const mdast = { type: 'root', children: [open, paragraph, close] } as any; + unified().use(joinGatesPlugin).runSync(mdast, file); + expect(file.messages.length).toBe(0); + expect(open.gate).toBeUndefined(); + expect(mdast.children.length).toBe(1); + expect(open.children[0]).toBe(paragraph); + }); + test('Test mismatch gate', () => { + const file = new VFile(); + const open = { type: 'node', gate: 'start' } as any; + const paragraph = { type: 'paragraph' } as any; + const close = { type: 'not-the-same-node', gate: 'end' } as any; + const mdast = { type: 'root', children: [open, paragraph, close] } as any; + unified().use(joinGatesPlugin).runSync(mdast, file); + expect(file.messages.length).toBe(1); // Raise warning + expect(open.gate).toBeUndefined(); + expect(mdast.children.length).toBe(1); + expect(open.children[0]).toBe(paragraph); + }); + test('Test gate without end', () => { + const file = new VFile(); + const open = { type: 'node', gate: 'start' } as any; + const paragraph = { type: 'paragraph' } as any; + const mdast = { type: 'root', children: [open, paragraph] } as any; + unified().use(joinGatesPlugin).runSync(mdast, file); + expect(file.messages.length).toBe(1); // Raise error + expect(open.gate).toBe('start'); + expect(mdast.children.length).toBe(1); + expect(open.children[0]).toBe(paragraph); + }); + test('Test nested gates', () => { + const file = new VFile(); + const open = { type: 'node', gate: 'start' } as any; + const open2 = { type: 'node', gate: 'start' } as any; + const paragraph = { type: 'paragraph' } as any; + const close = { type: 'node', gate: 'end' } as any; + const close2 = { type: 'node', gate: 'end' } as any; + const mdast = { type: 'root', children: [open, open2, paragraph, close, close2] } as any; + unified().use(joinGatesPlugin).runSync(mdast, file); + expect(file.messages.length).toBe(0); + expect(open.gate).toBeUndefined(); + expect(mdast.children.length).toBe(1); + expect(open.children[0]).toBe(open2); + expect(open2.children[0]).toBe(paragraph); + }); +}); diff --git a/packages/myst-transforms/src/joinGates.ts b/packages/myst-transforms/src/joinGates.ts new file mode 100644 index 000000000..b6da78c4d --- /dev/null +++ b/packages/myst-transforms/src/joinGates.ts @@ -0,0 +1,55 @@ +import type { GenericNode, GenericParent } from 'myst-common'; +import { fileWarn, fileError } from 'myst-common'; +import { map } from 'unist-util-map'; +import type { Root } from 'mdast'; +import type { Plugin } from 'unified'; +import type { VFile } from 'vfile'; + +export function joinGatesTransform(tree: GenericParent, file: VFile) { + map(tree, (node) => { + const nodes = ((node as GenericParent).children as GenericParent[])?.reduce( + (children, child) => { + const [last] = children.slice(-1); + const [secondLast] = children.slice(-2); + if (last?.gate !== 'start') return [...children, child]; + if (child.gate === 'start') { + // If we are opening a new gate, add to the stack and the logic below will close it + return [...children, child]; + } + if (child.gate === 'end') { + if (child.type !== last.type) { + fileWarn( + file, + `Gate close ("${child.type}") does not match opening gate (${child.gate}).`, + { node }, + ); + } + // Clean up the gate logic + delete last.gate; + if (secondLast?.gate === 'start') { + // We have two or more open gates (from above), close the current one and append the child + const closed = children.pop() as GenericNode; + secondLast.children = [...(secondLast.children ?? []), closed]; + } + return children; + } + // Append the child to the open gate if no end is found + last.children = [...(last.children ?? []), child]; + return children; + }, + [] as GenericNode[], + ); + const [last] = nodes?.slice(-1) ?? []; + if (last?.gate === 'start') { + fileError(file, `Gated node is not closed, expected a {${last.type}-end} directive.`, { + node, + }); + } + if (nodes !== undefined) (node as GenericParent).children = nodes; + return node; + }); +} + +export const joinGatesPlugin: Plugin<[], Root, Root> = () => (tree, file) => { + joinGatesTransform(tree as GenericParent, file); +};