1+ import type { WatchEventType } from "node:fs" ;
12import { watch , type FSWatcher } from "node:fs" ;
23import { access , constants , readFile , stat } from "node:fs/promises" ;
34import { createServer , type IncomingMessage , type RequestListener } from "node:http" ;
@@ -7,9 +8,8 @@ import {parseArgs} from "node:util";
78import send from "send" ;
89import { WebSocketServer , type WebSocket } from "ws" ;
910import { HttpError , isHttpError , isNodeError } from "./error.js" ;
10- import { computeHash } from "./hash.js" ;
1111import type { ParseResult } from "./markdown.js" ;
12- import { diffMarkdown , parseMarkdown } from "./markdown.js" ;
12+ import { diffMarkdown , readMarkdown } from "./markdown.js" ;
1313import { readPages } from "./navigation.js" ;
1414import { renderPreview } from "./render.js" ;
1515
@@ -135,19 +135,19 @@ function handleWatch(socket: WebSocket, root: string) {
135135 } ) ;
136136 }
137137
138- async function readMarkdown ( p : string , r : string ) {
139- const contents = await readFile ( p , "utf-8" ) ;
140- return { contents, parse : parseMarkdown ( contents , r ) , hash : computeHash ( contents ) } ;
141- }
142-
143138 async function refreshMarkdown ( path : string ) {
144139 let current = await readMarkdown ( path , root ) ;
145140 attachmentWatcher = new FileWatchers ( root , current . parse . files , refreshAttachment ( current . parse ) ) ;
146- return async ( ) => {
147- // TODO: Sometimes this gets an ENOENT as a transitional state.
141+ return async ( event : WatchEventType ) => {
142+ if ( event !== "change" ) return ; // TODO: decide how to handle a "rename" event
148143 const updated = await readMarkdown ( path , root ) ;
149144 if ( current . hash !== updated . hash ) {
150- send ( { type : "update" , length : current . parse . pieces . length , diff : diffMarkdown ( current . parse , updated . parse ) } ) ;
145+ send ( {
146+ type : "update" ,
147+ diff : diffMarkdown ( current , updated ) ,
148+ previousHash : current . hash ,
149+ updatedHash : updated . hash
150+ } ) ;
151151 attachmentWatcher ?. close ( ) ;
152152 attachmentWatcher = new FileWatchers ( root , updated . parse . files , refreshAttachment ( updated . parse ) ) ;
153153 current = updated ;
0 commit comments