Skip to content
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

✨ NEW: Add math directive #13

Merged
merged 4 commits into from
Nov 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,11 @@ All directives have a fallback renderer, but the the following are specifically
- Tables:
- `list-table`
- HTML:
- `sub`: Subscript
- `sup`: Superscript
- `abbr`: Abbreviation
- `sub`: Subscript
- `sup`: Superscript
- `abbr`: Abbreviation
- Other:
- `math`

## CSS Styling

Expand Down
7 changes: 7 additions & 0 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,13 @@ <h1>markdown-it-docutils</h1>
* H{sub}`2`O
* 4{sup}`th` of July
* {abbr}`CSS (Cascading Style Sheets)`

Math:

```{math}
:label: math_label
w_{t+1} = (1 + r_{t+1}) s(w_t) + y_{t+1}
```
</textarea
>
<div id="renderer" class="rendered"></div>
Expand Down
2 changes: 1 addition & 1 deletion src/directives/admonitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class BaseAdmonition extends Directive {
name: unchanged
}
public title = ""
run(data: IDirectiveData): Token[] {
run(data: IDirectiveData<keyof BaseAdmonition["option_spec"]>): Token[] {
const newTokens: Token[] = []

// we create an overall container, then individual containers for the title and body
Expand Down
6 changes: 3 additions & 3 deletions src/directives/code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class Code extends Directive {
name: unchanged,
class: class_option
}
run(data: IDirectiveData): Token[] {
run(data: IDirectiveData<keyof Code["option_spec"]>): Token[] {
// TODO handle options
this.assert_has_content(data)
const token = this.createToken("fence", "code", 0, {
Expand Down Expand Up @@ -68,7 +68,7 @@ export class CodeBlock extends Directive {
name: unchanged,
class: class_option
}
run(data: IDirectiveData): Token[] {
run(data: IDirectiveData<keyof CodeBlock["option_spec"]>): Token[] {
// TODO handle options
this.assert_has_content(data)
const token = this.createToken("fence", "code", 0, {
Expand All @@ -89,7 +89,7 @@ export class CodeCell extends Directive {
public has_content = true
public rawOptions = true

run(data: IDirectiveData): Token[] {
run(data: IDirectiveData<keyof CodeCell["option_spec"]>): Token[] {
// TODO store options and the fact that this is a code cell rather than a fence?
const token = this.createToken("fence", "code", 0, {
info: data.args ? data.args[0] : "",
Expand Down
4 changes: 2 additions & 2 deletions src/directives/images.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export class Image extends Directive {
...shared_option_spec,
align: create_choice(["left", "center", "right", "top", "middle", "bottom"])
}
create_image(data: IDirectiveData): Token {
create_image(data: IDirectiveData<keyof Image["option_spec"]>): Token {
// get URI
const src = uri(data.args[0] || "")

Expand Down Expand Up @@ -89,7 +89,7 @@ export class Figure extends Image {
figclass: class_option
}
public has_content = true
run(data: IDirectiveData): Token[] {
run(data: IDirectiveData<keyof Figure["option_spec"]>): Token[] {
const openToken = this.createToken("figure_open", "figure", 1, { map: data.map })
if (data.options.figclass) {
openToken.attrJoin("class", data.options.figclass.join(" "))
Expand Down
8 changes: 3 additions & 5 deletions src/directives/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,7 @@ export interface IDirectiveSpec {
/** if body content is allowed */
has_content?: boolean
/** mapping known option names to conversion functions */
option_spec?: {
[key: string]: OptionSpecConverter
}
option_spec?: Record<string, OptionSpecConverter>
/** If true, do not attempt to validate/convert options. */
rawOptions?: boolean
}
Expand Down Expand Up @@ -153,10 +151,10 @@ export class Directive implements IDirectiveSpec {
}

/** Data structure of a directive */
export interface IDirectiveData {
export interface IDirectiveData<T extends string = string> {
map: [number, number]
args: string[]
options: { [key: string]: any }
options: Record<T, any>
body: string
bodyMap: [number, number]
}
Expand Down
35 changes: 35 additions & 0 deletions src/directives/math.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/** Admonitions to visualise programming codes */
import type Token from "markdown-it/lib/token"
import { Directive, IDirectiveData } from "./main"
import { unchanged } from "./options"

/** Math directive with a label
*/
export class Math extends Directive {
public required_arguments = 0
public optional_arguments = 0
public final_argument_whitespace = false
public has_content = true
public option_spec = {
label: unchanged
}
run(data: IDirectiveData<keyof Math["option_spec"]>): Token[] {
// TODO handle options
this.assert_has_content(data)
const token = this.createToken("math_block", "div", 0, {
content: data.body,
map: data.bodyMap,
block: true
})
token.attrSet("class", "math block")
if (data.options.label) {
token.info = data.options.label
token.meta = { label: data.options.label, numbered: true }
}
return [token]
}
}

export const math = {
math: Math
}
7 changes: 4 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import { roles } from "./roles/main"
import { html } from "./roles/html"
import rolePlugin from "./roles/plugin"
import type { IOptions as IRoleOptions } from "./roles/types"
import { math } from "./roles/math"
import { math as mathRole } from "./roles/math"
import { math as mathDirective } from "./directives/math"

/** Allowed options for docutils plugin */
export interface IOptions extends IDirectiveOptions, IRoleOptions {
Expand All @@ -22,8 +23,8 @@ const OptionDefaults: IOptions = {
replaceFences: true,
rolesAfter: "inline",
directivesAfter: "block",
directives: { ...admonitions, ...images, ...code, ...tables },
roles: { ...roles, ...html, ...math }
directives: { ...admonitions, ...images, ...code, ...tables, ...mathDirective },
roles: { ...roles, ...html, ...mathRole }
}

/**
Expand Down
23 changes: 23 additions & 0 deletions tests/fixtures/directives.math.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
math directive
.
```{math}
w_{t+1} = (1 + r_{t+1}) s(w_t) + y_{t+1}
```
.
<div class="math block">
w_{t+1} = (1 + r_{t+1}) s(w_t) + y_{t+1}
</div>
.

math directive with label
.
```{math}
:label: my_label
w_{t+1} = (1 + r_{t+1}) s(w_t) + y_{t+1}
```
.
<div class="math block">
w_{t+1} = (1 + r_{t+1}) s(w_t) + y_{t+1}
</div>
.