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 Fuse from 'fuse.js'
|
||||||
import $ from 'jquery'
|
|
||||||
|
|
||||||
function createTitleSearch() {
|
function createTitleSearch() {
|
||||||
return new Promise(function (resolve, reject) {
|
return new Promise(function (resolve, reject) {
|
||||||
$.get('/links.json', function (documents) {
|
fetch('/links.json')
|
||||||
|
.then(result => result.json())
|
||||||
|
.then(documents => {
|
||||||
const options = {
|
const options = {
|
||||||
keys: ['title'],
|
keys: ['title'],
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,7 +3,9 @@ import $ from 'jquery';
|
||||||
|
|
||||||
function search(element) {
|
function search(element) {
|
||||||
return new Promise(function (resolve, reject) {
|
return new Promise(function (resolve, reject) {
|
||||||
$.get('/documents.json', function (documents) {
|
fetch('/documents.json')
|
||||||
|
.then(result => result.json())
|
||||||
|
.then(documents => {
|
||||||
let mappedDocuments = {}
|
let mappedDocuments = {}
|
||||||
$.each(documents, function (index, doc) {
|
$.each(documents, function (index, doc) {
|
||||||
mappedDocuments[doc.url] = doc
|
mappedDocuments[doc.url] = doc
|
||||||
|
|
94
file.go
94
file.go
|
@ -17,6 +17,11 @@ import (
|
||||||
"github.com/sergi/go-diff/diffmatchpatch"
|
"github.com/sergi/go-diff/diffmatchpatch"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
DocumentsFile = "_documents.json"
|
||||||
|
LinksFile = "_links.json"
|
||||||
|
)
|
||||||
|
|
||||||
type saveMessage struct {
|
type saveMessage struct {
|
||||||
p string
|
p string
|
||||||
page Page
|
page Page
|
||||||
|
@ -111,9 +116,96 @@ func (fp *FilePages) save(msg saveMessage) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
err = saveWithGit(fp, p, summary, author)
|
err = saveWithGit(fp, p, summary, author)
|
||||||
|
if err != nil {
|
||||||
return err
|
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 {
|
func saveWithGit(fp *FilePages, p string, summary, author string) error {
|
||||||
cmd := exec.Command("git", "add", ".")
|
cmd := exec.Command("git", "add", ".")
|
||||||
|
|
71
main.go
71
main.go
|
@ -821,79 +821,12 @@ func main() {
|
||||||
|
|
||||||
http.Handle("/auth/", &authHandler{})
|
http.Handle("/auth/", &authHandler{})
|
||||||
http.HandleFunc("/links.json", func(w http.ResponseWriter, r *http.Request) {
|
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")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
err = json.NewEncoder(w).Encode(&results)
|
http.ServeFile(w, r, LinksFile)
|
||||||
if err != nil {
|
|
||||||
http.Error(w, err.Error(), 500)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
http.HandleFunc("/documents.json", func(w http.ResponseWriter, r *http.Request) {
|
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")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
err = json.NewEncoder(w).Encode(&results)
|
http.ServeFile(w, r, DocumentsFile)
|
||||||
if err != nil {
|
|
||||||
http.Error(w, err.Error(), 500)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
http.HandleFunc("/fetchLink", func(w http.ResponseWriter, r *http.Request) {
|
http.HandleFunc("/fetchLink", func(w http.ResponseWriter, r *http.Request) {
|
||||||
link := r.URL.Query().Get("url")
|
link := r.URL.Query().Get("url")
|
||||||
|
|
|
@ -46,7 +46,6 @@
|
||||||
if (props.nodes.length) {
|
if (props.nodes.length) {
|
||||||
let nodeId = props.nodes[0]
|
let nodeId = props.nodes[0]
|
||||||
let node = nodes.get(nodeId)
|
let node = nodes.get(nodeId)
|
||||||
console.log(node)
|
|
||||||
window.location.href = '/edit/'+node.label
|
window.location.href = '/edit/'+node.label
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in New Issue
Block a user