Skip to content

Commit 20f9964

Browse files
committed
feat: Adding some generic and reusable composables
1 parent 7555f40 commit 20f9964

File tree

7 files changed

+169
-0
lines changed

7 files changed

+169
-0
lines changed

composables/useCamelize.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
export default function () {
2+
function camelize(str) {
3+
// Split the string at all dash characters
4+
return (
5+
str
6+
.split('-')
7+
// Convert first char to upper case for each word
8+
.map(a => a[0].toUpperCase() + a.substring(1))
9+
// Join all the strings back together
10+
.join('')
11+
)
12+
}
13+
14+
return {
15+
camelize,
16+
}
17+
}

composables/useMenuItems.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { queryCollection, useAsyncData } from '@nuxt/content'
2+
3+
export default function () {
4+
async function fetchMenuItems(menu) {
5+
return await queryCollection('menu')
6+
.where('stem', '=', `menu/${menu}`)
7+
.first()
8+
}
9+
async function fetchMenus() {
10+
return await queryCollection('menu').all()
11+
}
12+
13+
async function fetchMenuItemsAsync(menu) {
14+
const path = `menu/${menu}`
15+
return await useAsyncData(
16+
path,
17+
async () =>
18+
await queryCollection('menu').where('stem', '=', path).first(),
19+
)
20+
}
21+
22+
return {
23+
fetchMenuItems,
24+
fetchMenus,
25+
fetchMenuItemsAsync,
26+
}
27+
}

composables/useRenderedMarkdown.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { useNuxtApp } from '#app'
2+
3+
export default function () {
4+
function renderedMarkdown(text) {
5+
// @TODO This hack can be removed once this has been merged:
6+
// https://github.com/nuxt/content/pull/3320
7+
let cleanedText = text
8+
cleanedText = cleanedText.replaceAll('\\n', '\n')
9+
return useNuxtApp().$mdit.render(cleanedText)
10+
}
11+
return {
12+
renderedMarkdown,
13+
}
14+
}

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,9 @@
1919
"nuxt-svg-icon-sprite": "2.0.2",
2020
"typescript": "^5.9.2",
2121
"vue": "latest"
22+
},
23+
"dependencies": {
24+
"lazysizes": "^5.3.2",
25+
"markdown-it": "^14.1.0"
2226
}
2327
}

plugins/lazysizes.client.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import lazySizes from 'lazysizes'
2+
import { defineNuxtPlugin } from '#app'
3+
4+
import 'lazysizes/plugins/parent-fit/ls.parent-fit'
5+
6+
window.lazySizesConfig = window.lazySizesConfig || {}
7+
window.lazySizesConfig.expFactor = 10
8+
window.lazySizesConfig.loadMode = 3
9+
window.lazySizesConfig.loadHidden = false
10+
11+
export default defineNuxtPlugin((nuxtApp) => {
12+
nuxtApp.provide('lazySizes', lazySizes)
13+
})

plugins/markdownIt.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import MarkdownIt from 'markdown-it'
2+
import { defineNuxtPlugin } from '#app'
3+
4+
const markdownIt = new MarkdownIt({
5+
html: true,
6+
xhtmlOut: false,
7+
breaks: true,
8+
langPrefix: 'language-',
9+
linkify: true,
10+
typographer: true,
11+
quotes: '“”‘’',
12+
})
13+
14+
markdownIt.linkify.set({ fuzzyEmail: false })
15+
16+
// Remember old renderer, if overridden, or proxy to default renderer
17+
const defaultRender = markdownIt.renderer.rules.link_open || function (tokens, idx, options, env, self) {
18+
return self.renderToken(tokens, idx, options)
19+
}
20+
21+
markdownIt.renderer.rules.link_open = function (tokens, idx, options, env, self) {
22+
// If you are sure other plugins can't add `target` - drop check below
23+
const aIndex = tokens[idx].attrIndex('target')
24+
const relIndex = tokens[idx].attrIndex('rel')
25+
26+
if (aIndex < 0) {
27+
tokens[idx].attrPush(['target', '_blank'])
28+
}
29+
else {
30+
tokens[idx].attrs[aIndex][1] = '_blank'
31+
}
32+
33+
if (relIndex < 0) {
34+
tokens[idx].attrPush(['rel', 'noopener noreferrer'])
35+
}
36+
else {
37+
tokens[idx].attrs[relIndex][1] = 'noopener noreferrer'
38+
}
39+
40+
// pass token to default renderer.
41+
return defaultRender(tokens, idx, options, env, self)
42+
}
43+
44+
export default defineNuxtPlugin((nuxtApp) => {
45+
nuxtApp.provide('mdit', markdownIt)
46+
})

pnpm-lock.yaml

Lines changed: 48 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)