Browse Source

Write _links.json and _documents.json on save

single-block-api
Peter Stuifzand 2 years ago
parent
commit
dc32e5b5f8
  1. 49
      editor/src/fuse.js
  2. 56
      editor/src/search.js
  3. 96
      file.go
  4. 71
      main.go
  5. 1
      templates/graph.html

49
editor/src/fuse.js

@ -1,35 +1,36 @@ @@ -1,35 +1,36 @@
import Fuse from 'fuse.js'
import $ from 'jquery'
function createTitleSearch() {
return new Promise(function (resolve, reject) {
$.get('/links.json', function (documents) {
const options = {
keys: ['title'],
}
fetch('/links.json')
.then(result => result.json())
.then(documents => {
const options = {
keys: ['title'],
}
let commands = [
{title: 'Current Time', name: 'time'},
{title: 'Today', name: 'today'},
{title: 'Tomorrow', name: 'tomorrow'},
{title: 'Yesterday', name: 'yesterday'},
{title: 'TODO', name: 'todo'},
{title: 'DONE', name: 'done'},
{title: 'Page Reference', name: 'page_reference'},
{title: 'Code Block', name: 'code_block'},
];
let commands = [
{title: 'Current Time', name: 'time'},
{title: 'Today', name: 'today'},
{title: 'Tomorrow', name: 'tomorrow'},
{title: 'Yesterday', name: 'yesterday'},
{title: 'TODO', name: 'todo'},
{title: 'DONE', name: 'done'},
{title: 'Page Reference', name: 'page_reference'},
{title: 'Code Block', name: 'code_block'},
];
const fuseIndex = Fuse.createIndex(options.keys, documents)
let titleFuse = new Fuse(documents, options, fuseIndex)
let commandFuse = new Fuse(commands, {keys: ['title', 'name']})
const fuseIndex = Fuse.createIndex(options.keys, documents)
let titleFuse = new Fuse(documents, options, fuseIndex)
let commandFuse = new Fuse(commands, {keys: ['title', 'name']})
resolve({
documents,
titleSearch: titleFuse,
commandSearch: commandFuse,
commands: commands,
resolve({
documents,
titleSearch: titleFuse,
commandSearch: commandFuse,
commands: commands,
})
})
})
})
}

56
editor/src/search.js

@ -3,41 +3,43 @@ import $ from 'jquery'; @@ -3,41 +3,43 @@ import $ from 'jquery';
function search(element) {
return new Promise(function (resolve, reject) {
$.get('/documents.json', function (documents) {
let mappedDocuments = {}
$.each(documents, function (index, doc) {
mappedDocuments[doc.url] = doc
})
fetch('/documents.json')
.then(result => result.json())
.then(documents => {
let mappedDocuments = {}
$.each(documents, function (index, doc) {
mappedDocuments[doc.url] = doc
})
let idx = lunr(function () {
this.ref('url')
this.field('title')
this.field('body')
let idx = lunr(function () {
this.ref('url')
this.field('title')
this.field('body')
let lunridx = this
$.each(documents, function (index, doc) {
lunridx.add(doc)
let lunridx = this
$.each(documents, function (index, doc) {
lunridx.add(doc)
})
})
})
resolve({
element: element,
idx: idx,
search(query) {
let result = this.idx.search(query)
let actualResult = [];
resolve({
element: element,
idx: idx,
search(query) {
let result = this.idx.search(query)
let actualResult = [];
$.each(result, (key, value) => {
actualResult.push({
ref: value.ref,
title: mappedDocuments[value.ref].title,
$.each(result, (key, value) => {
actualResult.push({
ref: value.ref,
title: mappedDocuments[value.ref].title,
})
})
})
return actualResult
}
return actualResult
}
})
})
})
})
}

96
file.go

@ -17,6 +17,11 @@ import ( @@ -17,6 +17,11 @@ import (
"github.com/sergi/go-diff/diffmatchpatch"
)
const (
DocumentsFile = "_documents.json"
LinksFile = "_links.json"
)
type saveMessage struct {
p string
page Page
@ -31,7 +36,7 @@ type FilePages struct { @@ -31,7 +36,7 @@ type FilePages struct {
func NewFilePages(dirname string) PagesRepository {
fp := &FilePages{dirname, make(chan saveMessage)}
go func () {
go func() {
for msg := range fp.saveC {
fp.save(msg)
}
@ -111,10 +116,97 @@ func (fp *FilePages) save(msg saveMessage) error { @@ -111,10 +116,97 @@ func (fp *FilePages) save(msg saveMessage) error {
}
err = saveWithGit(fp, p, summary, author)
if err != nil {
return err
}
err = saveDocuments(fp)
err = saveLinks(fp)
return err
}
func saveLinks(fp *FilePages) error {
type Document struct {
Title string `json:"title"`
}
var results []Document
pages, err := mp.(*FilePages).AllPages()
if err != nil {
return err
}
for _, page := range pages {
results = append(results, Document{page.Title})
}
f, err := os.Create(LinksFile)
if err != nil {
return err
}
defer f.Close()
err = json.NewEncoder(f).Encode(&results)
if err != nil {
return err
}
return nil
}
func saveDocuments(fp *FilePages) error {
type Document struct {
Title string `json:"title"`
Body string `json:"body"`
URL string `json:"url"`
}
var results []Document
pages, err := mp.(*FilePages).AllPages()
if err != nil {
return err
}
for _, page := range pages {
content := strings.Builder{}
var listItems []struct {
Indented int
Text string
}
err = json.NewDecoder(strings.NewReader(page.Content)).Decode(&listItems)
if err == nil {
for _, item := range listItems {
content.WriteString(item.Text)
content.WriteByte(' ')
}
} else {
content.WriteString(page.Content)
content.WriteByte(' ')
}
for page, refs := range page.Refs {
content.WriteString(page)
content.WriteByte(' ')
for _, ref := range refs {
content.WriteString(ref.Line)
content.WriteByte(' ')
}
}
results = append(results, Document{
Title: page.Title,
Body: content.String(),
URL: page.Name,
})
}
f, err := os.Create(DocumentsFile)
if err != nil {
return err
}
defer f.Close()
err = json.NewEncoder(f).Encode(&results)
if err != nil {
return err
}
return nil
}
func saveWithGit(fp *FilePages, p string, summary, author string) error {
cmd := exec.Command("git", "add", ".")
cmd.Dir = fp.dirname

71
main.go

@ -821,79 +821,12 @@ func main() { @@ -821,79 +821,12 @@ func main() {
http.Handle("/auth/", &authHandler{})
http.HandleFunc("/links.json", func(w http.ResponseWriter, r *http.Request) {
type Document struct {
Title string `json:"title"`
}
var results []Document
pages, err := mp.(*FilePages).AllPages()
if err != nil {
http.Error(w, err.Error(), 500)
return
}
for _, page := range pages {
results = append(results, Document{page.Title})
}
w.Header().Set("Content-Type", "application/json")
err = json.NewEncoder(w).Encode(&results)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
http.ServeFile(w, r, LinksFile)
})
http.HandleFunc("/documents.json", func(w http.ResponseWriter, r *http.Request) {
type Document struct {
Title string `json:"title"`
Body string `json:"body"`
URL string `json:"url"`
}
var results []Document
pages, err := mp.(*FilePages).AllPages()
if err != nil {
http.Error(w, err.Error(), 500)
return
}
for _, page := range pages {
content := strings.Builder{}
var listItems []struct {
Indented int
Text string
}
err = json.NewDecoder(strings.NewReader(page.Content)).Decode(&listItems)
if err == nil {
for _, item := range listItems {
content.WriteString(item.Text)
content.WriteByte(' ')
}
} else {
content.WriteString(page.Content)
content.WriteByte(' ')
}
for page, refs := range page.Refs {
content.WriteString(page)
content.WriteByte(' ')
for _, ref := range refs {
content.WriteString(ref.Line)
content.WriteByte(' ')
}
}
results = append(results, Document{
Title: page.Title,
Body: content.String(),
URL: page.Name,
})
}
w.Header().Set("Content-Type", "application/json")
err = json.NewEncoder(w).Encode(&results)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
http.ServeFile(w, r, DocumentsFile)
})
http.HandleFunc("/fetchLink", func(w http.ResponseWriter, r *http.Request) {
link := r.URL.Query().Get("url")

1
templates/graph.html

@ -46,7 +46,6 @@ @@ -46,7 +46,6 @@
if (props.nodes.length) {
let nodeId = props.nodes[0]
let node = nodes.get(nodeId)
console.log(node)
window.location.href = '/edit/'+node.label
}
})

Loading…
Cancel
Save