Skip to content

Add Vue example #42

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions examples/vue/App.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<script setup lang="ts">
import SpeedHighlight from 'SpeedHighlight.vue'

import { ref } from 'vue'

const myCode = ref(`printf('Hello World!\n');`)
const myLang = ref('c')
</script>

<template>
<p>Here is a the example code:</p>
<SpeedHighlight :content="myCode" :lang="myLang" />
</template>
20 changes: 20 additions & 0 deletions examples/vue/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# VueJs 3.x Example of a Speed Highlight Component

## Install

```console
$ pnpm i @speed-highlight/core
Copy link
Member

Choose a reason for hiding this comment

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

I think it's better to stick with npm in the installation guide for clarity

```

## SpeedHighlight.vue

This is a simple component that wraps `speed-highlight` and makes it easier to use in your Vue app.
It takes two props: `content` & `lang` - and you can include it in your pages like this:

```vue
<SpeedHighlight content="printf('Hello World!\n');" lang="c" />
```

## App.vue

Simplified example page/parent, to show how you include `<SpeedHighlight />` on a page.
17 changes: 17 additions & 0 deletions examples/vue/SppedHighlight.vue
Copy link
Member

Choose a reason for hiding this comment

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

The filename has a typo

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<script lang="ts" setup>
import { defineProps, ref, onMounted } from 'vue'
import { highlightText } from '@speed-highlight/core'
import '@speed-highlight/core/dist/themes/default.css'

const { content = '', lang = 'c' } = defineProps<{ content: string; lang: string }>()

const highlightedCode = ref('')

onMounted(async () => {
highlightedCode.value = await highlightText(content, lang)
})
Comment on lines +6 to +12
Copy link
Member

Choose a reason for hiding this comment

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

I'm not very familiar with Vue, but it seems like the code isn't reactive to updates in the props value. It might be better to make it reactive to the props

</script>

<template>
<div :class="`shj-lang-${lang}`" v-html="highlightedCode"></div>
</template>