mirror of
				https://github.com/zadam/trilium.git
				synced 2025-11-04 13:39:01 +01:00 
			
		
		
		
	basic implementation of "similar notes" widget
This commit is contained in:
		
							parent
							
								
									bf5a31dcdb
								
							
						
					
					
						commit
						7c60080772
					
				
							
								
								
									
										2
									
								
								db/migrations/0142__similar_notes_widget.sql
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										2
									
								
								db/migrations/0142__similar_notes_widget.sql
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,2 @@
 | 
			
		||||
INSERT INTO options (name, value, utcDateCreated, utcDateModified, isSynced)
 | 
			
		||||
VALUES ('similarNotesWidget', '{"enabled":false,"expanded":true,"position":60}', '2018-07-29T18:31:00.874Z', '2018-07-29T18:31:00.874Z', 0);
 | 
			
		||||
@ -51,7 +51,8 @@ export default class SidebarOptions {
 | 
			
		||||
            {name: 'linkMap', title: 'Link map'},
 | 
			
		||||
            {name: 'noteInfo', title: 'Note info'},
 | 
			
		||||
            {name: 'noteRevisions', title: 'Note revisions'},
 | 
			
		||||
            {name: 'whatLinksHere', title: 'What links here'}
 | 
			
		||||
            {name: 'whatLinksHere', title: 'What links here'},
 | 
			
		||||
            {name: 'similarNotes', title: 'Similar notes'}
 | 
			
		||||
        ].map(widget => {
 | 
			
		||||
            widget.option = this.parseJsonSafely(options[widget.name + 'Widget']) || {
 | 
			
		||||
                enabled: true,
 | 
			
		||||
 | 
			
		||||
@ -65,7 +65,8 @@ class Sidebar {
 | 
			
		||||
            import("../widgets/link_map.js"),
 | 
			
		||||
            import("../widgets/note_revisions.js"),
 | 
			
		||||
            import("../widgets/attributes.js"),
 | 
			
		||||
            import("../widgets/what_links_here.js")
 | 
			
		||||
            import("../widgets/what_links_here.js"),
 | 
			
		||||
            import("../widgets/similar_notes.js")
 | 
			
		||||
        ])).map(m => m.default);
 | 
			
		||||
 | 
			
		||||
        const options = await optionsService.waitForOptions();
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										36
									
								
								src/public/javascripts/widgets/similar_notes.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										36
									
								
								src/public/javascripts/widgets/similar_notes.js
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,36 @@
 | 
			
		||||
import StandardWidget from "./standard_widget.js";
 | 
			
		||||
import linkService from "../services/link.js";
 | 
			
		||||
import server from "../services/server.js";
 | 
			
		||||
import treeCache from "../services/tree_cache.js";
 | 
			
		||||
 | 
			
		||||
class SimilarNotesWidget extends StandardWidget {
 | 
			
		||||
    getWidgetTitle() { return "Similar notes"; }
 | 
			
		||||
 | 
			
		||||
    getMaxHeight() { return "200px"; }
 | 
			
		||||
 | 
			
		||||
    async doRenderBody() {
 | 
			
		||||
        const similarNoteIds = await server.get('similar_notes/' + this.ctx.note.noteId);
 | 
			
		||||
 | 
			
		||||
        console.log(similarNoteIds);
 | 
			
		||||
 | 
			
		||||
        if (similarNoteIds.length === 0) {
 | 
			
		||||
            this.$body.text("No similar notes found ...");
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        await treeCache.getNotes(similarNoteIds); // preload all at once
 | 
			
		||||
 | 
			
		||||
        const $list = $("<ul>");
 | 
			
		||||
 | 
			
		||||
        for (const similarNoteId of similarNoteIds) {
 | 
			
		||||
            const $item = $("<li>")
 | 
			
		||||
                .append(await linkService.createNoteLink(similarNoteId));
 | 
			
		||||
 | 
			
		||||
            $list.append($item);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        this.$body.empty().append($list);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export default SimilarNotesWidget;
 | 
			
		||||
@ -29,6 +29,7 @@ const ALLOWED_OPTIONS = [
 | 
			
		||||
    'linkMapWidget',
 | 
			
		||||
    'noteRevisionsWidget',
 | 
			
		||||
    'whatLinksHereWidget',
 | 
			
		||||
    'similarNotesWidget',
 | 
			
		||||
    'codeNotesMimeTypes'
 | 
			
		||||
];
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										24
									
								
								src/routes/api/similar_notes.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										24
									
								
								src/routes/api/similar_notes.js
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,24 @@
 | 
			
		||||
"use strict";
 | 
			
		||||
 | 
			
		||||
const noteCacheService = require('../../services/note_cache');
 | 
			
		||||
const repository = require('../../services/repository');
 | 
			
		||||
 | 
			
		||||
async function getSimilarNotes(req) {
 | 
			
		||||
    const noteId = req.params.noteId;
 | 
			
		||||
 | 
			
		||||
    const note = await repository.getNote(noteId);
 | 
			
		||||
 | 
			
		||||
    if (!note) {
 | 
			
		||||
        return [404, `Note ${noteId} not found.`];
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    const results = await noteCacheService.findNotes(note.title);
 | 
			
		||||
 | 
			
		||||
    return results
 | 
			
		||||
        .map(r => r.noteId)
 | 
			
		||||
        .filter(similarNoteId => similarNoteId !== noteId);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
module.exports = {
 | 
			
		||||
    getSimilarNotes
 | 
			
		||||
};
 | 
			
		||||
@ -34,6 +34,7 @@ const searchRoute = require('./api/search');
 | 
			
		||||
const dateNotesRoute = require('./api/date_notes');
 | 
			
		||||
const linkMapRoute = require('./api/link_map');
 | 
			
		||||
const clipperRoute = require('./api/clipper');
 | 
			
		||||
const similarNotesRoute = require('./api/similar_notes');
 | 
			
		||||
 | 
			
		||||
const log = require('../services/log');
 | 
			
		||||
const express = require('express');
 | 
			
		||||
@ -235,6 +236,8 @@ function register(app) {
 | 
			
		||||
    route(POST, '/api/clipper/notes', clipperMiddleware, clipperRoute.createNote, apiResultHandler);
 | 
			
		||||
    route(POST, '/api/clipper/open/:noteId', clipperMiddleware, clipperRoute.openNote, apiResultHandler);
 | 
			
		||||
 | 
			
		||||
    apiRoute(GET, '/api/similar_notes/:noteId', similarNotesRoute.getSimilarNotes);
 | 
			
		||||
 | 
			
		||||
    app.use('', router);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -4,7 +4,7 @@ const build = require('./build');
 | 
			
		||||
const packageJson = require('../../package');
 | 
			
		||||
const {TRILIUM_DATA_DIR} = require('./data_dir');
 | 
			
		||||
 | 
			
		||||
const APP_DB_VERSION = 141;
 | 
			
		||||
const APP_DB_VERSION = 142;
 | 
			
		||||
const SYNC_VERSION = 10;
 | 
			
		||||
const CLIPPER_PROTOCOL_VERSION = "1.0";
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user