-
Notifications
You must be signed in to change notification settings - Fork 49
/
code.ts
191 lines (188 loc) · 5.22 KB
/
code.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import type { Caption, Container } from 'myst-spec';
import type { Code } from 'myst-spec-ext';
import yaml from 'js-yaml';
import type { DirectiveSpec, GenericNode } from 'myst-common';
import { fileError, fileWarn, normalizeLabel, ParseTypesEnum } from 'myst-common';
export const codeDirective: DirectiveSpec = {
name: 'code',
arg: {
type: ParseTypesEnum.string,
},
options: {
name: {
type: ParseTypesEnum.string,
},
class: {
type: ParseTypesEnum.string,
// class_option: list of strings?
},
// force: {
// type: ParseTypesEnum.boolean,
// doc: 'Ignore minor errors on highlighting',
// },
'number-lines': {
type: ParseTypesEnum.number,
},
},
body: {
type: ParseTypesEnum.string,
},
run(data): GenericNode[] {
const { label, identifier } = normalizeLabel(data.options?.name as string | undefined) || {};
const numberLines = data.options?.['number-lines'] as number | undefined;
const showLineNumbers = !!numberLines;
const startingLineNumber = numberLines && numberLines > 1 ? numberLines : undefined;
return [
{
type: 'code',
lang: data.arg,
identifier,
label,
class: data.options?.class,
showLineNumbers,
startingLineNumber,
value: data.body as string | undefined,
},
];
},
};
export const codeBlockDirective: DirectiveSpec = {
name: 'code-block',
arg: {
type: ParseTypesEnum.string,
},
options: {
name: {
type: ParseTypesEnum.string,
},
class: {
type: ParseTypesEnum.string,
// class_option: list of strings?
},
// force: {
// type: ParseTypesEnum.boolean,
// doc: 'Ignore minor errors on highlighting',
// },
caption: {
type: ParseTypesEnum.parsed,
},
linenos: {
type: ParseTypesEnum.boolean,
doc: 'Add line numbers',
},
'lineno-start': {
type: ParseTypesEnum.number,
doc: 'Start line numbering from a particular value',
},
// dedent: {
// type: ParseTypesEnum.number,
// doc: 'Strip indentation characters from the code block',
// },
'emphasize-lines': {
type: ParseTypesEnum.string,
doc: 'Emphasize particular lines (comma-separated numbers)',
},
},
body: {
type: ParseTypesEnum.string,
},
run(data): GenericNode[] {
const { label, identifier } = normalizeLabel(data.options?.name as string | undefined) || {};
// Validating this should probably happen first
const emphasizeLinesString = data.options?.['emphasize-lines'] as string | undefined;
const emphasizeLines = emphasizeLinesString
?.split(',')
.map((val) => Number(val.trim()))
.filter((val) => Number.isInteger(val));
if (!data.options?.caption) {
return [
{
type: 'code',
lang: data.arg,
identifier,
label,
class: data.options?.class,
showLineNumbers: data.options?.linenos,
startingLineNumber: data.options?.['lineno-start'],
emphasizeLines,
value: data.body as string,
},
];
}
const caption: Caption = {
type: 'caption',
children: [
{
type: 'paragraph',
children: data.options.caption as any[],
},
],
};
const code: Code = {
type: 'code',
lang: data.arg as string,
class: data.options?.class as string,
showLineNumbers: data.options?.linenos as boolean,
startingLineNumber: data.options?.['lineno-start'] as number,
emphasizeLines,
value: data.body as string,
};
const container: Container = {
type: 'container',
kind: 'code' as any,
label,
identifier,
children: [code as any, caption],
};
return [container];
},
};
export const codeCellDirective: DirectiveSpec = {
name: 'code-cell',
arg: {
type: ParseTypesEnum.string,
required: true,
},
options: {
tags: {
type: ParseTypesEnum.string,
},
},
body: {
type: ParseTypesEnum.string,
},
run(data, vfile): GenericNode[] {
const code: Code = {
type: 'code',
lang: data.arg as string,
executable: true,
value: data.body as string,
};
let tags: string[] | undefined;
// TODO: this validation should be done in a different place
// For example, specifying that the attribute is YAML,
// and providing a custom validation on the option.
if (typeof data.options?.tags === 'string') {
try {
tags = yaml.load(data.options.tags) as string[];
} catch (error) {
fileError(vfile, 'Could not load tags for code-cell directive', {
source: 'code-cell:tags',
});
}
} else if (data.options?.tags && Array.isArray(data.options.tags)) {
// if the options are loaded directly as yaml
tags = data.options.tags as unknown as string[];
}
if (tags && Array.isArray(tags) && tags.every((t) => typeof t === 'string')) {
if (tags && tags.length > 0) {
code.data = { tags: tags.map((t) => t.trim()) };
}
} else if (tags) {
fileWarn(vfile, 'tags in code-cell directive must be a list of strings', {
source: 'code-cell:tags',
});
}
return [code];
},
};