2

Trying to get CodeMirror's HTML highlighting to work (which as I see, has been a problem to some people as well) in my angular-cli (Angular 2 ) application.

Here's what I have:

app.component.html

<codemirror [(ngModel)]="code" [config]="config"></codemirror>
<div [innerHTML]="code"></div>

app.component.ts

export class AppComponent {
  code = `<p>My HTML</p>\n<!-- hello -->\n<p>Your HTML</p>`;

  config = {
    mode: 'htmlmixed',
    lineWrapping: false,
    lineNumbers: true,
    theme: 'monokai'
  };
}

angular-cli.json (listing the relevant section only)

"addons": [
    "../node_modules/codemirror/mode/xml/xml.js",
    "../node_modules/codemirror/mode/javascript/javascript.js",
    "../node_modules/codemirror/mode/htmlmixed/htmlmixed.js"
],

/.../

"styles": [
    "../node_modules/codemirror/lib/codemirror.css",
    "../node_modules/codemirror/theme/monokai.css"
],

app.module.ts

imports: [
    /* ... */
    CodemirrorModule,
],

I have tried various syntax patterns passing the mode as object or string, and - as you see - making sure that the xml gets loaded first. No go - the highlight is not there. I was always making sure to reboot ng serve after each edit of angular-cli.json.

Does anyone know where the problem could be buried?

1 Answer 1

5

You need to add <script src="node_modules/codemirror/lib/codemirror.js"></script> to index.html and you need to import mode you want to use in your component:

import 'codemirror/mode/htmlmixed/htmlmixed'; <-- this

export class AppComponent {
code = `<p>My HTML</p>\n<!-- hello -->\n<p>Your HTML</p>`;

config = {
  mode: 'htmlmixed',
  lineWrapping: false,
  lineNumbers: true,
  theme: 'monokai'
};
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. THat obviously helped. Seem like messing with the angular-cli.json is not require for js libs, just for attaching css.
@nuton Yeah, it's a bit problematic, don't know if you'll believe this, but I wasted a whole day to find out how to make highlighting work. :-)
I will. This kind of time waste happens to me every other day... Now I'm struggling with font metrics and incorrect text widths when it came to styling a custome theme with a non-standard font. Don't know how long it'll take. Duh. Thanks for sharing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.