@@ -115,7 +115,7 @@ export default {
115115 },
116116 getPageNotes (uri ) {
117117 let notes = logseq .settings ? .annotations ? .filter (x => x .uri === uri);
118- const title = notes[0 ]? .document .title ;
118+ const title = notes[0 ]? .document .title [ 0 ] ;
119119 const hids = new Set (notes .map (({id})=> id));
120120 let noteMap = new Map (notes .reduce ((acc , { id, text, tags, target, updated, references }) => {
121121 const exact = target[0 ]? .selector ? .filter (s => ' exact' in s)[0 ]? .exact ;
@@ -164,28 +164,59 @@ export default {
164164 this .updating = true ;
165165
166166 try {
167- const name = ' hypothesis__/' + uri .split (' //' )[1 ]
168- logseq .App .pushState (' page' , { name })
167+ let {title: hypothesisTitle, noteMap} = this .getPageNotes (uri)
168+ const logseqTitle = (await this .findPageName (uri)) || (await this .findPageNameV1 (uri))
169+
170+ // If page isn't found, create new one with hypothesisTitle. This approach allows for the title to be changed by the user
171+ const pageTitle = logseqTitle ? logseqTitle : ' hypothesis__/' + hypothesisTitle;
172+ logseq .App .pushState (' page' , { name: pageTitle })
169173 await delay (300 )
170174
171175 const page = await logseq .Editor .getCurrentPage ();
172- if (name !== page .originalName )
176+ if (pageTitle !== page .originalName )
173177 throw new Error (' page error' );
174178
175- await this .loadPageNotes (page, uri);
179+ await this .loadPageNotes (page, uri, hypothesisTitle, noteMap );
176180 } finally {
177181 this .updating = false ;
178182 }
179183 },
180- async loadPageNotes (page , uri ) {
184+ async findPageName (uri ) {
185+ const finds = (await logseq .DB .datascriptQuery (`
186+ [:find (pull ?b [*])
187+ :where
188+ [?b :block/properties ?p]
189+ [?b :block/name _]
190+ [(get ?p :hypothesis-uri) ?t]
191+ [(= "${ uri} " ?t)]]
192+ ` )).flat ()
193+
194+ if (finds .length > 1 ) {
195+ // TODO: throw error
196+ throw new Error (" Multiple pages has the same title" )
197+ } else if (finds == 0 ) {
198+ // throw new Error("Page doesn't exist")
199+ return
200+ } else return finds[0 ][" original-name" ]
201+ },
202+ async findPageNameV1 (uri ) {
203+ const name = ' hypothesis__/' + uri .replace (' .' , ' /' ).split (' //' )[1 ]
204+ const finds = (await logseq .DB .datascriptQuery (`
205+ [:find (pull ?b [*]) :where [?b :block/name "${ name} "]]` )).flat ()
206+ return finds .length ? finds[0 ][" original-name" ] : null
207+ },
208+ async loadPageNotes (page , uri , title , noteMap ) {
181209 if (! page || ! uri) return
182- let {title, noteMap} = this .getPageNotes (uri);
210+
211+ // hypothesis-uri is the prop by which the plugin identifies each page
212+ // hypothesis-naming-scheme is added for improved backwards compatability for later updates
213+ const pagePropBlockString = ` :PROPERTIES:\n :hypothesis-uri: ${ uri} \n :hypothesis-title: "${ title} "\n :hypothesis-naming-scheme: 0.2.0\n :END:` // for both org and markdown
183214
184215 let pageBlocksTree = await logseq .Editor .getCurrentPageBlocksTree ();
185- let targetBlock = pageBlocksTree[0 ];
186- if (! targetBlock ) {
187- targetBlock = await logseq .Editor .insertBlock (page .name , ` [:a {:href " ${ uri } " :target "_blank" :class "external-link"} [:span {:class "icon-hypothesis forbid-edit"}] " ${ title } "] ` , { isPageBlock : true });
188- pageBlocksTree = [targetBlock ];
216+ let pagePropBlock = pageBlocksTree[0 ];
217+ if (! pagePropBlock ) {
218+ pagePropBlock = await logseq .Editor .insertBlock (page .name , pagePropBlockString, { isPageBlock : true , properties : { preBlock : true } });
219+ pageBlocksTree = [pagePropBlock ];
189220 }
190221
191222 const blocks = pageBlocksTree .slice (1 );
@@ -201,21 +232,26 @@ export default {
201232 blockMap .set (hid, block);
202233 }
203234
204- await logseq .Editor .updateBlock (targetBlock .uuid , ` [:a {:href " ${ uri } " :target "_blank" :class "external-link"} [:span {:class "icon-hypothesis forbid-edit"}] " ${ title } "] ` );
235+ await logseq .Editor .updateBlock (pagePropBlock .uuid , pagePropBlockString );
205236 },
206237 async updatePage () {
207238 const page = await logseq .Editor .getCurrentPage ();
208- if (! page .name .startsWith (" hypothesis__/" ))
209- return ;
210- const pageBlocksTree = await logseq .Editor .getCurrentPageBlocksTree ();
211- if (pageBlocksTree .length < 1 )
212- return ;
213- const re = / {:href\s "(. *? )"/
214- const m = pageBlocksTree[0 ].content .match (re)
215- if (! m || ! m[1 ])
216- return ;
239+ let uri = page? .properties ? .hypothesisUri ;
240+ if (! uri) {
241+ if (! page .name .startsWith (" hypothesis__/" )) // handle naming scheme v0.1
242+ return ;
243+ const pageBlocksTree = await logseq .Editor .getCurrentPageBlocksTree ();
244+ if (pageBlocksTree .length < 1 )
245+ return ;
246+ const re = / {:href\s "(. *? )"/
247+ const m = pageBlocksTree[0 ].content .match (re)
248+ if (! m || ! m[1 ])
249+ return ;
250+ uri = m[1 ];
251+ }
217252 await this .fetchUpdates ();
218- await this .loadPageNotes (page, m[1 ])
253+ const {title , noteMap } = this .getPageNotes (uri)
254+ await this .loadPageNotes (page, uri, title, noteMap)
219255 }
220256 },
221257}
0 commit comments