@@ -15,7 +15,7 @@ import { gfmFromMarkdown } from 'mdast-util-gfm';
1515import { frontmatterFromMarkdown } from 'mdast-util-frontmatter' ;
1616
1717import { extractTagsFromText , extractTagsFromYaml } from './tags.js' ;
18- import { FOLDER_META_FILE , joinMergeWhitespace , normalizeWhitespace , SPACE } from './utils.js' ;
18+ import { DATE_REGEXP , FOLDER_META_FILE , joinMergeWhitespace , normalizeWhitespace , SPACE } from './utils.js' ;
1919
2020const WL_REGEXP = / ^ W L : ( \d { 1 , 2 } (?: \. \d { 1 , 2 } ) ? ) [ h H ] \s / ;
2121
@@ -32,6 +32,13 @@ const collectTextDepthFirst = (root: Node | undefined, acc: string = ''): string
3232 return acc ;
3333} ;
3434
35+ const extractDateFromText = ( text : string , tags : TagMap , tag_name : string = 'date' ) => {
36+ const date_match = text . match ( DATE_REGEXP ) ;
37+ if ( date_match ) {
38+ tags [ tag_name ] = date_match [ 1 ] . replaceAll ( '-' , '' ) ;
39+ }
40+ } ;
41+
3542const parseListItemNode = ( node : ListItem , ctx : ParseFileContext , item : Task | Worklog | null ) => {
3643 if ( ! item ) {
3744 const text = collectTextDepthFirst ( node ) ;
@@ -73,12 +80,13 @@ const parseParentNode = (node: Parent, ctx: ParseFileContext, item: Task | Workl
7380
7481const parseHeadingNode = ( node : Heading , ctx : ParseFileContext , item : Task | Worklog | null ) => {
7582 let parent = ctx . heading ;
76- while ( parent && parent . depth > node . depth ) {
83+ while ( parent && parent . depth >= node . depth ) {
7784 parent = parent . parent ;
7885 }
7986 const tags = parent ? { ...parent . tags } : { } ;
8087 const text = collectTextDepthFirst ( node ) ;
8188 extractTagsFromText ( text , tags ) ;
89+ extractDateFromText ( text , tags , 'date' ) ;
8290 ctx . heading = { depth : node . depth , tags, parent } ;
8391} ;
8492
@@ -126,8 +134,6 @@ const from_markdown_opts = {
126134 mdastExtensions : [ frontmatterFromMarkdown ( [ 'yaml' ] ) , gfmFromMarkdown ( ) ] ,
127135} ;
128136
129- const DATE_IN_FILENAME_REGEXP = / (?: ^ | [ ^ \d ] ) ( \d { 8 } | (?: \d { 4 } - \d { 2 } - \d { 2 } ) ) (?: $ | [ ^ \d ] ) / ;
130-
131137export const parseFile = async ( ctx : ParseFileContext ) => {
132138 ctx . tasks . forEach ( ( task ) => {
133139 if ( task . file === ctx . file ) {
@@ -142,10 +148,7 @@ export const parseFile = async (ctx: ParseFileContext) => {
142148 try {
143149 const data = await readFile ( ctx . file , { encoding : 'utf8' } ) ;
144150 const root_node = fromMarkdown ( data , from_markdown_opts ) ;
145- const date_match = ctx . file . match ( DATE_IN_FILENAME_REGEXP ) ;
146- if ( date_match ) {
147- ctx . tags [ 'date' ] = date_match [ 1 ] . replaceAll ( '-' , '' ) ;
148- }
151+ extractDateFromText ( ctx . file , ctx . tags , 'date' ) ;
149152 parseNode ( root_node , ctx , null ) ;
150153 } catch ( err ) {
151154 if ( ( err as any ) . code !== 'ENOENT' ) {
0 commit comments