@@ -3,14 +3,26 @@ import { MarkdownPostProcessor, MarkdownPostProcessorContext, MarkdownPreviewRen
33import { FileSystemAdapter , TFile , TFolder , normalizePath } from 'obsidian' ;
44import * as Yaml from 'yaml' ;
55import * as d3 from 'd3' ;
6+ import * as fs from 'fs' ;
7+ import * as path from 'path' ;
68
79type DataPoint = {
8- date : string ,
10+ date : Date ,
911 value : number
1012}
1113
1214class GraphInfo {
13- title : string
15+ title : string ;
16+ tagName : string ;
17+ dates : Date [ ] ;
18+ tagMeasures : number [ ] ;
19+
20+ constructor ( tagName : string ) {
21+ this . title = "" ;
22+ this . tagName = tagName ;
23+ this . dates = [ ] ;
24+ this . tagMeasures = [ ] ;
25+ }
1426}
1527
1628interface TagsStatSettings {
@@ -32,7 +44,11 @@ export default class TagsStat extends Plugin {
3244 console . log ( 'loading plugin' ) ;
3345
3446 TagsStat . app = this . app ;
35- TagsStat . plugin = this ;
47+
48+ if ( this . app . vault . adapter instanceof FileSystemAdapter ) {
49+ TagsStat . rootPath = this . app . vault . adapter . getBasePath ( ) ;
50+ // console.log(TagsStat.rootPath);
51+ }
3652
3753 await this . loadSettings ( ) ;
3854
@@ -102,21 +118,12 @@ export default class TagsStat extends Plugin {
102118 . attr ( "transform" ,
103119 "translate(" + margin . left + "," + margin . top + ")" ) ;
104120
105- let data : DataPoint [ ] = [
106- { date : "2021-02-18" , value : 10.35 } ,
107- { date : "2021-02-19" , value : 32.84 } ,
108- { date : "2021-02-20" , value : 45.92 } ,
109- { date : "2021-02-21" , value : 76.8 } ,
110- { date : "2021-02-22" , value : 83.47 } ,
111- { date : "2021-02-23" , value : 99.39 }
112- ] ;
121+ let data : DataPoint [ ] = [ ] ;
122+ let i ;
123+ for ( i = 0 ; i < graphInfo . dates . length ; i ++ ) {
124+ data . push ( { date : graphInfo . dates [ i ] , value : graphInfo . tagMeasures [ i ] } ) ;
125+ }
113126
114- let parseTime = d3 . timeParse ( "%Y-%m-%d" ) ;
115- let dates = [ ] ;
116- for ( let p of data ) {
117- dates . push ( parseTime ( p . date ) ) ;
118- }
119-
120127 // Add caption
121128 svg . append ( "text" )
122129 . text ( graphInfo . title )
@@ -125,7 +132,7 @@ export default class TagsStat extends Plugin {
125132 . style ( "stroke" , "white" ) ;
126133
127134 // Add X axis
128- let xDomain = d3 . extent ( dates ) ;
135+ let xDomain = d3 . extent ( graphInfo . dates ) ;
129136 let xScale = d3 . scaleTime ( )
130137 . domain ( xDomain )
131138 . range ( [ 0 , width ] ) ;
@@ -141,7 +148,7 @@ export default class TagsStat extends Plugin {
141148 . style ( "text-anchor" , "start" ) ;
142149
143150 // Add Y axis
144- let yMax = d3 . max ( data , function ( p ) { return + p . value ; } ) ;
151+ let yMax = d3 . max ( graphInfo . tagMeasures ) ;
145152 let yScale = d3 . scaleLinear ( )
146153 . domain ( [ 0 , yMax ] )
147154 . range ( [ height , 0 ] ) ;
@@ -150,7 +157,7 @@ export default class TagsStat extends Plugin {
150157
151158 // Add line
152159 let line = d3 . line ( )
153- . x ( function ( p ) { return xScale ( parseTime ( p . date ) ) ; } )
160+ . x ( function ( p ) { return xScale ( p . date ) ; } )
154161 . y ( function ( p ) { return yScale ( p . value ) ; } ) ;
155162
156163 svg . append ( "path" )
@@ -190,18 +197,20 @@ export default class TagsStat extends Plugin {
190197
191198 const yaml = Yaml . parse ( yamlBlock . textContent ) ;
192199 if ( ! yaml || ! yaml . tagName ) return ;
193- console . log ( yaml ) ;
200+ // console.log(yaml);
194201
195- //Prepare graph info
196- let graphInfo = new GraphInfo ( ) ;
197- if ( yaml . title && yaml . title !== "" ) {
202+ // Prepare graph info
203+ let graphInfo = new GraphInfo ( yaml . tagName ) ;
204+ graphInfo . tagName = yaml . tagName ;
205+ if ( yaml . title ) {
198206 graphInfo . title = yaml . title ;
199207 }
208+
200209 // Get files
201210 let files : TFile [ ] = [ ] ;
202211 if ( yaml . folder && yaml . folder !== "" ) {
203212 if ( yaml . folder === "" ) {
204- files = files . concat ( TagsStat . app . vault . getFiles ( ) ) ;
213+ files = files . concat ( TagsStat . app . vault . getMarkdownFiles ( ) ) ;
205214 }
206215 else {
207216 let folder = TagsStat . app . vault . getAbstractFileByPath ( normalizePath ( yaml . folder ) ) ;
@@ -214,7 +223,41 @@ export default class TagsStat extends Plugin {
214223 files = files . concat ( TagsStat . getFilesInFolder ( folder ) ) ;
215224 }
216225 }
217- console . log ( files ) ;
226+ // console.log(files);
227+
228+ // Get stats from files
229+ for ( let file of files ) {
230+ let fileBaseName = file . basename ;
231+ // console.log(fileBaseName);
232+ let fileDate = d3 . timeParse ( "%Y-%m-%d" ) ( fileBaseName ) ;
233+ graphInfo . dates . push ( fileDate ) ;
234+ // console.log(fileDate);
235+
236+ let filePath = path . join ( TagsStat . rootPath , file . path ) ;
237+ // console.log(filePath);
238+
239+ let content = fs . readFileSync ( filePath , { encoding : "utf-8" } ) ;
240+ // console.log(content);
241+ let strHashtagRegex = "(^|\\s)#" + yaml . tagName + "(:(?<number>[\\-]?[0-9]+[\\.][0-9]+|[\\-]?[0-9]+)(?<unit>\\w*)?)?(\\s|$)" ;
242+ let hashTagRegex = new RegExp ( strHashtagRegex , "gm" ) ;
243+ let match ;
244+ let tagMeasure = 0.0 ;
245+ while ( match = hashTagRegex . exec ( content ) ) {
246+ console . log ( match ) ;
247+ if ( match [ 0 ] . includes ( ":" ) ) {
248+ // console.log("valued-tag");
249+ let value = parseFloat ( match . groups . number ) ;
250+ // console.log(value);
251+ tagMeasure += value ;
252+ }
253+ else {
254+ // console.log("simple-tag");
255+ tagMeasure = tagMeasure + 1.0 ;
256+ }
257+ }
258+ graphInfo . tagMeasures . push ( tagMeasure ) ;
259+ }
260+ // console.log(graphInfo);
218261
219262 const destination = document . createElement ( 'div' ) ;
220263
0 commit comments