https://github.com/dimfeld/codemirror-json5
Codemirror 6 support for JSON5
https://github.com/dimfeld/codemirror-json5
Last synced: over 1 year ago
JSON representation
Codemirror 6 support for JSON5
- Host: GitHub
- URL: https://github.com/dimfeld/codemirror-json5
- Owner: dimfeld
- License: apache-2.0
- Created: 2022-12-10T20:15:02.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2023-09-21T21:21:25.000Z (almost 3 years ago)
- Last Synced: 2024-10-07T07:06:39.998Z (almost 2 years ago)
- Language: TypeScript
- Size: 19.5 KB
- Stars: 4
- Watchers: 2
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE-APACHE
Awesome Lists containing this project
README
# codemirror-json5
This package implements JSON5 support for Codemirror 6.
Beyond the basic support, it provides a linter and state fields that expose:
- The parsed object produced by the JSON5 parser, so that your surrounding code can use that result instead of parsing it twice.
- The path in the object that the cursor is on.
## Usage
```javascript
import { json5, json5ParseLinter } from 'codemirror-json5';
import { linter } from '@codemirror/lint';
import { EditorState } from '@codemirror/state';
import { EditorView } from '@codemirror/view';
const view = new EditorView({
state: EditorState.create({
doc: `{ a_doc: 'goes here' }`,
extensions: [
json5(),
linter(json5ParseLinter()),
]
})
});
```
## Retrieving Editor State
```typescript
import { EditorState } from '@codemirror/state';
import { jsonCursorPath, json5ParseCache } from 'codemirror-json5';
import get from 'just-safe-get';
function getJson5Info(state: EditorState) {
const object = state.field(json5ParseCache);
const { path } = state.field(jsonCursorPath);
return {
parsed: object.obj,
parseError: object.err,
cursorPath: path,
cursorValue: get(object.obj ?? {}, path),
};
}
```