Skip to content

Instantly share code, notes, and snippets.

View jfjensen's full-sized avatar

Jes Fink-Jensen jfjensen

View GitHub Profile
@jfjensen
jfjensen / main.go
Created March 19, 2022 12:56
This is the golang code for rendering a markdown file to HTML/Bulma CSS
package main
import (
"bytes"
"github.com/gomarkdown/markdown"
"github.com/gomarkdown/markdown/ast"
"github.com/gomarkdown/markdown/html"
"io"
"io/ioutil"
"strconv"
@jfjensen
jfjensen / blog-post-example.md
Last active March 19, 2022 13:01
This is the markdown file that we'll be rendering

Lorem Picsum

Vulputate ut pharetra sit amet.

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Phasellus vestibulum lorem sed risus ultricies. Adipiscing vitae proin sagittis nisl rhoncus mattis rhoncus urna. Purus in massa tempor nec feugiat nisl pretium.

Enim lobortis scelerisque fermentum dui faucibus. Commodo viverra maecenas accumsan lacus. Ac ut consequat semper viverra nam libero justo laoreet sit. Magna etiam tempor orci eu lobortis elementum. Eget sit amet tellus cras adipiscing enim. Sagittis nisl rhoncus mattis rhoncus urna neque viverra justo nec.

Quis varius quam quisque id diam vel.

@jfjensen
jfjensen / func_main.go
Created March 19, 2022 21:35
This is the main function of the Markdown to HTML
func main() {
htmlHeader := `
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/css/bulma.min.css">
<title>Blog Post Example</title>
@jfjensen
jfjensen / func_renderHook.go
Created March 19, 2022 21:37
This is the renderHook function of the Markdown to HTML program
func renderHook(w io.Writer, node ast.Node, entering bool) (ast.WalkStatus, bool) {
if _, ok := node.(*ast.Heading); ok {
level := strconv.Itoa(node.(*ast.Heading).Level)
if entering && level == "1" {
w.Write([]byte(`<h1 class="title is-1 has-text-centered">`))
} else if entering {
w.Write([]byte("<h" + level + ">"))
} else {
@jfjensen
jfjensen / connectDB.go
Created April 7, 2022 09:48
This is a short program that connects to and disconnects from a MongoDB server
package main
import (
"context"
"fmt"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
func main() {
ctx := context.TODO()
opts := options.Client().ApplyURI("mongodb://localhost:27017")
@jfjensen
jfjensen / pingDB.go
Created April 7, 2022 09:54
Let's also ping the MongoDB server to make sure that we are actually connected to it.
package main
import (
"context"
"fmt"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/mongo/readpref"
)
func main() {
ctx := context.TODO()
@jfjensen
jfjensen / listDBs.go
Created April 7, 2022 09:57
This program will list all the databases present on the MongoDB server
package main
import (
"context"
"fmt"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
func main() {
ctx := context.TODO()
@jfjensen
jfjensen / createDBcollection.go
Created April 7, 2022 10:00
This program will create a database collection on a MongoDB server
package main
import (
"context"
"fmt"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
func main() {
ctx := context.TODO()
opts := options.Client().ApplyURI("mongodb://localhost:27017")
@jfjensen
jfjensen / mongoAdd.go
Last active April 7, 2022 15:32
Add documents to a MongoDB database collection
example := bson.D{
{"someString", "Example String"},
{"someInteger", 12},
{"someStringSlice", []string{"Example 1", "Example 2", "Example 3"}},
}
r, err := exampleCollection.InsertOne(ctx, example)
if err != nil {
panic(err)
}
fmt.Println(r.InsertedID)
@jfjensen
jfjensen / queryDB.go
Last active April 7, 2022 15:35
This piece of code will query a MongoDB database collection
c := exampleCollection.FindOne(ctx, bson.M{"_id": r.InsertedID})
var exampleResult bson.M
c.Decode(&exampleResult)
fmt.Printf("\nItem with ID: %v contains the following:\n", exampleResult["_id"])
fmt.Println("someString:", exampleResult["someString"])
fmt.Println("someInteger:", exampleResult["someInteger"])
fmt.Println("someStringSlice:", exampleResult["someStringSlice"])
filter := bson.D{{"someInteger", bson.D{{"$lt", 60}}}}