Browse Source

Problem: we don't have SR

Solution: add SR
master
Peter Stuifzand 2 months ago
parent
commit
b33cfeb395
  1. 2
      editor/src/editor.js
  2. 41
      editor/src/sr.js
  3. 4
      editor/src/wikilinks2.js
  4. 2
      file.go
  5. 17
      main.go
  6. 49
      search.go
  7. 1
      templates/view.html

2
editor/src/editor.js

@ -369,7 +369,7 @@ function Editor(holder, input) { @@ -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 => {

41
editor/src/sr.js

@ -5,18 +5,24 @@ import qs from "querystring"; @@ -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) { @@ -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) { @@ -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, .start-sr').on('click', function () {
isSR = $(this).hasClass('start-sr')
$('.start-review').on('click', function () {
search.startQuery('+tag:review +tag:sr/1', {internal: false}).then((results) => {
// 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 () { @@ -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 () { @@ -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))

4
editor/src/wikilinks2.js

@ -64,6 +64,10 @@ Plugin.prototype.parse = function (state, silent) { @@ -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 '<span class="metadata-key">'+key+'</span>: '+value;
}
if (link === 'TODO') {
return '<input type="checkbox" class="checkbox">';
} else if (link === 'DONE') {

2
file.go

@ -586,7 +586,7 @@ func saveLinksIncremental(dirname, title string) error { @@ -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})
}

17
main.go

@ -1130,16 +1130,24 @@ func main() { @@ -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() { @@ -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)

49
search.go

@ -29,7 +29,6 @@ import ( @@ -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) { @@ -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) { @@ -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,21 +295,24 @@ func createSearchObjects(rootBlockID string) ([]pageBlock, error) { @@ -293,21 +295,24 @@ 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,
}
queue = append(queue, blocks.Children[current]...)
}
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)
if rootBlockID == "Henk_Stuifzand" {
spew.Dump(pageBlocks)
queue = append(queue, blocks.Children[current]...)
}
return pageBlocks, nil

1
templates/view.html

@ -58,6 +58,7 @@ @@ -58,6 +58,7 @@
<a href="/graph/" class="navbar-item">Graph</a>
<a href="/{{ .TodayPage }}" class="navbar-item">Today</a>
<a href="" class="navbar-item start-review">Review</a>
<a href="" class="navbar-item start-sr">SR</a>
<a href="/auth/logout" class="navbar-item">Logout</a>
<span class="navbar-item"><b>{{ $.Session.Me }}</b></span>
{{ else }}

Loading…
Cancel
Save