Skip to content

Commit d58dcfa

Browse files
committed
refactor: Convert composables to txpescript
1 parent dc28925 commit d58dcfa

File tree

4 files changed

+91
-31
lines changed

4 files changed

+91
-31
lines changed

composables/useCamelize.js

Lines changed: 0 additions & 17 deletions
This file was deleted.

composables/useCamelize.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
export default function useCamelize() {
2+
/**
3+
* Converts a dash-separated string to PascalCase
4+
* @param input - The dash-separated string to convert (e.g., "hello-world")
5+
* @returns The PascalCase string (e.g., "HelloWorld")
6+
* @example
7+
* toPascalCase("hello-world") // returns "HelloWorld"
8+
* toPascalCase("my-component-name") // returns "MyComponentName"
9+
*/
10+
function toPascalCase(input: string): string {
11+
// Handle edge cases
12+
if (!input || typeof input !== 'string') {
13+
return ''
14+
}
15+
16+
// Split the string at dash characters and filter out empty parts
17+
const wordParts = input.split('-').filter(part => part.length > 0)
18+
19+
// Convert each word: first character to uppercase, rest to lowercase
20+
const pascalCaseWords = wordParts.map(capitalizeFirstLetter)
21+
22+
// Join all words together
23+
return pascalCaseWords.join('')
24+
}
25+
26+
/**
27+
* Capitalizes the first letter of a word and keeps the rest as-is
28+
* @param word - The word to capitalize
29+
* @returns The word with first letter capitalized
30+
*/
31+
function capitalizeFirstLetter(word: string): string {
32+
if (word.length === 0) {
33+
return ''
34+
}
35+
36+
const firstCharacter = word[0]?.toUpperCase() ?? ''
37+
const remainingCharacters = word.substring(1)
38+
39+
return firstCharacter + remainingCharacters
40+
}
41+
42+
// Keep the original name for backward compatibility
43+
const camelize = toPascalCase
44+
45+
return {
46+
camelize,
47+
toPascalCase,
48+
capitalizeFirstLetter,
49+
}
50+
}

composables/useRenderedMarkdown.js

Lines changed: 0 additions & 14 deletions
This file was deleted.

composables/useRenderedMarkdown.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { useNuxtApp } from '#app'
2+
3+
interface MarkdownRenderer {
4+
render(text: string): string
5+
}
6+
7+
export default function useRenderedMarkdown() {
8+
/**
9+
* Renders markdown text to HTML using the Nuxt Content markdown-it instance
10+
* @param text - The markdown text to render
11+
* @returns The rendered HTML string
12+
* @example
13+
* renderMarkdown("# Hello\nWorld") // returns "<h1>Hello</h1><p>World</p>"
14+
*/
15+
function renderMarkdown(text: string): string {
16+
// Handle edge cases
17+
if (!text || typeof text !== 'string') {
18+
return ''
19+
}
20+
21+
// Clean up escaped newlines
22+
// @TODO This hack can be removed once this has been merged:
23+
// https://github.com/nuxt/content/pull/3320
24+
const cleanedText = text.replaceAll('\\n', '\n')
25+
26+
// Get the Nuxt app instance and access the markdown-it renderer
27+
const nuxtApp = useNuxtApp()
28+
const markdownRenderer = nuxtApp.$mdit as MarkdownRenderer
29+
30+
// Render the markdown to HTML
31+
return markdownRenderer.render(cleanedText)
32+
}
33+
34+
// Keep the original name for backward compatibility
35+
const renderedMarkdown = renderMarkdown
36+
37+
return {
38+
renderMarkdown,
39+
renderedMarkdown,
40+
}
41+
}

0 commit comments

Comments
 (0)