[英]Doesn't Monaco Editor support XML language by default?
I am trying to use Monaco Editor for XML language, but XML string is not getting formatted while the editor instance is being rendered, whereas its working fine for JSON.我正在尝试将 Monaco Editor 用于 XML 语言,但在呈现编辑器实例时 XML 字符串未格式化,而它在 JSON 上工作正常。
I also don't find "Format Document" option in Context Menu of Editor(if it is opened with XML language).我也没有在编辑器的上下文菜单中找到“格式化文档”选项(如果它是用 XML 语言打开的)。 Does Monaco editor support XML formatting by default?
摩纳哥编辑器默认支持XML格式吗?
It appears that Monaco Editor doesn't format xml by default.默认情况下,摩纳哥编辑器似乎不格式化 xml。 A colleague of mine - @thewahome , had to write code to format the xml.
我的一位同事 - @thewahome不得不编写代码来格式化 xml。 If you're interested on how to format xml the following snippet will help.
如果您对如何格式化 xml 感兴趣,以下代码段将有所帮助。
function formatXml(xml: any) {
const PADDING = ' '.repeat(2);
const reg = /(>)(<)(\/*)/g;
let pad = 0;
xml = xml.replace(reg, '$1\r\n$2$3');
return xml.split('\r\n').map((node: any, index: number) => {
let indent = 0;
if (node.match(/.+<\/\w[^>]*>$/)) {
indent = 0;
} else if (node.match(/^<\/\w/) && pad > 0) {
pad -= 1;
} else if (node.match(/^<\w[^>]*[^\/]>.*$/)) {
indent = 1;
} else {
indent = 0;
}
pad += indent;
return PADDING.repeat(pad - indent) + node;
}).join('\r\n');
}
The support for XML in Monaco is strange.摩纳哥对XML的支持很奇怪。 As far as I know, there is no XML formatter implemented.
据我所知,没有实现 XML 格式化程序。 But don't use XML formatting on clientside .
但不要在客户端使用 XML 格式。 It's a potential bottleneck in your app if you edit large texts.
如果您编辑大文本,这可能是您应用程序的瓶颈。 Format XML only once on your backend and send it already formatted.
仅在您的后端格式化一次 XML并发送已格式化的文件。 Monaco can be configured to follow XML indention as you type.
Monaco 可以配置为在您键入时遵循 XML 缩进。
You can play around with this in Monaco editor playground .您可以在Monaco editor playground中使用它。 (just copy, paste, run):
(只需复制、粘贴、运行):
monaco.editor.create(document.getElementById('container'), {
value: "<group>\n<content>\n<value text=\"Press Ctrl + Shift + F\">\</value>\n</content>\n</group>",
language: "xml",
tabSize: 2,
autoIndent: "full",
detectIndentation: true,
formatOnType: true,
formatOnPaste: true,
});
monaco.languages.setLanguageConfiguration("xml", {
"indentationRules": {
"increaseIndentPattern": new RegExp("<(?!\\?|[^>]*\\/>)([-_.A-Za-z0-9]+)(?=\\s|>)\\b[^>]*>(?!.*<\\/\\1>)|<!--(?!.*-->)|\\{[^}\"']*$"),
"decreaseIndentPattern": new RegExp("^\\s*(<\\/(?!html)[-_.A-Za-z0-9]+\\b[^>]*>|-->|})"),
},
});
monaco.editor.addKeybindingRules([
{
// Reindent lines with Ctrl + Shift + F
keybinding: monaco.KeyMod.CtrlCmd | monaco.KeyMod.Shift | monaco.KeyCode.KeyF,
command: "editor.action.reindentlines",
},
]);
indentationRules
are borrowed from vscode HTML extension configuration and simplified. indentationRules
借鉴了vscode HTML 扩展配置,并进行了简化。 More information about it here .更多信息请点击此处。
This configuration also enables the "Reindent lines" action in the context menu.此配置还在上下文菜单中启用“Reindent lines”操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.