Skip to content

Instantly share code, notes, and snippets.

Avatar

Matthias Hagemann hagemann

View GitHub Profile
@hagemann
hagemann / blocksToHtml.js
Last active Oct 27, 2022
Render EditorJS blocks to HTML.
View blocksToHtml.js
const renderHtml = (blocks) => {
var html = blocks.map(block => {
switch (block.type) {
case 'code':
return `<pre><code>${ block.data.code }</pre></code>`
/**
* Original type is 'header' but I renamed the block to follow correct terminology:
* https://github.com/editor-js/header/issues/21
*/
@hagemann
hagemann / pdf.js
Created Jun 4, 2018
Example to generate PDF with external image
View pdf.js
const Printer = require('pdfmake')
const axios = require('axios')
const path = require('path')
module.exports.pdf = async (req, res, next) => {
var printer = new Printer({
Roboto: {
normal: path.resolve('src', 'fonts', 'Roboto.ttf'),
bold: path.resolve('src', 'fonts', 'Roboto-Bold.ttf'),
}
@hagemann
hagemann / database.js
Last active Dec 9, 2022
Promisified MySQL middleware for Node.js
View database.js
const util = require('util')
const mysql = require('mysql')
const pool = mysql.createPool({
connectionLimit: 10,
host: 'localhost',
user: 'root',
password: 'password',
database: 'my_database'
})
@hagemann
hagemann / countries.json
Created Jan 3, 2018
List of countries in JSON
View countries.json
{
"AF": "Afghanistan",
"AX": "Åland Islands",
"AL": "Albania",
"DZ": "Algeria",
"AS": "American Samoa",
"AD": "Andorra",
"AO": "Angola",
"AI": "Anguilla",
"AQ": "Antarctica",
@hagemann
hagemann / slugify.js
Last active Jan 30, 2023
Slugify makes a string URI-friendly
View slugify.js
function slugify(string) {
const a = 'àáâäæãåāăąçćčđďèéêëēėęěğǵḧîïíīįìıİłḿñńǹňôöòóœøōõőṕŕřßśšşșťțûüùúūǘůűųẃẍÿýžźż·/_,:;'
const b = 'aaaaaaaaaacccddeeeeeeeegghiiiiiiiilmnnnnoooooooooprrsssssttuuuuuuuuuwxyyzzz------'
const p = new RegExp(a.split('').join('|'), 'g')
return string.toString().toLowerCase()
.replace(/\s+/g, '-') // Replace spaces with -
.replace(p, c => b.charAt(a.indexOf(c))) // Replace special characters
.replace(/&/g, '-and-') // Replace & with 'and'
.replace(/[^\w\-]+/g, '') // Remove all non-word characters