@@ -3,7 +3,7 @@ use markdown::{
33 mdast:: { FootnoteDefinition , Node , Text } ,
44} ;
55use notify:: { EventKind , RecursiveMode , Watcher , event:: ModifyKind } ;
6- // use rouille::{router, try_or_400, websocket};
6+ use rouille:: { router, try_or_400, websocket} ;
77use serde:: { Deserialize , Serialize } ;
88use std:: {
99 borrow:: Cow ,
@@ -13,6 +13,7 @@ use std::{
1313 hash:: { DefaultHasher , Hash , Hasher } ,
1414 path:: { Path , PathBuf } ,
1515 process:: Command ,
16+ thread,
1617 time:: Instant ,
1718} ;
1819
@@ -933,6 +934,7 @@ fn md_render_article(
933934 } ,
934935 )
935936 . unwrap ( ) ;
937+ search_index. ingest_md_ast ( & md_ast, & html_path) ;
936938
937939 md_lint_rec ( & md_ast, & md_path) ;
938940
@@ -1312,7 +1314,7 @@ fn check_langs() {
13121314 }
13131315}
13141316
1315- fn watch ( cache : & mut Cache ) {
1317+ fn watch ( etx : crossbeam_channel :: Sender < ( ) > , cache : & mut Cache ) {
13161318 let ( tx, rx) = std:: sync:: mpsc:: channel :: < notify:: Result < notify:: Event > > ( ) ;
13171319 let mut watcher = notify:: recommended_watcher ( tx) . unwrap ( ) ;
13181320 watcher
@@ -1325,15 +1327,17 @@ fn watch(cache: &mut Cache) {
13251327 EventKind :: Access ( _access_kind) => { }
13261328 EventKind :: Modify ( ModifyKind :: Data ( _) ) => {
13271329 for path in event. paths {
1328- let stem = path. file_stem ( ) . unwrap ( ) ;
1329- if stem == & "header.html" . as_ref ( ) || stem == & "footer.html" . as_ref ( ) {
1330+ let file_name = path. file_name ( ) . unwrap ( ) ;
1331+ if file_name == & "header.html" . as_ref ( )
1332+ || file_name == & "footer.html" . as_ref ( )
1333+ {
13301334 println ! (
13311335 "🔄 header/footer changed, rebuilding & reloading all files: {}" ,
1332- stem . to_str( ) . unwrap( )
1336+ file_name . to_str( ) . unwrap( )
13331337 ) ;
13341338 cache. clear ( ) ;
13351339 generate_all ( cache) ;
1336- // websocket.send_text("" ).unwrap();
1340+ etx . send ( ( ) ) . unwrap ( ) ;
13371341 }
13381342 if path. extension ( ) == Some ( ".js" . as_ref ( ) )
13391343 || path. extension ( ) == Some ( ".css" . as_ref ( ) )
@@ -1347,17 +1351,17 @@ fn watch(cache: &mut Cache) {
13471351 {
13481352 println ! (
13491353 "🔄 asset changed, reloading all files: {}" ,
1350- stem . to_str( ) . unwrap( )
1354+ file_name . to_str( ) . unwrap( )
13511355 ) ;
1352- // websocket.send_text("" ).unwrap();
1356+ etx . send ( ( ) ) . unwrap ( ) ;
13531357 }
13541358 if path. extension ( ) == Some ( "md" . as_ref ( ) ) {
13551359 println ! (
13561360 "🔄 md file changed, rebuilding & reloading it: {}" ,
1357- stem . to_str( ) . unwrap( )
1361+ file_name . to_str( ) . unwrap( )
13581362 ) ;
13591363 generate_all ( cache) ;
1360- // websocket.send_text(&file_path_str ).unwrap();
1364+ etx . send ( ( ) ) . unwrap ( ) ;
13611365 }
13621366 }
13631367 }
@@ -1395,82 +1399,39 @@ fn main() {
13951399 if let Some ( arg) = arg1
13961400 && arg == "watch"
13971401 {
1398- watch ( & mut cache) ;
1399- }
1402+ let ( etx, erx) = crossbeam_channel:: unbounded ( ) ;
1403+ thread:: spawn ( move || {
1404+ watch ( etx, & mut cache) ;
1405+ } ) ;
14001406
1401- // let cache = Arc::new(Mutex::new(cache));
1402- // rouille::start_server("localhost:8001", move |request| {
1403- // router!(request,
1404- // (GET) (/blog) => {
1405- // rouille::Response::redirect_302("/blog/index.html")
1406- // },
1407- // (GET) (/blog/) => {
1408- // rouille::Response::redirect_302("/blog/index.html")
1409- // },
1410- // (GET) (/ws) => {
1411- // let (response, websocket) = try_or_400!(websocket::start(request, Some("echo")));
1412-
1413- // let cache = cache.clone();
1414- // thread::spawn(move || {
1415- // // This line will block until the `response` above has been returned.
1416- // let ws = websocket.recv().unwrap();
1417- // websocket_handling_thread(ws, cache);
1418- // });
1419- // response
1420- // },
1421- // _ => rouille::match_assets(request, "..")
1422- // )
1423- // });
1407+ rouille:: start_server ( "localhost:8001" , move |request| {
1408+ router ! ( request,
1409+ ( GET ) ( /blog) => {
1410+ rouille:: Response :: redirect_302( "/blog/index.html" )
1411+ } ,
1412+ ( GET ) ( /blog/) => {
1413+ rouille:: Response :: redirect_302( "/blog/index.html" )
1414+ } ,
1415+ ( GET ) ( /ws) => {
1416+ let ( response, websocket) = try_or_400!( websocket:: start( request, Some ( "echo" ) ) ) ;
1417+
1418+ let erx= erx. clone( ) ;
1419+ thread:: spawn( move || {
1420+ // This line will block until the `response` above has been returned.
1421+ let mut ws = websocket. recv( ) . unwrap( ) ;
1422+ println!( "ws connection" ) ;
1423+ loop {
1424+ if let Ok ( _) = erx. recv( ) {
1425+ println!( "ws event" ) ;
1426+ ws. send_text( "reload" ) . unwrap( ) ;
1427+ println!( "ws event sent" ) ;
1428+ }
1429+ }
1430+ } ) ;
1431+ response
1432+ } ,
1433+ _ => rouille:: match_assets( request, ".." )
1434+ )
1435+ } ) ;
1436+ }
14241437}
1425-
1426- // fn websocket_handling_thread(
1427- // mut websocket: websocket::Websocket,
1428- // cache: Arc<Mutex<HashMap<String, Article>>>,
1429- // ) {
1430- // println!("🚀 new websocket");
1431-
1432- // let mut watcher = notify::recommended_watcher(|event: Result<notify::Event, notify::Error>| {
1433- // if let Ok(event) = event {
1434- // dbg!(&event);
1435- // // let file_path_str = event.path.file_stem().unwrap_or_default().to_string_lossy();
1436- // // let path_str = event.path.to_str().unwrap();
1437- // // match path_str {
1438- // // _ if path_str.ends_with("header.html") || path_str.ends_with("footer.html") => {
1439- // // println!("🔄 header/footer changed, rebuilding & reloading all files");
1440- // // let mut cache = cache.lock().unwrap();
1441- // // cache.clear();
1442- // // generate_all(&mut cache);
1443- // // websocket.send_text("").unwrap();
1444- // // }
1445- // // _ if path_str.ends_with(".js")
1446- // // || path_str.ends_with(".css")
1447- // // || path_str.ends_with(".svg")
1448- // // || path_str.ends_with(".png")
1449- // // || path_str.ends_with(".webm")
1450- // // || path_str.ends_with(".mp4")
1451- // // || path_str.ends_with(".jpeg")
1452- // // || path_str.ends_with(".ico")
1453- // // || path_str.ends_with(".gif") =>
1454- // // {
1455- // // println!("🔄 asset changed, reloading all files: {}", path_str);
1456- // // websocket.send_text("").unwrap();
1457- // // }
1458- // // _ if path_str.ends_with(".md") => {
1459- // // println!(
1460- // // "🔄 md file changed, rebuilding & reloading it: {}",
1461- // // path_str
1462- // // );
1463- // // let mut cache = cache.lock().unwrap();
1464- // // generate_all(&mut cache);
1465- // // websocket.send_text(&file_path_str).unwrap();
1466- // // }
1467- // // _ => {}
1468- // // };
1469- // }
1470- // })
1471- // .unwrap();
1472- // watcher
1473- // .watch(Path::new("."), RecursiveMode::Recursive)
1474- // .unwrap();
1475- // println!("end of file watch & websocket handling");
1476- // }
0 commit comments