Skip to content

Instantly share code, notes, and snippets.

View webarthur's full-sized avatar
✌️

Arthur Ronconi webarthur

✌️
View GitHub Profile
@webarthur
webarthur / add-line-numbers-to-code.js
Created July 30, 2023 20:11
Adds line numbers to code blocks on the web page - version 2
/**
* Adds line numbers to code blocks on the page.
*/
function addLinesToCode () {
// Select all <pre><code> elements on the page
document.querySelectorAll('pre code')
.forEach(container => {
// Get code lines
const lines = (container.textContent.match(/\n/g)||[]).length + 1
@webarthur
webarthur / example.js
Last active July 21, 2023 14:26
Middleware for ExpressJS to validate the request data against a schema using Mongoose.
const express = require('express')
const Article = require('./article.model.js')
const validate = require('./validate.middleware.js')
const router = express.Router()
router.get('/articles',
validate('query', {
title: String,
text: String,
@webarthur
webarthur / template-literals-render.js
Created July 10, 2023 18:12
A simple template literals engine as a function for Node.js
const { readFileSync } = require('fs')
const { resolve } = require('path')
// Resolving the absolute path of the 'views' directory
const viewDir = resolve(`${ __dirname }/views/`)
/**
* Renders a view with the provided data.
* @param {string} viewName - The name of the view to render.
* @param {object} data - The data to pass to the view.
@webarthur
webarthur / sharp-resize.js
Created July 10, 2023 02:39
Sharp: Resize if image is bigger than a specific size (use withoutEnlargement prop)
const sharp = require('sharp')
const maxWidth = 1200
const maxHeight = 2000
await sharp('uploads/image.png')
.resize(maxWidth, maxHeight * 1.5, {
fit: 'inside',
withoutEnlargement: true, // this
})
@webarthur
webarthur / mongodb-query-dates.js
Last active July 8, 2023 15:39
7 methods to query between dates in MongoDB using the greater than ($gte) operator and the less than ($lt) operator
let docs
// METHOD 1 - new Date() +1 day
const startDate = new Date('2021-03-31 00:00')
const endDate = new Date(startDate.getTime() + 86400000)
docs = await collection.find({
date1: {
$gte: startDate,
$lt: endDate
}
@webarthur
webarthur / load-prism-highlight.js
Created July 7, 2023 03:14
Manually load Prism.js for syntax highlighting after the DOM has loaded. Force to highlight javascript syntax.
// Set the manual property of Prism object to true
window.Prism = { manual: true }
// Add an event listener for when the window is loaded
window.addEventListener('load', () => {
// Import the Prism.js library from the specified URL
import('https://cdnjs.cloudflare.com/ajax/libs/prism/9000.0.1/prism.min.js')
.then(() => {
console.log('Prism loaded.')
@webarthur
webarthur / add-numero-de-linhas-ao-codigo.js
Last active July 30, 2023 20:12
Adiciona números de linha aos blocos de código na página.
/**
* Adiciona números de linha aos blocos de código na página.
*/
function adicionarNumerosLinhasCodigo () {
// Seleciona todos os elementos <pre><code> na página
document.querySelectorAll('pre code')
.forEach(container => {
// Obter as linhas de código
const linhas = container.innerHTML.split('\n')
@webarthur
webarthur / add-line-numbers-to-code.js
Last active July 30, 2023 20:11
Adds line numbers to code blocks on the web page - version 1
/**
* Adds line numbers to code blocks on the page.
*/
function addLineNumbersToCode () {
// Select all <pre><code> elements on the page
document.querySelectorAll('pre code')
.forEach(container => {
// Get code lines
const lines = container.innerHTML.split('\n')
@webarthur
webarthur / .xbindkeysrc
Created November 29, 2022 16:54
xbindkeys configuration
###########################
# xbindkeys configuration #
###########################
#
# Version: 0.1.3
#
# If you edit this, do not forget to uncomment any lines that you change.
# The pound(#) symbol may be used anywhere for comments.
#
# A list of keys is in /usr/include/X11/keysym.h and in
@webarthur
webarthur / my-snippets.code-snippets
Created May 3, 2022 17:59
VS Code JavaScript snippets
{
"<router-link>": {
"scope": "html",
"prefix": ["rt", "rou", "rout"],
"body": [
"<router-link to=\"$1\">$2</router-link>"
],
},