@@ -21,10 +21,8 @@ const redirectsEvaluator = generateRedirectsEvaluator(redirectsFileContents, {
2121export default class extends WorkerEntrypoint < Env > {
2222 override async fetch ( request : Request ) {
2323 if ( request . url . endsWith ( "/index.md" ) ) {
24- const res = await this . env . ASSETS . fetch (
25- request . url . replace ( "index.md" , "" ) ,
26- request ,
27- ) ;
24+ const htmlUrl = request . url . replace ( "index.md" , "" ) ;
25+ const res = await this . env . ASSETS . fetch ( htmlUrl , request ) ;
2826
2927 if ( res . status === 404 ) {
3028 return res ;
@@ -35,8 +33,9 @@ export default class extends WorkerEntrypoint<Env> {
3533 res . headers . get ( "content-type" ) ?. startsWith ( "text/html" )
3634 ) {
3735 const html = await res . text ( ) ;
36+ const dom = parse ( html ) ;
3837
39- const content = parse ( html ) . querySelector ( ".sl-markdown-content" ) ;
38+ const content = dom . querySelector ( ".sl-markdown-content" ) ;
4039
4140 if ( ! content ) {
4241 return new Response ( "Not Found" , { status : 404 } ) ;
@@ -51,7 +50,27 @@ export default class extends WorkerEntrypoint<Env> {
5150 remarkStringify ,
5251 ] ) ;
5352
54- return new Response ( markdown , {
53+ const title = dom . querySelector ( "title" ) ?. textContent ;
54+ const description = dom . querySelector ( "meta[name='description']" )
55+ ?. attributes . content ;
56+ const lastUpdated =
57+ dom . querySelector ( ".meta time" ) ?. attributes . datetime ;
58+
59+ const withFrontmatter = [
60+ "---" ,
61+ `title: ${ title } ` ,
62+ description ? `description: ${ description } ` : [ ] ,
63+ lastUpdated ? `lastUpdated: ${ lastUpdated } ` : [ ] ,
64+ `source_url:` ,
65+ ` html: ${ htmlUrl } ` ,
66+ ` md: ${ request . url } ` ,
67+ "---\n" ,
68+ markdown ,
69+ ]
70+ . flat ( )
71+ . join ( "\n" ) ;
72+
73+ return new Response ( withFrontmatter , {
5574 headers : {
5675 "content-type" : "text/markdown; charset=utf-8" ,
5776 } ,
0 commit comments