Add sidebar

This commit is contained in:
Peter Stuifzand 2020-08-05 10:03:54 +02:00
parent 1f105eff39
commit 556f462a2d
5 changed files with 67 additions and 29 deletions

View File

@ -191,25 +191,25 @@ function renderGraphs() {
}) })
} }
if (holder) { const MD = new MarkdownIt({
const MD = new MarkdownIt({ linkify: true,
linkify: true, highlight: function (str, lang) {
highlight: function (str, lang) { if (lang === 'mermaid') {
if (lang === 'mermaid') { return '<div class="mermaid">' + str + '</div>';
return '<div class="mermaid">' + str + '</div>';
}
return '';
} }
}) return '';
MD.use(MarkdownItWikilinks({ }
baseURL: holder.dataset.baseUrl, })
uriSuffix: '', MD.use(MarkdownItWikilinks({
relativeBaseURL: '/edit/', baseURL: document.querySelector('body').dataset.baseUrl,
htmlAttributes: { uriSuffix: '',
class: 'wiki-link' relativeBaseURL: '/edit/',
}, htmlAttributes: {
})).use(MarkdownItMark).use(MarkdownItKatex) class: 'wiki-link'
},
})).use(MarkdownItMark).use(MarkdownItKatex)
if (holder) {
const options = { const options = {
transform(text, element) { transform(text, element) {
let converted = text let converted = text
@ -489,3 +489,10 @@ search(searchInput).then(searcher => {
return true return true
}) })
}) })
document.querySelectorAll(".page-loader")
.forEach((el, key, parent) => {
fetch('/' + el.dataset.page + '?format=markdown').then(res => res.text()).then(text => {
el.innerHTML = MD.render(text)
})
})

View File

@ -207,3 +207,17 @@ mark {
.marker, .fold { .marker, .fold {
user-select: none; user-select: none;
} }
.grid {
display: grid;
grid-template-columns: 300px auto;
.sidebar {
padding: 0 12px;
width: 100%;
}
}
.section {
padding: 0;
}

25
main.go
View File

@ -151,6 +151,8 @@ type recentPage struct {
Recent []Change Recent []Change
} }
var baseTemplate = []string{"templates/layout.html", "templates/sidebar.html"}
type indexHandler struct{} type indexHandler struct{}
type graphHandler struct{} type graphHandler struct{}
type saveHandler struct { type saveHandler struct {
@ -177,7 +179,9 @@ func (*authHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet { if r.Method == http.MethodGet {
if r.URL.Path == "/auth/login" { if r.URL.Path == "/auth/login" {
t, err := template.ParseFiles("templates/layout.html", "templates/login.html") templates := baseTemplate
templates = append(templates, "templates/login.html")
t, err := template.ParseFiles(templates...)
if err != nil { if err != nil {
http.Error(w, err.Error(), 500) http.Error(w, err.Error(), 500)
return return
@ -215,7 +219,9 @@ func (*authHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, sess.NextURI, 302) http.Redirect(w, r, sess.NextURI, 302)
return return
} else if r.URL.Path == "/auth/logout" { } else if r.URL.Path == "/auth/logout" {
t, err := template.ParseFiles("templates/layout.html", "templates/logout.html") templates := baseTemplate
templates = append(templates, "templates/logout.html")
t, err := template.ParseFiles(templates...)
if err != nil { if err != nil {
http.Error(w, err.Error(), 500) http.Error(w, err.Error(), 500)
return return
@ -459,8 +465,9 @@ func (h *editHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
TodayPage: todayPage(), TodayPage: todayPage(),
ShowGraph: page != "Daily_Notes", ShowGraph: page != "Daily_Notes",
} }
templates := baseTemplate
t, err := template.ParseFiles("templates/layout.html", "templates/edit.html") templates = append(templates, "templates/edit.html")
t, err := template.ParseFiles(templates...)
if err != nil { if err != nil {
http.Error(w, err.Error(), 500) http.Error(w, err.Error(), 500)
return return
@ -540,8 +547,9 @@ func (h *graphHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
Nodes: template.JS(nodesBuf.String()), Nodes: template.JS(nodesBuf.String()),
Edges: template.JS(edgesBuf.String()), Edges: template.JS(edgesBuf.String()),
} }
templates := baseTemplate
t, err := template.ParseFiles("templates/layout.html", "templates/graph.html") templates = append(templates, "templates/graph.html")
t, err := template.ParseFiles(templates...)
if err != nil { if err != nil {
http.Error(w, err.Error(), 500) http.Error(w, err.Error(), 500)
return return
@ -670,8 +678,9 @@ func (h *indexHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ShowGraph: page != "Daily_Notes", ShowGraph: page != "Daily_Notes",
TodayPage: todayPage(), TodayPage: todayPage(),
} }
templates := baseTemplate
t, err := template.ParseFiles("templates/layout.html", "templates/view.html") templates = append(templates, "templates/view.html")
t, err := template.ParseFiles(templates...)
if err != nil { if err != nil {
http.Error(w, err.Error(), 500) http.Error(w, err.Error(), 500)
return return

View File

@ -225,7 +225,7 @@
} }
</style> </style>
</head> </head>
<body> <body data-base-url="{{ .BaseURL }}">
<div> <div>
<nav class="navbar" role="navigation" aria-label="main navigation"> <nav class="navbar" role="navigation" aria-label="main navigation">
<div class="navbar-brand"> <div class="navbar-brand">
@ -262,9 +262,14 @@
</div> </div>
</div> </div>
<section class="section"> <div class="grid">
{{ template "content" . }} {{ template "sidebar" . }}
</section>
<section class="section">
{{ template "content" . }}
</section>
</div>
<div class="h-app"> <div class="h-app">
<a href="/" class="u-url p-name">Wiki</a> <a href="/" class="u-url p-name">Wiki</a>

3
templates/sidebar.html Normal file
View File

@ -0,0 +1,3 @@
{{ define "sidebar" }}
<div class="sidebar page-loader content" data-page="Sidebar"></div>
{{ end }}