Gathering detailed insights and metrics for monaco-yaml
Gathering detailed insights and metrics for monaco-yaml
Gathering detailed insights and metrics for monaco-yaml
Gathering detailed insights and metrics for monaco-yaml
npm install monaco-yaml
61.1
Supply Chain
93.7
Quality
89
Maintenance
100
Vulnerability
98.2
License
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
270 Stars
439 Commits
73 Forks
7 Watching
2 Branches
26 Contributors
Updated on 11 Nov 2024
Minified
Minified + Gzipped
TypeScript (86.09%)
JavaScript (13.91%)
Cumulative downloads
Total Downloads
Last day
-24%
39,882
Compared to previous day
Last week
-11.3%
171,187
Compared to previous week
Last month
35.6%
759,091
Compared to previous month
Last year
81.7%
6,910,798
Compared to previous year
11
1
YAML language plugin for the Monaco Editor. It provides the following features when editing YAML files:
Schemas can also be provided by configuration. See here for the API that the plugin offers to configure the YAML language support.
@monaco-editor/loader
or @monaco-editor/react
?monaco-yaml
with a framework? (Angular, React, Vue, etc.)monaco-yaml
work with create-react-app
?monaco-yaml
working? Official Monaco language extensions do work.1npm install monaco-yaml
Before implementing monaco-yaml
, or even Monaco editor, it’s recommended to learn the
basic concepts of Monaco editor.
To configure monaco-yaml
, call configureMonacoYaml
.
1import * as monaco from 'monaco-editor' 2import { configureMonacoYaml } from 'monaco-yaml' 3 4configureMonacoYaml(monaco, { 5 enableSchemaRequest: true, 6 schemas: [ 7 { 8 // If YAML file is opened matching this glob 9 fileMatch: ['**/.prettierrc.*'], 10 // Then this schema will be downloaded from the internet and used. 11 uri: 'https://json.schemastore.org/prettierrc.json' 12 }, 13 { 14 // If YAML file is opened matching this glob 15 fileMatch: ['**/person.yaml'], 16 // The following schema will be applied 17 schema: { 18 type: 'object', 19 properties: { 20 name: { 21 type: 'string', 22 description: 'The person’s display name' 23 }, 24 age: { 25 type: 'integer', 26 description: 'How old is the person in years?' 27 }, 28 occupation: { 29 enum: ['Delivery person', 'Software engineer', 'Astronaut'] 30 } 31 } 32 }, 33 // And the URI will be linked to as the source. 34 uri: 'https://github.com/remcohaszing/monaco-yaml#usage' 35 } 36 ] 37}) 38 39const prettierc = monaco.editor.createModel( 40 'singleQuote: true\nproseWrap: always\nsemi: yes\n', 41 undefined, 42 monaco.Uri.parse('file:///.prettierrc.yaml') 43) 44 45monaco.editor.createModel( 46 'name: John Doe\nage: 42\noccupation: Pirate\n', 47 undefined, 48 monaco.Uri.parse('file:///person.yaml') 49) 50 51monaco.editor.create(document.getElementById('editor'), { 52 automaticLayout: true, 53 model: prettierc 54})
Also make sure to register the web worker. When using Webpack 5, this looks like the code below. Other bundlers may use a different syntax, but the idea is the same. Languages you don’t used can be omitted.
1window.MonacoEnvironment = {
2 getWorker(moduleId, label) {
3 switch (label) {
4 case 'editorWorkerService':
5 return new Worker(new URL('monaco-editor/esm/vs/editor/editor.worker', import.meta.url))
6 case 'css':
7 case 'less':
8 case 'scss':
9 return new Worker(new URL('monaco-editor/esm/vs/language/css/css.worker', import.meta.url))
10 case 'handlebars':
11 case 'html':
12 case 'razor':
13 return new Worker(
14 new URL('monaco-editor/esm/vs/language/html/html.worker', import.meta.url)
15 )
16 case 'json':
17 return new Worker(
18 new URL('monaco-editor/esm/vs/language/json/json.worker', import.meta.url)
19 )
20 case 'javascript':
21 case 'typescript':
22 return new Worker(
23 new URL('monaco-editor/esm/vs/language/typescript/ts.worker', import.meta.url)
24 )
25 case 'yaml':
26 return new Worker(new URL('monaco-yaml/yaml.worker', import.meta.url))
27 default:
28 throw new Error(`Unknown label ${label}`)
29 }
30 }
31}
A demo is available on monaco-yaml.js.org.
Some usage examples can be found in the examples directory.
monaco-yaml
has the following exports:
configureMonacoYaml(monaco, options?)
Configure monaco-yaml
.
Note: There may only be one configured instance of
monaco-yaml
at a time.
monaco
(object
): The Monaco editor module. Typically you get this by importing
monaco-editor
. Third party integrations often expose it as the global monaco
variable instead.options
(object
): Options to configure monaco-yaml
.completion
(boolean
): If set, enable schema based autocompletion. (Default: true
)customTags
(string[]
): A list of custom tags. (Default: []
)enableSchemaRequest
(boolean
): If set, the schema service will load schema content on-demand.
(Default: false
)format
(boolean
): Prettier from the bundle. (Default: true
)hover
(boolean
): If set, enable hover typs based the JSON schema. (Default: true
)isKubernetes
(boolean
): If true, a different diffing algorithm is used to generate error
messages. (Default: false
)schemas
(object[]
): A list of known schemas and/or associations of schemas to file names.
(Default: []
)validate
(boolean
): based validation. (Default: true
)yamlVersion
('1.1' | '1.2'
): The YAML version to use for parsing. (Default: 1,2
)An object that can be used to dispose or update monaco-yaml
.
Yes, starting from version 5.0.0
Yes, starting from version 5.0.0
@monaco-editor/loader
or @monaco-editor/react
?Yes, starting from version 5.0.0
Yes. The web worker provides the core functionality of monaco-yaml
.
No. monaco-yaml
uses dependencies from node_modules
, so they can be deduped and your bundle size
is decreased. This comes at the cost of not being able to use it without a bundler.
monaco-yaml
with a framework? (Angular, React, Vue, etc.)monaco-yaml
only uses the Monaco Editor. It’s not tied to a framework, all that’s needed is a DOM
node to attach the Monaco Editor to. See the
Monaco Editor examples for examples
on how to integrate Monaco Editor in your project, then configure monaco-yaml
as described above.
monaco-yaml
work with create-react-app
?Yes, but you’ll have to eject. See #92 (comment) for details.
Some users have experienced the following error when using Vite:
Uncaught (in promise) Error: Unexpected usage
at EditorSimpleWorker.loadForeignModule (editorSimpleWorker.js)
at webWorker.js
As a workaround, create a file named yaml.worker.js
in your own project with the following
contents:
1import 'monaco-yaml/yaml.worker.js'
Then in your Monaco environment getWorker
function, reference this file instead of referencing
monaco-yaml/yaml.worker.js
directly:
1import YamlWorker from './yaml.worker.js?worker' 2 3window.MonacoEnvironment = { 4 getWorker(moduleId, label) { 5 switch (label) { 6 // Handle other cases 7 case 'yaml': 8 return new YamlWorker() 9 default: 10 throw new Error(`Unknown label ${label}`) 11 } 12 } 13}
monaco-yaml
working? Official Monaco language extensions do work.This is most likely due to the fact that monaco-yaml
is using a different instance of the
monaco-editor
package than you are. This is something you’ll want to avoid regardless of
monaco-editor
, because it means your bundle is significantly larger than it needs to be. This is
likely caused by one of the following issues:
A code splitting misconfiguration
To solve this, try inspecting your bundle using for example
webpack-bundle-analyzer. If
monaco-editor
is in there twice, this is the issue. It’s up to you to solve this, as it’s
project-specific.
You’re using a package which imports monaco-editor
for you, but it’s using a different version.
You can find out why the monaco-editor
is installed using npm ls monaco-editor
or
yarn why monaco-editor
. It should exist only once, but it’s ok if it’s deduped.
You may be able to solve this by deleting your node_modules
folder and package-lock.json
or
yarn.lock
, then running npm install
or yarn install
respectively.
If you’re using
monaco webpack plugin, then
instead of the above code, you can extend the plugin’s configuration. Extend your
webpack.config.js
file with the following:
1import { MonacoWebpackPlugin } from 'monaco-editor-webpack-plugin' 2 3export default { 4 // ...the rest of your webpack configuration... 5 plugins: [ 6 new MonacoWebpackPlugin({ 7 languages: ['yaml'], 8 customLanguages: [ 9 { 10 label: 'yaml', 11 entry: 'monaco-yaml', 12 worker: { 13 id: 'monaco-yaml/yamlWorker', 14 entry: 'monaco-yaml/yaml.worker' 15 } 16 } 17 ] 18 }) 19 ] 20}
You can also refer to the example of a complete project.
Syntax highlighting is provided by Monaco editor itself, not by monaco-yaml
. If you use a build
tool to filter languages or features, make sure to include the yaml
language. If you import Monaco
editor from monaco-editor/esm/vs/editor/editor.api.js
and cherry-pick features manually, make sure
to also import monaco-editor/esm/vs/basic-languages/yaml/yaml.contribution.js
. If you use
monaco-editor-core
, make sure to import monaco-languages/release/esm/yaml/yaml.contribution.js
.
You may have provided a schema configured like this:
1{ 2 uri: "http://example.com", 3 fileMatch: ["file_name.yml"], 4 schema: { 5 $schema: "http://json-schema.org/draft-07/schema#", 6 $id: "http://example.com", 7 title: "placeholder title", 8 description: "placeholder description", 9 type: "object", 10 properties: { 11 name: { 12 description: "name property description", 13 type: "string", 14 }, 15 }, 16 required: ["name"], 17 }, 18}
And would be surprised to see the error:
Unable to load schema from '<http://example.com>': Failed to fetch.
It happens because plugin uses schema URI not only as the URL to download the schema from, but also
to determine the schema name. To fix this, change the uri
parameter to
http://example.com/schema-name.json
.
Please see our contributing guidelines
Originally @kpdecker forked this repository from
monaco-json
by
@microsoft and rewrote it to work with
yaml-language-server
instead. Later
the repository maintenance was taken over by @pengx17. Eventually the
repository was tranferred to the account of @remcohaszing, who is
currently maintaining this repository with the help of @fleon and
@yazaabed.
The heavy processing is done in
yaml-language-server
, best known for
being the backbone for vscode-yaml
. This
repository provides a thin layer to add functionality provided by yaml-language-server
into
monaco-editor
.
No vulnerabilities found.
No security vulnerabilities found.