Skip to content

Commit 8c02c04

Browse files
committed
Add code-titles plugin
1 parent 63c32f4 commit 8c02c04

File tree

5 files changed

+143
-44
lines changed

5 files changed

+143
-44
lines changed

README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,23 @@ features they make available to you.
6969
- [remark-table-of-contents](/json-schema-org/json-schema-spec/blob/main/remark-table-of-contents.js)
7070
-- Adds a table of contents in a section with a header called "Table of
7171
Contents".
72+
- [remark-code-titles](/json-schema-org/json-schema-spec/blob/main/remark-code-titles.js)
73+
-- Add titles to code blocks
74+
- Example:
75+
```markdown
76+
\`\`\`jsonschema "My Fun Title"
77+
{ "type": "string" }
78+
\`\`\`
79+
```
80+
- The languages `jsonschema` and `json` have special styling
81+
- The title will be parsed as a JSON string, but you have to double escape
82+
escaped characters. So, to get `My "quoted" title`, you would need to be
83+
`"My \\\\"quoted\\\\" title"`.
7284
- [remark-torchlight](https://github.com/torchlight-api/remark-torchlight) --
7385
Syntax highlighting and more using https://torchlight.dev. Features include
7486
line numbers and line highlighting.
7587
- [remark-flexible-containers](https://github.com/ipikuka/remark-flexible-containers)
76-
- Add a callout box using the following syntax. Supported container types are
88+
-- Add a callout box using the following syntax. Supported container types are
7789
`warning`, `note`, and `experimental`.
7890

7991
```

build/build.js

Lines changed: 37 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build/remark-code-titles.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import { visit } from "unist-util-visit";
2+
import { text } from "mdast-builder";
3+
4+
5+
const hexdig = `[0-9a-fA-F]`;
6+
const char = `(?:\\\\["\\/\\\\brfnt]|\\\\u${hexdig}{4}|[^"\\\\])`;
7+
const jsonStringPattern = new RegExp(`^"${char}*"`);
8+
9+
const remarkNumberHeadings = () => (tree) => {
10+
visit(tree, "code", (codeNode, index, parent) => {
11+
// Support title without a language
12+
if (codeNode.lang && codeNode.lang[0] === "\"") {
13+
codeNode.meta = `${codeNode.lang} ${codeNode.meta}`;
14+
codeNode.lang = null;
15+
}
16+
17+
let title = "";
18+
const titleClasses = ["remark-code-title"];
19+
20+
const language = codeNode.lang ?? "";
21+
if (language.toLowerCase() === "jsonschema") {
22+
codeNode.lang = "json";
23+
title = "JSON Schema";
24+
titleClasses.push("code-title-jsonschema");
25+
} else if (language.toLowerCase() === "json") {
26+
title = "JSON";
27+
titleClasses.push("code-title-json");
28+
} else {
29+
titleClasses.push("code-title-unknown");
30+
}
31+
32+
if ("meta" in codeNode) {
33+
const match = jsonStringPattern.exec(codeNode.meta);
34+
if (match) {
35+
const customTitle = JSON.parse(match[0]);
36+
title = title ? `${title} - ${customTitle}` : customTitle;
37+
codeNode.meta = codeNode.meta.slice(match[0].length).trim();
38+
}
39+
}
40+
41+
const containerChildren = [];
42+
if (title) {
43+
const titleNode = div([text(title)], { className: titleClasses });
44+
containerChildren.push(titleNode);
45+
}
46+
containerChildren.push(codeNode);
47+
48+
const wrappedCodeNode = div(containerChildren, { className: ["remark-code-container"] });
49+
50+
parent.children.splice(index, 1, wrappedCodeNode);
51+
});
52+
};
53+
54+
const div = (children, properties) => {
55+
return {
56+
type: "container",
57+
children,
58+
data: {
59+
hName: "div",
60+
hProperties: properties
61+
}
62+
};
63+
};
64+
65+
export default remarkNumberHeadings;

jsonschema-core.md

Lines changed: 25 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ Schema document and resource concepts will be clarified in a future draft.
290290

291291
Some keywords take schemas themselves, allowing JSON Schemas to be nested:
292292

293-
```json
293+
```jsonschema
294294
{
295295
"title": "root",
296296
"items": {
@@ -628,7 +628,7 @@ of multiple primitive types. The companion validation vocabulary also includes a
628628
primitive types. This allows for a concise expression of use cases such as a
629629
function that might return either a string of a certain length or a null value:
630630

631-
```json
631+
```jsonschema
632632
{
633633
"type": ["string", "null"],
634634
"maxLength": 255
@@ -696,7 +696,7 @@ the [Validation specification](#json-schema-validation):
696696

697697
Note that some lines are wrapped for clarity.
698698

699-
```json
699+
```jsonschema
700700
{
701701
"title": "Feature list",
702702
"type": "array",
@@ -775,7 +775,7 @@ subschemas.
775775
Note that the overall schema results may still include annotations collected
776776
from other schema locations. Given this schema:
777777

778-
```json
778+
```jsonschema
779779
{
780780
"oneOf": [
781781
{
@@ -1185,7 +1185,7 @@ a valid JSON Schema.
11851185
As an example, here is a schema describing an array of positive integers, where
11861186
the positive integer constraint is a subschema in `$defs`:
11871187

1188-
```json
1188+
```jsonschema
11891189
{
11901190
"type": "array",
11911191
"items": { "$ref": "#/$defs/positiveInteger" },
@@ -1305,7 +1305,7 @@ that schema SHOULD be used automatically.
13051305

13061306
For example, consider this schema:
13071307

1308-
```json
1308+
```jsonschema
13091309
{
13101310
"$id": "https://example.net/root.json",
13111311
"type": "array",
@@ -1361,7 +1361,7 @@ identify embedded schema resources or locations within them.
13611361
Consider the following schema document that contains another schema resource
13621362
embedded within it:
13631363

1364-
```json
1364+
```jsonschema
13651365
{
13661366
"$id": "https://example.com/foo",
13671367
"items": {
@@ -1383,7 +1383,7 @@ object, but that object's IRI relative to its resource's canonical IRI is
13831383
Now consider the following two schema resources linked by reference using a IRI
13841384
value for `$ref`:
13851385

1386-
```json
1386+
```jsonschema
13871387
{
13881388
"$id": "https://example.com/foo",
13891389
"items": {
@@ -1392,10 +1392,10 @@ value for `$ref`:
13921392
}
13931393
```
13941394

1395-
```json
1395+
```jsonschema
13961396
{
13971397
"$id": "https://example.com/bar",
1398-
"additionalProperties": { }
1398+
"additionalProperties": {}
13991399
}
14001400
```
14011401

@@ -2240,8 +2240,7 @@ details: the collection of results produced by subschemas
22402240

22412241
For these examples, the following schema and instances will be used.
22422242

2243-
Schema
2244-
```json
2243+
```jsonschema
22452244
{
22462245
"$schema": "https://json-schema.org/draft/next/schema",
22472246
"$id": "https://json-schema.org/schemas/example",
@@ -2284,16 +2283,15 @@ Schema
22842283
}
22852284
```
22862285

2287-
Failing instance
2288-
```json
2286+
2287+
```json "Failing instance"
22892288
{
22902289
"foo": {"foo-prop": "not 1", "other-prop": false},
22912290
"bar": {"bar-prop": 2}
22922291
}
22932292
```
22942293

2295-
Passing instance
2296-
```json
2294+
```json "Passing instance"
22972295
{
22982296
"foo": {
22992297
"foo-prop": 1,
@@ -2381,8 +2379,7 @@ Output units which do not contain errors or annotations SHOULD be excluded from
23812379
this format, however implementations MAY choose to include them for
23822380
completeness.
23832381

2384-
Failing results
2385-
```json
2382+
```json "Failing results"
23862383
{
23872384
"valid": false,
23882385
"details": [
@@ -2417,8 +2414,7 @@ Failing results
24172414
}
24182415
```
24192416

2420-
Passing results
2421-
```json
2417+
```json "Passing results"
24222418
{
24232419
"valid": true,
24242420
"details": [
@@ -2495,8 +2491,7 @@ All output units are included in this format.
24952491

24962492
The location properties of the root output unit MAY be omitted.
24972493

2498-
Failing results (errors)
2499-
```json
2494+
```json "Failing results (errors)"
25002495
{
25012496
"valid": false,
25022497
"evaluationPath": "",
@@ -2586,8 +2581,7 @@ Failing results (errors)
25862581
}
25872582
```
25882583

2589-
Passing results (annotations)
2590-
```json
2584+
```json "Passing results (annotations)"
25912585
{
25922586
"valid": true,
25932587
"evaluationPath": "",
@@ -2875,7 +2869,7 @@ Consider the following schema, which shows `$id` being used to identify both the
28752869
root schema and various subschemas, and `$anchor` being used to define plain
28762870
name fragment identifiers.
28772871

2878-
```json
2872+
```jsonschema
28792873
{
28802874
"$id": "https://example.com/root.json",
28812875
"$defs": {
@@ -2998,8 +2992,7 @@ schema allows and ignores other instance properties. The second is more strict
29982992
and only allows the "data" and "children" properties. An example instance with
29992993
"data" misspelled as "daat" is also shown.
30002994

3001-
Tree schema, extensible
3002-
```json
2995+
```jsonschema "Tree schema, extensible"
30032996
{
30042997
"$schema": "https://json-schema.org/draft/next/schema",
30052998
"$id": "https://example.com/tree",
@@ -3018,8 +3011,7 @@ Tree schema, extensible
30183011
}
30193012
```
30203013

3021-
Strict-tree schema, guards against misspelled properties
3022-
```json
3014+
```jsonschema "Strict-tree schema, guards against misspelled properties"
30233015
{
30243016
"$schema": "https://json-schema.org/draft/next/schema",
30253017
"$id": "https://example.com/strict-tree",
@@ -3030,8 +3022,7 @@ Strict-tree schema, guards against misspelled properties
30303022
}
30313023
```
30323024

3033-
Instance with misspelled field
3034-
```json
3025+
```jsonschema "Instance with misspelled field"
30353026
{
30363027
"children": [ { "daat": 1 } ]
30373028
}
@@ -3161,7 +3152,7 @@ which they are understood.
31613152

31623153
This meta-schema combines several vocabularies for general use.
31633154

3164-
```json
3155+
```jsonschema
31653156
{
31663157
"$schema": "https://json-schema.org/draft/next/schema",
31673158
"$id": "https://example.com/meta/general-use-example",
@@ -3192,7 +3183,7 @@ This meta-schema combines several vocabularies for general use.
31923183

31933184
This meta-schema describes only a single extension vocabulary.
31943185

3195-
```json
3186+
```jsonschema
31963187
{
31973188
"$schema": "https://json-schema.org/draft/next/schema",
31983189
"$id": "https://example.com/meta/example-vocab",
@@ -3246,7 +3237,7 @@ generator should consider the reference target to be a distinct class, and how
32463237
those classes are related. Note that this example is solely for illustrative
32473238
purposes, and is not intended to propose a functional code generation keyword.
32483239

3249-
```json
3240+
```jsonschema
32503241
{
32513242
"allOf": [
32523243
{

0 commit comments

Comments
 (0)