Write _links.json and _documents.json on save
This commit is contained in:
parent
25209d508e
commit
dc32e5b5f8
|
@ -1,9 +1,10 @@
|
|||
import Fuse from 'fuse.js'
|
||||
import $ from 'jquery'
|
||||
|
||||
function createTitleSearch() {
|
||||
return new Promise(function (resolve, reject) {
|
||||
$.get('/links.json', function (documents) {
|
||||
fetch('/links.json')
|
||||
.then(result => result.json())
|
||||
.then(documents => {
|
||||
const options = {
|
||||
keys: ['title'],
|
||||
}
|
||||
|
|
|
@ -3,7 +3,9 @@ import $ from 'jquery';
|
|||
|
||||
function search(element) {
|
||||
return new Promise(function (resolve, reject) {
|
||||
$.get('/documents.json', function (documents) {
|
||||
fetch('/documents.json')
|
||||
.then(result => result.json())
|
||||
.then(documents => {
|
||||
let mappedDocuments = {}
|
||||
$.each(documents, function (index, doc) {
|
||||
mappedDocuments[doc.url] = doc
|
||||
|
|
96
file.go
96
file.go
|
@ -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 {
|
|||
|
||||
func NewFilePages(dirname string) PagesRepository {
|
||||
fp := &FilePages{dirname, make(chan saveMessage)}
|
||||
go func () {
|
||||
go func() {
|
||||
for msg := range fp.saveC {
|
||||
fp.save(msg)
|
||||
}
|
||||
|
@ -111,8 +116,95 @@ 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 {
|
||||
|
|
71
main.go
71
main.go
|
@ -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")
|
||||
|
|
|
@ -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…
Reference in New Issue
Block a user