Gathering detailed insights and metrics for @samnoyes/codemirror-json-schema
Gathering detailed insights and metrics for @samnoyes/codemirror-json-schema
Gathering detailed insights and metrics for @samnoyes/codemirror-json-schema
Gathering detailed insights and metrics for @samnoyes/codemirror-json-schema
A JSONSchema enabled mode for codemirror 6, for json4 and json5, inspired by monaco-json
npm install @samnoyes/codemirror-json-schema
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
66 Stars
107 Commits
13 Forks
4 Watching
5 Branches
5 Contributors
Updated on 16 Nov 2024
TypeScript (96.82%)
HTML (3.12%)
Shell (0.06%)
Cumulative downloads
Total Downloads
Last day
-94.9%
2
Compared to previous day
Last week
-91.7%
23
Compared to previous week
Last month
3,675%
302
Compared to previous month
Last year
0%
413
Compared to previous year
10
5
18
5
Codemirror 6 extensions that provide full JSON Schema support for @codemirror/lang-json
& codemirror-json5
language modes
This is now a full-featured library for json schema for json
, json5
and yaml
as cm6 extensions!
StateField
and linting refreshschema.description
and custom formatHover
and formatError
configurationTo give you as much flexibility as possible, everything codemirror related is a peer or optional dependency
Based on whether you want to support json4, json5 or both, you will need to install the relevant language mode for our library to use.
formatHover
and/or formatError
configuration will be passed to markdown-it
which doesn't handle html by default.jsonSchema()
or json5Schema()
modes. See the custom usages below to learn how to use the new stateExtensions
and handleRefresh
exports.with auto-install-peers true
or similar:
npm install --save @codemirror/lang-json codemirror-json-schema
without auto-install-peers true
:
npm install --save @codemirror/lang-json codemirror-json-schema @codemirror/language @codemirror/lint @codemirror/view @codemirror/state @lezer/common
This sets up @codemirror/lang-json
and our extension for you.
If you'd like to have more control over the related configurations, see custom usage below
1import { EditorState } from "@codemirror/state"; 2import { jsonSchema } from "codemirror-json-schema"; 3 4const schema = { 5 type: "object", 6 properties: { 7 example: { 8 type: "boolean", 9 }, 10 }, 11}; 12 13const json5State = EditorState.create({ 14 doc: "{ example: true }", 15 extensions: [jsonSchema(schema)], 16});
This approach allows you to configure the json mode and parse linter, as well as our linter, hovers, etc more specifically.
1import { EditorState } from "@codemirror/state"; 2import { linter } from "@codemirror/lint"; 3import { hoverTooltip } from "@codemirror/view"; 4import { json, jsonParseLinter, jsonLanguage } from "@codemirror/lang-json"; 5 6import { 7 jsonSchemaLinter, 8 jsonSchemaHover, 9 jsonCompletion, 10 stateExtensions, 11 handleRefresh 12} from "codemirror-json-schema"; 13 14const schema = { 15 type: "object", 16 properties: { 17 example: { 18 type: "boolean", 19 }, 20 }, 21}; 22 23const state = EditorState.create({ 24 doc: `{ "example": true }`, 25 extensions: [ 26 json(), 27 linter(jsonParseLinter(), { 28 // default is 750ms 29 delay: 300 30 }), 31 linter(jsonSchemaLinter(), { 32 needsRefresh: handleRefresh, 33 }), 34 jsonLanguage.data.of({ 35 autocomplete: jsonCompletion(), 36 }), 37 hoverTooltip(jsonSchemaHover()), 38 stateExtensions(schema) 39 ]; 40})
with auto-install-peers true
or similar:
npm install --save codemirror-json5 codemirror-json-schema
without auto-install-peers true
:
npm install --save codemirror-json5 codemirror-json-schema @codemirror/language @codemirror/lint @codemirror/view @codemirror/state @lezer/common
This sets up codemirror-json5
mode for you.
If you'd like to have more control over the related configurations, see custom usage below
1import { EditorState } from "@codemirror/state"; 2import { json5Schema } from "codemirror-json-schema/json5"; 3 4const schema = { 5 type: "object", 6 properties: { 7 example: { 8 type: "boolean", 9 }, 10 }, 11}; 12 13const json5State = EditorState.create({ 14 doc: `{ 15 example: true, 16 // json5 is awesome! 17 }`, 18 extensions: [json5Schema(schema)], 19});
This approach allows you to configure the json5 mode and parse linter, as well as our linter, hovers, etc more specifically.
1import { EditorState } from "@codemirror/state"; 2import { linter } from "@codemirror/lint"; 3import { json5, json5ParseLinter, json5Language } from "codemirror-json5"; 4import { 5 json5SchemaLinter, 6 json5SchemaHover, 7 json5Completion, 8} from "codemirror-json-schema/json5"; 9import { stateExtensions, handleRefresh } from "codemirror-json-schema"; 10 11const schema = { 12 type: "object", 13 properties: { 14 example: { 15 type: "boolean", 16 }, 17 }, 18}; 19 20const json5State = EditorState.create({ 21 doc: `{ 22 example: true, 23 // json5 is awesome! 24 }`, 25 extensions: [ 26 json5(), 27 linter(json5ParseLinter(), { 28 // the default linting delay is 750ms 29 delay: 300, 30 }), 31 linter( 32 json5SchemaLinter({ 33 needsRefresh: handleRefresh, 34 }) 35 ), 36 hoverTooltip(json5SchemaHover()), 37 json5Language.data.of({ 38 autocomplete: json5Completion(), 39 }), 40 stateExtensions(schema), 41 ], 42});
If you want to, you can provide schema dynamically, in several ways.
This works the same for either json or json5, using the underlying codemirror 6 StateFields, via the updateSchema
method export.
In this example
jsonSchema()
and json5Schema()
modes1import { EditorState } from "@codemirror/state"; 2import { EditorView } from "@codemirror/view"; 3 4import { json5Schema } from "codemirror-json-schema/json5"; 5 6import { updateSchema } from "codemirror-json-schema"; 7 8const json5State = EditorState.create({ 9 doc: `{ 10 example: true, 11 // json5 is awesome! 12 }`, 13 // note: you can still provide initial 14 // schema when creating state 15 extensions: [json5Schema()], 16}); 17 18const editor = new EditorView({ state: json5State }); 19 20const schemaSelect = document.getElementById("schema-selection"); 21 22schemaSelect!.onchange = async (e) => { 23 const val = e.target!.value!; 24 if (!val) { 25 return; 26 } 27 // parse the remote schema spec to json 28 const data = await ( 29 await fetch(`https://json.schemastore.org/${val}`) 30 ).json(); 31 // this will update the schema state field, in an editor specific way 32 updateSchema(editor, data); 33};
if you are using the "custom path" with this approach, you will need to configure linting refresh as well:
1import { linter } from "@codemirror/lint"; 2import { json5SchemaLinter } from "codemirror-json-schema/json5"; 3import { handleRefresh } from "codemirror-json-schema"; 4 5const state = EditorState.create({ 6 // ... 7 extensions: [ 8 linter(json5SchemaLinter(), { 9 needsRefresh: handleRefresh, 10 }) 11 ]; 12}
monaco-json
and monaco-yaml
both provide json schema features for json, cson and yaml, and we want the nascent codemirror 6 to have them as well!
Also, json5 is slowly growing in usage, and it needs full language support for the browser!
No vulnerabilities found.
No security vulnerabilities found.