Skip to content

Commit c694451

Browse files
authored
Added YAML support (#85)
Closes #84 - Added YAML support - Also updated the DOM rendering bits to use markdown instead of setting the HTML directly, which is a safer approach - Also slightly improved the demo UI (it didn't look good with black text on black background) - Also added a bunch of tests To support YAML (and potentially other formats), I updated the logic to "resolve" tokens before using them. This is important since the [YAML grammar/tokens](https://github.com/lezer-parser/yaml/blob/main/src/yaml.grammar) is different from the [JSON grammar/tokens](https://github.com/lezer-parser/json/blob/main/src/json.grammar) (which is very similar to the [JSON5 grammar](https://github.com/dimfeld/lezer-json5/blob/master/src/json5.grammar)). So we map the tokens from the different grammars to the equivalent token in the "base" grammar (currently this is the JSON grammar tokens but I think we can/should map to some other arbitrary token set which will force us to properly implement the mappings, otherwise additional changes made may work in JSON mode but without proper testing, may not work in YAML mode, if the mappings were not added).
1 parent b3a0aa7 commit c694451

39 files changed

+2164
-728
lines changed

.changeset/silly-oranges-worry.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"codemirror-json-schema": minor
3+
---
4+
5+
Added YAML support, switched back to markdown for messages, provide markdown rendering, and fix some autocompletion issues

dev/index.html

+10
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
<style>
3030
:root {
3131
--bg-color: #282c34;
32+
--fbc-light-gray: #f0f0f4;
3233
}
3334
h1,
3435
h2,
@@ -66,10 +67,15 @@
6667
}
6768
.cm6-json-schema-hover,
6869
.cm-editor .cm-diagnostic-error {
70+
font-size: 12px;
6971
padding: 0.5rem;
7072
}
7173
.cm6-json-schema-hover--description {
7274
margin-bottom: 0.5rem;
75+
max-width: 600px;
76+
}
77+
.cm6-json-schema-hover--code-wrapper {
78+
border-top: 1px solid #888;
7379
}
7480
.grid-row {
7581
display: grid;
@@ -110,6 +116,10 @@ <h2><code>package.json</code> demo</h2>
110116
<h2><code>package.json5</code> demo</h2>
111117
<div id="editor-json5"></div>
112118
</div>
119+
<div>
120+
<h2><code>package.yaml</code> demo</h2>
121+
<div id="editor-yaml"></div>
122+
</div>
113123
</div>
114124
<script type="module" src="./index.ts"></script>
115125
</body>

dev/index.ts

+17-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import {
2929
import { JSONSchema7 } from "json-schema";
3030

3131
// sample data
32-
import { jsonText, json5Text } from "./sample-text";
32+
import { jsonText, json5Text, yamlText } from "./sample-text";
3333
import packageJsonSchema from "./package.schema.json";
3434

3535
// json4
@@ -38,6 +38,8 @@ import { jsonSchema, updateSchema } from "../src/index";
3838
// json5
3939
import { json5Schema } from "../src/json5";
4040

41+
import { yamlSchema } from "../src/yaml";
42+
4143
const schema = packageJsonSchema as JSONSchema7;
4244

4345
/**
@@ -99,9 +101,23 @@ const editor2 = new EditorView({
99101
parent: document.querySelector("#editor-json5")!,
100102
});
101103

104+
/**
105+
* yaml!
106+
*/
107+
const yamlState = EditorState.create({
108+
doc: yamlText,
109+
extensions: [commonExtensions, yamlSchema(schema)],
110+
});
111+
112+
const editor3 = new EditorView({
113+
state: yamlState,
114+
parent: document.querySelector("#editor-yaml")!,
115+
});
116+
102117
const handleSchemaChange = (newSchema: JSONSchema7) => {
103118
updateSchema(editor1, newSchema);
104119
updateSchema(editor2, newSchema);
120+
updateSchema(editor3, newSchema);
105121
};
106122

107123
// new EditorState.fi(editor1, editor2);

dev/sample-text.ts

+9
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,12 @@ export const json5Text = `{
1919
}],
2020
'type': '',
2121
}`;
22+
23+
export const yamlText = `name: lexunicon
24+
version: 0.0.0
25+
description: A lexicon for the unicon programming language
26+
contributors:
27+
- email: email
28+
url: h
29+
type: ''
30+
`;

package.json

+15-5
Original file line numberDiff line numberDiff line change
@@ -42,21 +42,30 @@
4242
"default": "./cjs/index.js"
4343
},
4444
"./json5": {
45-
"import": "./dist/json5.js",
46-
"types": "./dist/json5.d.ts",
47-
"require": "./cjs/json5.js",
48-
"default": "./cjs/json5.js"
45+
"import": "./dist/json5/index.js",
46+
"types": "./dist/json5/index.d.ts",
47+
"require": "./cjs/json5/index.js",
48+
"default": "./cjs/json5/index.js"
49+
},
50+
"./yaml": {
51+
"import": "./dist/yaml/index.js",
52+
"types": "./dist/yaml/index.d.ts",
53+
"require": "./cjs/yaml/index.js",
54+
"default": "./cjs/yaml/index.js"
4955
}
5056
},
5157
"repository": "github:acao/codemirror-json-schema",
5258
"homepage": "https://codemirror-json-schema.netlify.app/",
5359
"dependencies": {
5460
"@changesets/changelog-github": "^0.4.8",
61+
"@codemirror/lang-yaml": "^6.0.0",
5562
"@sagold/json-pointer": "^5.1.1",
5663
"@types/json-schema": "^7.0.12",
5764
"@types/node": "^20.4.2",
5865
"json-schema": "^0.4.0",
59-
"json-schema-library": "^9.1.2"
66+
"json-schema-library": "^9.1.2",
67+
"markdown-it": "^14.0.0",
68+
"yaml": "^2.3.4"
6069
},
6170
"optionalDependencies": {
6271
"@codemirror/lang-json": "^6.0.1",
@@ -82,6 +91,7 @@
8291
"@codemirror/view": "^6.14.1",
8392
"@evilmartians/lefthook": "^1.4.6",
8493
"@lezer/common": "^1.0.3",
94+
"@types/markdown-it": "^13.0.7",
8595
"@vitest/coverage-v8": "^0.34.6",
8696
"codemirror": "^6.0.1",
8797
"codemirror-json5": "^1.0.3",

pnpm-lock.yaml

+111-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)