Skip to content

Commit 2a56117

Browse files
committed
chore: wip
1 parent 2f49e04 commit 2a56117

File tree

4 files changed

+77
-11
lines changed

4 files changed

+77
-11
lines changed

examples/md-files/hello-world.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: "Hello World - stx Markdown Demo"
33
description: "A simple hello world example using stx markdown templating"
44
author: "stx"
55
date: 2025-10-08
6-
themeColor: "#FFFFFF"
6+
themeColor: "#2563eb"
77
---
88

99
# Hello, World

packages/stx/bin/cli.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ if (isDirectMode) {
280280
syntaxHighlighting: {
281281
enabled: true,
282282
serverSide: true,
283-
defaultTheme: 'github-light' as SyntaxHighlightTheme,
283+
defaultTheme: 'github-dark' as SyntaxHighlightTheme,
284284
highlightUnknownLanguages: true,
285285
},
286286
},
@@ -460,7 +460,7 @@ else {
460460
.command('dev <file>', 'Start a development server for an STX file')
461461
.option('--port <port>', 'Port to use for the dev server', { default: 3000 })
462462
.option('--no-watch', 'Disable file watching and auto-reload')
463-
.option('--highlight-theme <theme>', 'Syntax highlighting theme for Markdown code blocks', { default: 'github-light' })
463+
.option('--highlight-theme <theme>', 'Syntax highlighting theme for Markdown code blocks', { default: 'github-dark' })
464464
.option('--no-highlight', 'Disable syntax highlighting for Markdown code blocks')
465465
.option('--no-highlight-unknown', 'Disable syntax highlighting for unknown languages in Markdown')
466466
.option('--no-cache', 'Disable caching of parsed files')
@@ -493,7 +493,7 @@ else {
493493
syntaxHighlighting: {
494494
enabled: options.highlight !== false,
495495
serverSide: true,
496-
defaultTheme: (options.highlightTheme || 'github-light') as SyntaxHighlightTheme,
496+
defaultTheme: (options.highlightTheme || 'github-dark') as SyntaxHighlightTheme,
497497
highlightUnknownLanguages: options.highlightUnknown !== false,
498498
},
499499
}

packages/stx/src/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ export const defaultConfig: StxConfig = {
101101
syntaxHighlighting: {
102102
enabled: true,
103103
serverSide: true,
104-
defaultTheme: 'github-light',
104+
defaultTheme: 'github-dark',
105105
highlightUnknownLanguages: true,
106106
additionalThemes: [
107107
'light-plus',

packages/stx/src/dev-server.ts

Lines changed: 72 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,33 @@ import { config } from './config'
1111
// TODO: import this from `bun-plugin-stx`. Oddly, there seemingly are issues right now
1212
import { plugin as stxPlugin } from './plugin'
1313

14+
/**
15+
* Find an available port starting from the given port
16+
*/
17+
async function findAvailablePort(startPort: number, maxAttempts = 10): Promise<number> {
18+
for (let i = 0; i < maxAttempts; i++) {
19+
const port = startPort + i
20+
try {
21+
// Try to create a temporary server to check if port is available
22+
const testServer = serve({
23+
port,
24+
fetch: () => new Response('test'),
25+
})
26+
testServer.stop()
27+
return port
28+
}
29+
catch (error: any) {
30+
// Port is in use, try next one
31+
if (error.code === 'EADDRINUSE') {
32+
continue
33+
}
34+
// Other error, rethrow
35+
throw error
36+
}
37+
}
38+
throw new Error(`Could not find an available port between ${startPort} and ${startPort + maxAttempts - 1}`)
39+
}
40+
1441
// ANSI color codes for terminal output
1542
const colors = {
1643
reset: '\x1B[0m',
@@ -433,10 +460,23 @@ async function serveMarkdownFile(filePath: string, options: DevServerOptions = {
433460
return false
434461
}
435462

463+
// Find an available port (with fallback)
464+
let actualPort = port
465+
try {
466+
actualPort = await findAvailablePort(port)
467+
if (actualPort !== port) {
468+
console.log(`${colors.yellow}Port ${port} is busy, using port ${actualPort} instead${colors.reset}`)
469+
}
470+
}
471+
catch (error) {
472+
console.error(`${colors.red}Could not find an available port${colors.reset}`)
473+
return false
474+
}
475+
436476
// Start a server
437-
console.log(`${colors.blue}Starting server on ${colors.cyan}http://localhost:${port}/${colors.reset}...`)
477+
console.log(`${colors.blue}Starting server on ${colors.cyan}http://localhost:${actualPort}/${colors.reset}...`)
438478
const server = serve({
439-
port,
479+
port: actualPort,
440480
fetch(request) {
441481
const url = new URL(request.url)
442482

@@ -591,10 +631,23 @@ export async function serveStxFile(filePath: string, options: DevServerOptions =
591631
return false
592632
}
593633

634+
// Find an available port (with fallback)
635+
let actualPort = port
636+
try {
637+
actualPort = await findAvailablePort(port)
638+
if (actualPort !== port) {
639+
console.log(`${colors.yellow}Port ${port} is busy, using port ${actualPort} instead${colors.reset}`)
640+
}
641+
}
642+
catch (error) {
643+
console.error(`${colors.red}Could not find an available port${colors.reset}`)
644+
return false
645+
}
646+
594647
// Start a server
595-
console.log(`${colors.blue}Starting server on ${colors.cyan}http://localhost:${port}/${colors.reset}...`)
648+
console.log(`${colors.blue}Starting server on ${colors.cyan}http://localhost:${actualPort}/${colors.reset}...`)
596649
const server = serve({
597-
port,
650+
port: actualPort,
598651
fetch(request) {
599652
const url = new URL(request.url)
600653

@@ -1103,10 +1156,23 @@ export async function serveMultipleStxFiles(filePaths: string[], options: DevSer
11031156
return false
11041157
}
11051158

1159+
// Find an available port (with fallback)
1160+
let actualPort = port
1161+
try {
1162+
actualPort = await findAvailablePort(port)
1163+
if (actualPort !== port) {
1164+
console.log(`${colors.yellow}Port ${port} is busy, using port ${actualPort} instead${colors.reset}`)
1165+
}
1166+
}
1167+
catch (error) {
1168+
console.error(`${colors.red}Could not find an available port${colors.reset}`)
1169+
return false
1170+
}
1171+
11061172
// Start a server
1107-
console.log(`${colors.blue}Starting server on ${colors.cyan}http://localhost:${port}/${colors.reset}...`)
1173+
console.log(`${colors.blue}Starting server on ${colors.cyan}http://localhost:${actualPort}/${colors.reset}...`)
11081174
const server = serve({
1109-
port,
1175+
port: actualPort,
11101176
fetch(request) {
11111177
const url = new URL(request.url)
11121178

0 commit comments

Comments
 (0)