@@ -6,6 +6,7 @@ import {createServer} from "node:http";
6
6
import type { IncomingMessage , RequestListener , Server , ServerResponse } from "node:http" ;
7
7
import { basename , dirname , extname , join , normalize } from "node:path" ;
8
8
import { fileURLToPath } from "node:url" ;
9
+ import { difference } from "d3-array" ;
9
10
import send from "send" ;
10
11
import { type WebSocket , WebSocketServer } from "ws" ;
11
12
import { version } from "../package.json" ;
@@ -14,6 +15,7 @@ import {Loader} from "./dataloader.js";
14
15
import { HttpError , isEnoent , isHttpError , isSystemError } from "./error.js" ;
15
16
import { FileWatchers } from "./fileWatchers.js" ;
16
17
import { createImportResolver , rewriteModule } from "./javascript/imports.js" ;
18
+ import { getImplicitSpecifiers , getImplicitStylesheets } from "./libraries.js" ;
17
19
import { diffMarkdown , readMarkdown } from "./markdown.js" ;
18
20
import type { ParseResult , ReadMarkdownResult } from "./markdown.js" ;
19
21
import { renderPreview } from "./render.js" ;
@@ -246,10 +248,17 @@ function getWatchPaths(parseResult: ParseResult): string[] {
246
248
return paths ;
247
249
}
248
250
251
+ function getStylesheets ( { cells} : ParseResult ) : Set < string > {
252
+ const inputs = new Set < string > ( ) ;
253
+ for ( const cell of cells ) cell . inputs ?. forEach ( inputs . add , inputs ) ;
254
+ return getImplicitStylesheets ( getImplicitSpecifiers ( inputs ) ) ;
255
+ }
256
+
249
257
function handleWatch ( socket : WebSocket , req : IncomingMessage , options : { root : string } ) {
250
258
const { root} = options ;
251
259
let path : string | null = null ;
252
260
let current : ReadMarkdownResult | null = null ;
261
+ let stylesheets : Set < string > | null = null ;
253
262
let markdownWatcher : FSWatcher | null = null ;
254
263
let attachmentWatcher : FileWatchers | null = null ;
255
264
console . log ( faint ( "socket open" ) , req . url ) ;
@@ -285,6 +294,10 @@ function handleWatch(socket: WebSocket, req: IncomingMessage, options: {root: st
285
294
case "change" : {
286
295
const updated = await readMarkdown ( path , root ) ;
287
296
if ( current . parse . hash === updated . parse . hash ) break ;
297
+ const updatedStylesheets = getStylesheets ( updated . parse ) ;
298
+ for ( const href of difference ( stylesheets , updatedStylesheets ) ) send ( { type : "remove-stylesheet" , href} ) ;
299
+ for ( const href of difference ( updatedStylesheets , stylesheets ) ) send ( { type : "add-stylesheet" , href} ) ;
300
+ stylesheets = updatedStylesheets ;
288
301
const diff = diffMarkdown ( current , updated ) ;
289
302
send ( { type : "update" , diff, previousHash : current . parse . hash , updatedHash : updated . parse . hash } ) ;
290
303
current = updated ;
@@ -303,6 +316,7 @@ function handleWatch(socket: WebSocket, req: IncomingMessage, options: {root: st
303
316
path += ".md" ;
304
317
current = await readMarkdown ( path , root ) ;
305
318
if ( current . parse . hash !== initialHash ) return void send ( { type : "reload" } ) ;
319
+ stylesheets = getStylesheets ( current . parse ) ;
306
320
attachmentWatcher = await FileWatchers . of ( root , path , getWatchPaths ( current . parse ) , refreshAttachment ) ;
307
321
markdownWatcher = watch ( join ( root , path ) , watcher ) ;
308
322
}
0 commit comments