Write _links.json and _documents.json on save
This commit is contained in:
parent
25209d508e
commit
dc32e5b5f8
|
@ -1,35 +1,36 @@
|
||||||
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')
|
||||||
const options = {
|
.then(result => result.json())
|
||||||
keys: ['title'],
|
.then(documents => {
|
||||||
}
|
const options = {
|
||||||
|
keys: ['title'],
|
||||||
|
}
|
||||||
|
|
||||||
let commands = [
|
let commands = [
|
||||||
{title: 'Current Time', name: 'time'},
|
{title: 'Current Time', name: 'time'},
|
||||||
{title: 'Today', name: 'today'},
|
{title: 'Today', name: 'today'},
|
||||||
{title: 'Tomorrow', name: 'tomorrow'},
|
{title: 'Tomorrow', name: 'tomorrow'},
|
||||||
{title: 'Yesterday', name: 'yesterday'},
|
{title: 'Yesterday', name: 'yesterday'},
|
||||||
{title: 'TODO', name: 'todo'},
|
{title: 'TODO', name: 'todo'},
|
||||||
{title: 'DONE', name: 'done'},
|
{title: 'DONE', name: 'done'},
|
||||||
{title: 'Page Reference', name: 'page_reference'},
|
{title: 'Page Reference', name: 'page_reference'},
|
||||||
{title: 'Code Block', name: 'code_block'},
|
{title: 'Code Block', name: 'code_block'},
|
||||||
];
|
];
|
||||||
|
|
||||||
const fuseIndex = Fuse.createIndex(options.keys, documents)
|
const fuseIndex = Fuse.createIndex(options.keys, documents)
|
||||||
let titleFuse = new Fuse(documents, options, fuseIndex)
|
let titleFuse = new Fuse(documents, options, fuseIndex)
|
||||||
let commandFuse = new Fuse(commands, {keys: ['title', 'name']})
|
let commandFuse = new Fuse(commands, {keys: ['title', 'name']})
|
||||||
|
|
||||||
resolve({
|
resolve({
|
||||||
documents,
|
documents,
|
||||||
titleSearch: titleFuse,
|
titleSearch: titleFuse,
|
||||||
commandSearch: commandFuse,
|
commandSearch: commandFuse,
|
||||||
commands: commands,
|
commands: commands,
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,41 +3,43 @@ 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')
|
||||||
let mappedDocuments = {}
|
.then(result => result.json())
|
||||||
$.each(documents, function (index, doc) {
|
.then(documents => {
|
||||||
mappedDocuments[doc.url] = doc
|
let mappedDocuments = {}
|
||||||
})
|
|
||||||
|
|
||||||
let idx = lunr(function () {
|
|
||||||
this.ref('url')
|
|
||||||
this.field('title')
|
|
||||||
this.field('body')
|
|
||||||
|
|
||||||
let lunridx = this
|
|
||||||
$.each(documents, function (index, doc) {
|
$.each(documents, function (index, doc) {
|
||||||
lunridx.add(doc)
|
mappedDocuments[doc.url] = doc
|
||||||
|
})
|
||||||
|
|
||||||
|
let idx = lunr(function () {
|
||||||
|
this.ref('url')
|
||||||
|
this.field('title')
|
||||||
|
this.field('body')
|
||||||
|
|
||||||
|
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 = [];
|
||||||
|
|
||||||
|
$.each(result, (key, value) => {
|
||||||
|
actualResult.push({
|
||||||
|
ref: value.ref,
|
||||||
|
title: mappedDocuments[value.ref].title,
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
return 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,
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
return actualResult
|
|
||||||
}
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
96
file.go
96
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
|
||||||
|
@ -31,7 +36,7 @@ type FilePages struct {
|
||||||
|
|
||||||
func NewFilePages(dirname string) PagesRepository {
|
func NewFilePages(dirname string) PagesRepository {
|
||||||
fp := &FilePages{dirname, make(chan saveMessage)}
|
fp := &FilePages{dirname, make(chan saveMessage)}
|
||||||
go func () {
|
go func() {
|
||||||
for msg := range fp.saveC {
|
for msg := range fp.saveC {
|
||||||
fp.save(msg)
|
fp.save(msg)
|
||||||
}
|
}
|
||||||
|
@ -111,10 +116,97 @@ func (fp *FilePages) save(msg saveMessage) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
err = saveWithGit(fp, p, summary, author)
|
err = saveWithGit(fp, p, summary, author)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
err = saveDocuments(fp)
|
||||||
|
err = saveLinks(fp)
|
||||||
return err
|
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", ".")
|
||||||
cmd.Dir = fp.dirname
|
cmd.Dir = fp.dirname
|
||||||
|
|
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