@@ -17,7 +17,7 @@ export async function getMemos(): Promise<Memo[]> {
1717 Object . entries ( memoModules ) . map ( async ( [ path , rawContent ] ) => {
1818 const slug = path . split ( '/' ) . pop ( ) ?. replace ( '.md' , '' ) || 'unknown' ;
1919
20- const markdownString = rawContent as string ;
20+ let markdownString = rawContent as string ;
2121
2222 const resolveAssets = ( markdown : string , memoPath : string ) => {
2323 return markdown . replace ( / ! \[ ( .* ?) \] \( ( .* ?) \) / g, ( match , alt , distinctUrl ) => {
@@ -50,27 +50,67 @@ export async function getMemos(): Promise<Memo[]> {
5050 } ) ;
5151 } ;
5252
53- let processedMarkdown = resolveAssets ( markdownString , path ) ;
53+ const fmRegex = / ^ - - - \s * [ \r \n ] + ( [ \s \S ] * ?) [ \r \n ] + - - - / ;
54+ const fmMatch = markdownString . match ( fmRegex ) ;
55+ let created : Date | null = null ;
56+ let modified : Date | null = null ;
57+
58+ if ( fmMatch ) {
59+ const fm = fmMatch [ 1 ] ;
60+ markdownString = markdownString . replace ( fmRegex , '' ) . trim ( ) ;
61+
62+ const createdMatch = fm . match ( / c r e a t e d : \s * ( .+ ) / ) ;
63+ if ( createdMatch ) {
64+ const createdStr = createdMatch [ 1 ] . trim ( ) ;
65+ try {
66+ created = new Date ( createdStr ) ;
67+ } catch ( e ) {
68+ console . error ( `Failed to parse created date from frontmatter for ${ slug } :` , e ) ;
69+ }
70+ }
71+
72+ const modifiedMatch = fm . match ( / m o d i f i e d : \s * ( .+ ) / ) ;
73+ if ( modifiedMatch ) {
74+ const modifiedStr = modifiedMatch [ 1 ] . trim ( ) ;
75+ try {
76+ modified = new Date ( modifiedStr ) ;
77+ } catch ( e ) {
78+ console . error ( `Failed to parse modified date from frontmatter for ${ slug } :` , e ) ;
79+ }
80+ }
81+ }
5482
55- processedMarkdown = processedMarkdown . replace (
83+ let markdown = resolveAssets ( markdownString , path ) ;
84+
85+ markdown = markdown . replace (
5686 / ( ^ | \s ) # ( [ ^ \s # . , ! ? ; : ( ) \[ \] " ' ] + ) / g,
5787 '$1<button class="tag-link" data-tag="$2">#$2</button>'
5888 ) ;
5989
60- const htmlContent = await marked . parse ( processedMarkdown ) ;
90+ const htmlContent = await marked . parse ( markdown ) ;
6191
6292 let date = new Date ( ) ;
63- const match = slug . match ( / ^ ( \d { 4 } ) ( \d { 2 } ) ( \d { 2 } ) ( \d { 2 } ) ( \d { 2 } ) ( \d { 2 } ) $ / ) ;
64- if ( match ) {
65- const year = match [ 1 ] ;
66- const month = match [ 2 ] ;
67- const day = match [ 3 ] ;
68- const hour = match [ 4 ] ;
69- const minute = match [ 5 ] ;
70- const second = match [ 6 ] ;
71-
72- const isoString = `${ year } -${ month } -${ day } T${ hour } :${ minute } :${ second } Z` ;
73- date = new Date ( isoString ) ;
93+
94+ const filenameDate = ( ( ) => {
95+ const match = slug . match ( / ^ ( \d { 4 } ) ( \d { 2 } ) ( \d { 2 } ) ( \d { 2 } ) ( \d { 2 } ) ( \d { 2 } ) $ / ) ;
96+ if ( match ) {
97+ const year = match [ 1 ] ;
98+ const month = match [ 2 ] ;
99+ const day = match [ 3 ] ;
100+ const hour = match [ 4 ] ;
101+ const minute = match [ 5 ] ;
102+ const second = match [ 6 ] ;
103+
104+ const isoString = `${ year } -${ month } -${ day } T${ hour } :${ minute } :${ second } Z` ;
105+ return new Date ( isoString ) ;
106+ }
107+ return null ;
108+ } ) ( ) ;
109+
110+ if ( config . order_by === 'modified' ) {
111+ date = modified || created || filenameDate || new Date ( ) ;
112+ } else {
113+ date = created || filenameDate || new Date ( ) ;
74114 }
75115
76116 let tags : string [ ] = [ ] ;
@@ -89,7 +129,7 @@ export async function getMemos(): Promise<Memo[]> {
89129 } )
90130 ) ;
91131
92- memos . sort ( ( a , b ) => b . slug . localeCompare ( a . slug ) ) ;
132+ memos . sort ( ( a , b ) => b . date . getTime ( ) - a . date . getTime ( ) ) ;
93133
94134 return memos ;
95135}
0 commit comments