Skip to content

feat: syntax highlight inline template #59

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 18, 2020
Merged

Conversation

znck
Copy link
Member

@znck znck commented Mar 16, 2020

This PR includes syntax highlight support for inline templates, e.g.: fig. 1.

inline template syntax highlight

Fig. 1: Inline template syntax highlight

Any object with a string template key is highlighted, e.g.:

const A = Vue.defineComponent({
    template: '<div />' // or "<div />"
})
const B = Vue.defineComponent({
    template: `<div />`
})

Theming

Adequate class names are added to customize the appearance of different tokens, see fig. 2.

DOM structure of highlighted inline templates

Fig. 2: Token classes to style parts of a directive.

Caveats

  1. Cannot highlight in an invalid JavaScript snippet.

    const app = Vue.createApp(...)
    //                        ^^^ invalid
    const app = Vue.createApp(/* ... */)
    //                           ^^^ valid
  2. Cannot highlight template strings with interpolation.

    const app = Vue.createApp({
        template: `
            <div attr="${foo}" />
        `//            ^^^^^^ highlight is aborted due to interpolation expression. 
    })

Bonus

You can use this highlighter for template snippets too, just use vue-html as language.

    Example snippet:
    
    ```vue-html
    <div v-bind="$attrs" />
    ```

@znck znck requested a review from phanan March 16, 2020 08:59
Copy link
Member

@NataliaTepluhina NataliaTepluhina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Rahul! This looks great

Copy link
Member

@phanan phanan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is wonderful. I'm only adding several super minor CS "fixes," which can pretty much be ignored.

znck and others added 3 commits March 16, 2020 19:15
Co-Authored-By: Phan An <me@phanan.net>
Co-Authored-By: Phan An <me@phanan.net>
Co-Authored-By: Phan An <me@phanan.net>
@znck znck merged commit 40e83d9 into master Mar 18, 2020
@znck znck deleted the template-syntax-highlight branch March 18, 2020 05:32
moHaHa pushed a commit to moHaHa/vuejs-docs that referenced this pull request May 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants