@@ -4,10 +4,17 @@ import path from "node:path";
44import http from "node:http" ;
55import browserSync from "browser-sync" ;
66
7- import { fileExists , invokeInDir } from "./utils.js" ;
7+ import {
8+ asciidocExtensions ,
9+ fileExists ,
10+ invokeInDir ,
11+ markdownExtensions ,
12+ normalizeSupportedExtnames ,
13+ } from "./utils.js" ;
814import { adocConvert } from "./adoc-convert.js" ;
15+ import { getTitleFromMarkdown , mdConvert } from "./md-convert.js" ;
916
10- export const createAsciidocMiddleware = ( rootDir , asciidoctorOptions = { } ) => {
17+ export const createAsciidocMiddleware = ( rootDir , config = { } ) => {
1118 const absoluteRootDir = path . resolve ( rootDir ) ;
1219
1320 return async ( request , res , next ) => {
@@ -21,20 +28,37 @@ export const createAsciidocMiddleware = (rootDir, asciidoctorOptions = {}) => {
2128 return res . end ( http . STATUS_CODES [ res . statusCode ] ) ;
2229 }
2330
24- if ( / \. ( a d o c | a s c i i d o c | a c s ) $ / i. test ( url . pathname ) ) {
25- const adocPath = path . join ( absoluteRootDir , url . pathname ) ;
26- const exists = await fileExists ( adocPath ) ;
27- if ( ! exists || ! adocPath . startsWith ( absoluteRootDir ) ) {
31+ const extname = normalizeSupportedExtnames ( path . extname ( url . pathname ) ) ;
32+
33+ if ( [ ".md" , ".adoc" ] . includes ( extname ) ) {
34+ const filePath = path . join ( absoluteRootDir , url . pathname ) ;
35+ const exists = await fileExists ( filePath ) ;
36+ if ( ! exists || ! filePath . startsWith ( absoluteRootDir ) ) {
2837 res . statusCode = 404 ;
2938
3039 return res . end ( http . STATUS_CODES [ res . statusCode ] ) ;
3140 }
3241
33- const adoc = await fs . readFile ( adocPath , { encoding : "utf8" } ) ;
34- let html = await invokeInDir ( path . dirname ( adocPath ) , ( ) => {
35- return adocConvert ( adoc , asciidoctorOptions ) ;
42+ const contents = await fs . readFile ( filePath , { encoding : "utf8" } ) ;
43+ let html = await invokeInDir ( path . dirname ( filePath ) , async ( ) => {
44+ switch ( extname ) {
45+ case ".adoc" : {
46+ return adocConvert ( contents , config . asciidoctorOptions ) ;
47+ }
48+ case ".md" : {
49+ const title =
50+ ( await getTitleFromMarkdown ( contents ) ) ?? "Untitled" ;
51+ return mdConvert ( contents , config . markdownOptions ) . then (
52+ ( body ) =>
53+ `<!DOCTYPE html><html><head><title>${ title } </title></head><body>${ body } </body></html>`
54+ ) ;
55+ }
56+ default : {
57+ throw new Error ( `Unsupported extension: ${ extname } ` ) ;
58+ }
59+ }
3660 } ) ;
37- res . setHeader ( "Content-Type" , "text/html" ) ;
61+ res . setHeader ( "Content-Type" , "text/html; charset=utf-8 " ) ;
3862 html = html . replace (
3963 / < \/ h e a d > / ,
4064 `<script async src="//${ request . headers . host } /browser-sync/browser-sync-client.js"></script></head>`
@@ -52,17 +76,14 @@ export const createAsciidocMiddleware = (rootDir, asciidoctorOptions = {}) => {
5276 } ;
5377} ;
5478
55- export const startAsciidocServer = async (
56- rootDir = "." ,
57- asciidoctorOptions = { }
58- ) => {
79+ export const startAsciidocServer = async ( rootDir = "." , config = { } ) => {
5980 const bs = browserSync . create ( ) ;
6081
6182 bs . init ( {
62- files : " **/*.{adoc,asciidoc,acs}" ,
83+ files : ` **/*.{${ [ ... asciidocExtensions , ... markdownExtensions ] . join ( "," ) } ` ,
6384 server : rootDir ,
64- injectFileTypes : [ "adoc" , "asciidoc" , "acs" ] ,
65- middleware : [ createAsciidocMiddleware ( rootDir , asciidoctorOptions ) ] ,
85+ injectFileTypes : [ ... asciidocExtensions , ... markdownExtensions ] ,
86+ middleware : [ createAsciidocMiddleware ( rootDir , config ) ] ,
6687 directory : true ,
6788 open : false ,
6889 ui : false ,
0 commit comments