From b33cfeb3957a7d3613b6f11117b3a9375ab219da Mon Sep 17 00:00:00 2001 From: Peter Stuifzand Date: Wed, 27 Apr 2022 20:43:45 +0200 Subject: [PATCH] Problem: we don't have SR Solution: add SR --- editor/src/editor.js | 2 +- editor/src/sr.js | 41 +++++++++++++++++++++++---------- editor/src/wikilinks2.js | 4 ++++ file.go | 2 +- main.go | 17 ++++++++++---- search.go | 49 ++++++++++++++++++++++------------------ templates/view.html | 1 + 7 files changed, 76 insertions(+), 40 deletions(-) diff --git a/editor/src/editor.js b/editor/src/editor.js index 4e56ff1..f97c3d8 100644 --- a/editor/src/editor.js +++ b/editor/src/editor.js @@ -369,7 +369,7 @@ function Editor(holder, input) { transformTable.call(this, editor, id, element); return } else if (converted.startsWith("```", 0) || converted.startsWith("$$", 0)) { - converted = MD.render(converted) + converted = MD.renderInline(converted) } else if (converted.startsWith("=", 0)) { transformMathExpression(converted, scope) .then(converted => { diff --git a/editor/src/sr.js b/editor/src/sr.js index 52cfce2..7cede12 100644 --- a/editor/src/sr.js +++ b/editor/src/sr.js @@ -5,18 +5,24 @@ import qs from "querystring"; function fillResult($modal, result) { $modal.data('block-id', result.id) $modal.data('block-original-text', result.line) + let visibleText = result.line.replace(/\s*#\S+/g, '').trim(); + $modal.data('block-text-without-tags', visibleText) $modal.find('.block-title').show().text(result.title) - $modal.find('.block-text').show().val(result.line) + $modal.find('.block-text').show().val(visibleText) } -function replaceBlock(id, oldText) { - return fetch('/api/block/replace', { +async function replaceBlock(id, oldText) { + if (oldText === false) return false; + + await fetch('/api/block/replace', { method: 'post', headers: { 'Content-Type': 'application/x-www-form-urlencoded', }, body: qs.encode({id, text: oldText}), }); + + return true } function appendBlock(id, newText) { @@ -30,13 +36,12 @@ function appendBlock(id, newText) { } function resetSRBox(text) { - let newText = text.replace(/#(?:\[\[)?sr\/(\d+)(?:\]\])?/, '#sr/1'); - return newText; + return text + " #review #sr/1" } function processSRBox(review, originalText) { let oldText = originalText - .replace(/#(?:\[\[)?sr\/(\d+)(?:\]\])?/, function (text, srCount) { + .replace(/#sr\/(\d+)/, function (text, srCount) { if (review === 'never') return ''; let nextCount = 1; const count = parseInt(srCount) @@ -45,15 +50,22 @@ function processSRBox(review, originalText) { if (review === 'later') nextCount = count + 1 return '#sr/' + nextCount; }).trim() - if (review === 'never') return oldText.replace(/#(?:\[\[)?review(?:\]\])?/, '') + if (review === 'never') return oldText.replace(/#review/, '') + if (oldText === originalText) return false return oldText } $(function () { let reviewBlocks = []; + let isSR = false; - $('.start-review').on('click', function () { - search.startQuery('+tag:review +tag:sr/1', {internal: false}).then((results) => { + $('.start-review, .start-sr').on('click', function () { + isSR = $(this).hasClass('start-sr') + + // Use different queries for different days + const query = isSR ? '+tag:sr tag:"sr/1" tag:"sr/2"' : '+tag:review tag:"sr/1"' + + search.startQuery(query, {internal: false}).then((results) => { reviewBlocks = results $('.review-modal .end-of-review').hide(); @@ -81,16 +93,21 @@ $(function () { window.location.reload() }); + $('.modal .show-answer').on('click', function () { + $('.review-modal .block-answer').show(); + }); + $('.modal .review').on('click', function () { const $modal = $(this).parents('.modal') const review = $(this).data('review') // NOTE: should we keep the review and sr/* tag in the editable text? const originalText = $modal.data('block-original-text') + const originalTextWithoutTags = $modal.data('block-text-without-tags') let text = $modal.find('.block-text').val() let id = $modal.data('block-id') - processText(review, text, originalText, id) + processText(review, text, originalTextWithoutTags, originalText, id) .then(() => { if (reviewBlocks.length > 0) { const first = reviewBlocks.shift() @@ -109,8 +126,8 @@ $(function () { }).catch(e => console.log(e)) }); - async function processText(review, text, originalText, id) { - if (text !== originalText) { + async function processText(review, text, originalTextWithoutTags, originalText, id) { + if (text !== originalTextWithoutTags) { await appendBlock(id, resetSRBox(text)) } await replaceBlock(id, processSRBox(review, originalText)) diff --git a/editor/src/wikilinks2.js b/editor/src/wikilinks2.js index 788fac3..5660616 100644 --- a/editor/src/wikilinks2.js +++ b/editor/src/wikilinks2.js @@ -64,6 +64,10 @@ Plugin.prototype.parse = function (state, silent) { Plugin.prototype.render = function (tokens, id, options, env) { let {match: link, tag} = tokens[id].meta if (tag) { + if (tag === 'metadata') { + let {key, value} = tokens[id].meta + return ''+key+': '+value; + } if (link === 'TODO') { return ''; } else if (link === 'DONE') { diff --git a/file.go b/file.go index 37bfb6c..491b410 100644 --- a/file.go +++ b/file.go @@ -586,7 +586,7 @@ func saveLinksIncremental(dirname, title string) error { titles[title] = true results = nil - for t, _ := range titles { + for t := range titles { results = append(results, Document{t}) } diff --git a/main.go b/main.go index 3a1fe3a..49ba2ee 100644 --- a/main.go +++ b/main.go @@ -1130,16 +1130,24 @@ func main() { http.Error(w, "missing id", 400) return } - repo := blockRepo{ - dirname: "data", - } + repo := blockRepo{dirname: "data"} block, err := repo.Load(id) block.Text = r.Form.Get("text") err = repo.Save(id, block) + // update search index searchObjects, err := createSearchObjects(id) + batch := searchIndex.NewBatch() for _, so := range searchObjects { - searchIndex.Index(so.ID, so) + err = batch.Index(so.ID, so) + if err != nil { + log.Println(err) + } } + err = searchIndex.Batch(batch) + if err != nil { + log.Println(err) + } + // TODO: update backlinks return })) http.HandleFunc("/api/block/append", wrapAuth(func(w http.ResponseWriter, r *http.Request) { @@ -1168,6 +1176,7 @@ func main() { block.Children = append(block.Children, generatedID) err = repo.Save(id, block) + // update search index searchObjects, err := createSearchObjects(id) spew.Dump("searchObjects", searchObjects) diff --git a/search.go b/search.go index 7b7705d..e66ae87 100644 --- a/search.go +++ b/search.go @@ -29,7 +29,6 @@ import ( "github.com/blevesearch/bleve/v2" "github.com/blevesearch/bleve/v2/mapping" - "github.com/davecgh/go-spew/spew" "github.com/iancoleman/strcase" ) @@ -214,7 +213,7 @@ func (s *searchHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { sr := bleve.NewSearchRequest(q) sr.IncludeLocations = false sr.Size = 25 - sr.Fields = []string{"page", "title", "text", "date"} + sr.Fields = []string{"page", "title", "text", "date", "parent"} sr.Highlight = bleve.NewHighlightWithStyle("html") sr.Highlight.AddField("text") results, err := s.searchIndex.Search(sr) @@ -230,13 +229,16 @@ func (s *searchHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { } type pageBlock struct { - ID string `json:"id"` - Title string `json:"title"` - Page string `json:"page"` - Text string `json:"text"` - Link []string `json:"link"` - Tag []string `json:"tag"` - Date []time.Time `json:"date"` + ID string `json:"id"` + Parent string `json:"parent"` + Title string `json:"title"` + Page string `json:"page"` + Text string `json:"text"` + Link []string `json:"link"` + Tag []string `json:"tag"` + Date []time.Time `json:"date"` + Key string `json:"key"` + Value string `json:"value"` } func (p pageBlock) Type() string { @@ -293,23 +295,26 @@ func createSearchObjects(rootBlockID string) ([]pageBlock, error) { dates = append(dates, pageDate) } - pageBlocks = append(pageBlocks, pageBlock{ - ID: current, - Title: blocks.Texts[blocks.PageID], - Page: blocks.PageID, - Text: blocks.Texts[current], - Link: linkNames, - Tag: tags, - Date: dates, - }) + block := pageBlock{ + ID: current, + Parent: blocks.ParentID, + Title: blocks.Texts[blocks.PageID], + Page: blocks.PageID, + Text: blocks.Texts[current], + Link: linkNames, + Tag: tags, + Date: dates, + } + + if kvpair := strings.SplitN(blocks.Texts[current], "::", 2); len(kvpair) == 2 { + block.Key = strings.TrimSpace(kvpair[0]) + block.Value = strings.TrimSpace(kvpair[1]) + } + pageBlocks = append(pageBlocks, block) queue = append(queue, blocks.Children[current]...) } - if rootBlockID == "Henk_Stuifzand" { - spew.Dump(pageBlocks) - } - return pageBlocks, nil } diff --git a/templates/view.html b/templates/view.html index abefd1d..f04a37c 100644 --- a/templates/view.html +++ b/templates/view.html @@ -58,6 +58,7 @@ Graph Today Review + SR Logout {{ $.Session.Me }} {{ else }}