Skip to content

Commit 0dacdb2

Browse files
committed
Math directive with label
* Better typing for options
1 parent ffaa12a commit 0dacdb2

File tree

7 files changed

+69
-12
lines changed

7 files changed

+69
-12
lines changed

src/directives/admonitions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class BaseAdmonition extends Directive {
1818
name: unchanged
1919
}
2020
public title = ""
21-
run(data: IDirectiveData): Token[] {
21+
run(data: IDirectiveData<keyof BaseAdmonition["option_spec"]>): Token[] {
2222
const newTokens: Token[] = []
2323

2424
// we create an overall container, then individual containers for the title and body

src/directives/code.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export class Code extends Directive {
2929
name: unchanged,
3030
class: class_option
3131
}
32-
run(data: IDirectiveData): Token[] {
32+
run(data: IDirectiveData<keyof Code["option_spec"]>): Token[] {
3333
// TODO handle options
3434
this.assert_has_content(data)
3535
const token = this.createToken("fence", "code", 0, {
@@ -68,7 +68,7 @@ export class CodeBlock extends Directive {
6868
name: unchanged,
6969
class: class_option
7070
}
71-
run(data: IDirectiveData): Token[] {
71+
run(data: IDirectiveData<keyof CodeBlock["option_spec"]>): Token[] {
7272
// TODO handle options
7373
this.assert_has_content(data)
7474
const token = this.createToken("fence", "code", 0, {
@@ -89,7 +89,7 @@ export class CodeCell extends Directive {
8989
public has_content = true
9090
public rawOptions = true
9191

92-
run(data: IDirectiveData): Token[] {
92+
run(data: IDirectiveData<keyof CodeCell["option_spec"]>): Token[] {
9393
// TODO store options and the fact that this is a code cell rather than a fence?
9494
const token = this.createToken("fence", "code", 0, {
9595
info: data.args ? data.args[0] : "",

src/directives/images.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export class Image extends Directive {
3838
...shared_option_spec,
3939
align: create_choice(["left", "center", "right", "top", "middle", "bottom"])
4040
}
41-
create_image(data: IDirectiveData): Token {
41+
create_image(data: IDirectiveData<keyof Image["option_spec"]>): Token {
4242
// get URI
4343
const src = uri(data.args[0] || "")
4444

@@ -89,7 +89,7 @@ export class Figure extends Image {
8989
figclass: class_option
9090
}
9191
public has_content = true
92-
run(data: IDirectiveData): Token[] {
92+
run(data: IDirectiveData<keyof Figure["option_spec"]>): Token[] {
9393
const openToken = this.createToken("figure_open", "figure", 1, { map: data.map })
9494
if (data.options.figclass) {
9595
openToken.attrJoin("class", data.options.figclass.join(" "))

src/directives/main.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,7 @@ export interface IDirectiveSpec {
6565
/** if body content is allowed */
6666
has_content?: boolean
6767
/** mapping known option names to conversion functions */
68-
option_spec?: {
69-
[key: string]: OptionSpecConverter
70-
}
68+
option_spec?: Record<string, OptionSpecConverter>
7169
/** If true, do not attempt to validate/convert options. */
7270
rawOptions?: boolean
7371
}
@@ -153,10 +151,10 @@ export class Directive implements IDirectiveSpec {
153151
}
154152

155153
/** Data structure of a directive */
156-
export interface IDirectiveData {
154+
export interface IDirectiveData<T extends string = string> {
157155
map: [number, number]
158156
args: string[]
159-
options: { [key: string]: any }
157+
options: Record<T, any>
160158
body: string
161159
bodyMap: [number, number]
162160
}

src/directives/math.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/** Admonitions to visualise programming codes */
2+
import type Token from "markdown-it/lib/token"
3+
import { Directive, IDirectiveData } from "./main"
4+
import { unchanged } from "./options"
5+
6+
/** Math directive with a label
7+
*/
8+
export class Math extends Directive {
9+
public required_arguments = 0
10+
public optional_arguments = 0
11+
public final_argument_whitespace = false
12+
public has_content = true
13+
public option_spec = {
14+
label: unchanged
15+
}
16+
run(data: IDirectiveData<keyof Math["option_spec"]>): Token[] {
17+
// TODO handle options
18+
this.assert_has_content(data)
19+
const token = this.createToken("math_block", "div", 0, {
20+
content: data.body,
21+
map: data.bodyMap,
22+
block: true
23+
})
24+
token.attrSet("class", "math block")
25+
if (data.options.label) {
26+
token.info = data.options.label
27+
token.meta = { label: data.options.label, numbered: true }
28+
}
29+
return [token]
30+
}
31+
}
32+
33+
export const math = {
34+
math: Math
35+
}

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import directivePlugin, { IOptions as IDirectiveOptions } from "./directives/plu
66
import { tables } from "./directives/tables"
77
import { roles } from "./roles/main"
88
import rolePlugin, { IOptions as IRoleOptions } from "./roles/plugin"
9+
import { math } from "./directives/math"
910

1011
/** Allowed options for docutils plugin */
1112
export interface IOptions extends IDirectiveOptions, IRoleOptions {
@@ -18,7 +19,7 @@ const OptionDefaults: IOptions = {
1819
replaceFences: true,
1920
rolesAfter: "inline",
2021
directivesAfter: "block",
21-
directives: { ...admonitions, ...images, ...code, ...tables },
22+
directives: { ...admonitions, ...images, ...code, ...tables, ...math },
2223
roles: { ...roles }
2324
}
2425

tests/fixtures/directives.math.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
math directive
2+
.
3+
```{math}
4+
w_{t+1} = (1 + r_{t+1}) s(w_t) + y_{t+1}
5+
```
6+
.
7+
<div class="math block">
8+
w_{t+1} = (1 + r_{t+1}) s(w_t) + y_{t+1}
9+
</div>
10+
.
11+
12+
math directive with label
13+
.
14+
```{math}
15+
:label: my_label
16+
w_{t+1} = (1 + r_{t+1}) s(w_t) + y_{t+1}
17+
```
18+
.
19+
<div class="math block">
20+
w_{t+1} = (1 + r_{t+1}) s(w_t) + y_{t+1}
21+
</div>
22+
.
23+

0 commit comments

Comments
 (0)