From 348d5487939491a5d2a936728a23e1aa2feb2d75 Mon Sep 17 00:00:00 2001 From: Marcel van der Boom Date: Tue, 20 Nov 2018 11:29:12 +0100 Subject: [PATCH 01/13] Add code highlighting option This is a first stab at having a configurable code highlighting option, similar to the MathJax rendering option. This change makes a checkbox in the settings for code highlighting using the highlightjs.org library. What works: code highlighting in multi-user env is like I would expect. single and anon(?) needs work Things to resolve/consider: - does the .IsCode test for code highlighting need to stay? At least this and that should use the same version of the highlight.js lib. - can the common templating part be 'included' somehow? - the anon vs single-user vs multi-user code is not completely clear (to me) - bring js to local instead of cloudfare cdn (perhaps combine with MathJax) --- collections.go | 5 +++++ database.go | 15 +++++++++++++++ templates/collection-post.tmpl | 13 +++++++++++++ templates/collection-tags.tmpl | 13 +++++++++++++ templates/collection.tmpl | 14 +++++++++++++- templates/post.tmpl | 16 ++++++++++++++++ templates/user/collection.tmpl | 5 +++++ 7 files changed, 80 insertions(+), 1 deletion(-) diff --git a/collections.go b/collections.go index ea5b929..da803e1 100644 --- a/collections.go +++ b/collections.go @@ -70,6 +70,7 @@ type ( Privacy int `schema:"privacy" json:"privacy"` Pass string `schema:"password" json:"password"` MathJax bool `schema:"mathjax" json:"mathjax"` + Highlight bool `schema:"highlight" json:"highlight"` Handle string `schema:"handle" json:"handle"` // Actual collection values updated in the DB @@ -306,6 +307,10 @@ func (c *Collection) RenderMathJax() bool { return c.db.CollectionHasAttribute(c.ID, "render_mathjax") } +func (c *Collection) CodeHighlight() bool { + return c.db.CollectionHasAttribute(c.ID, "code_highlight") +} + func newCollection(app *app, w http.ResponseWriter, r *http.Request) error { reqJSON := IsJSON(r.Header.Get("Content-Type")) alias := r.FormValue("alias") diff --git a/database.go b/database.go index 0c79e10..e9f052e 100644 --- a/database.go +++ b/database.go @@ -800,6 +800,21 @@ func (db *datastore) UpdateCollection(c *SubmittedCollection, alias string) erro } } + // Update CodeHighlight value + if c.Highlight { + _, err = db.Exec("INSERT INTO collectionattributes (collection_id, attribute, value) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE value = ?", collID, "code_highlight", "1", "1") + if err != nil { + log.Error("Unable to insert code_highlight value: %v", err) + return err + } + } else { + _, err = db.Exec("DELETE FROM collectionattributes WHERE collection_id = ? AND attribute = ?", collID, "code_highlight") + if err != nil { + log.Error("Unable to delete code_highlight value: %v", err) + return err + } + } + // Update rest of the collection data res, err = db.Exec("UPDATE collections SET "+q.Updates+" WHERE "+q.Conditions, q.Params...) if err != nil { diff --git a/templates/collection-post.tmpl b/templates/collection-post.tmpl index 6f34863..b3e04cc 100644 --- a/templates/collection-post.tmpl +++ b/templates/collection-post.tmpl @@ -45,6 +45,19 @@ {{end}} + + {{if .Collection.CodeHighlight}} + + + + {{end}} + diff --git a/templates/collection-tags.tmpl b/templates/collection-tags.tmpl index 021febc..4c2a126 100644 --- a/templates/collection-tags.tmpl +++ b/templates/collection-tags.tmpl @@ -45,6 +45,19 @@ {{end}} + {{if .CodeHighlight}} + + + + {{end}} + + diff --git a/templates/collection.tmpl b/templates/collection.tmpl index ef2401d..a3fe8f6 100644 --- a/templates/collection.tmpl +++ b/templates/collection.tmpl @@ -43,6 +43,18 @@ {{end}} + + {{if .CodeHighlight}} + + + + {{end}} {{if or .IsOwner .SingleUser}}