Deprecated markdown preview

This commit is contained in:
Sun Knudsen 2023-08-26 14:26:13 -04:00
parent d6b7f836a0
commit e845aa90c3
No known key found for this signature in database
GPG Key ID: 02C43AD072D57783
13 changed files with 0 additions and 9007 deletions

View File

@ -1,8 +0,0 @@
root = true
[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 2

View File

@ -1,3 +0,0 @@
PORT=8080
REPO=
LOCALHOST_PROXY=

3
.gitignore vendored
View File

@ -1,3 +0,0 @@
.env
*.code-workspace
/node_modules

67
.vscode/tasks.json vendored
View File

@ -1,67 +0,0 @@
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "Organize steps",
"command": "node ./tasks/organize-steps.js '${file}'",
"presentation": {
"reveal": "silent"
},
"problemMatcher": []
},
{
"type": "shell",
"label": "Preview markdown",
"command": "node ./tasks/preview-markdown.js '${relativeFile}'",
"presentation": {
"reveal": "silent"
},
"problemMatcher": []
},
{
"type": "shell",
"label": "Preview markdown (using reverse proxy)",
"command": "node ./tasks/preview-markdown.js '${relativeFile}' use-proxy",
"presentation": {
"reveal": "silent"
},
"problemMatcher": []
},
{
"type": "shell",
"label": "Proxify link",
"command": "node ./tasks/proxify-link.js '${selectedText}'",
"presentation": {
"reveal": "silent"
},
"problemMatcher": []
},
{
"type": "shell",
"label": "Insert YouTube player",
"command": "node ./tasks/insert-youtube-player.js '${file}' '${lineNumber}' '${input:youtubeWatchUrl}'",
"presentation": {
"reveal": "silent"
},
"problemMatcher": []
},
{
"type": "shell",
"label": "Update YouTube players",
"command": "node ./tasks/update-youtube-players.js",
"presentation": {
"reveal": "always"
},
"problemMatcher": []
}
],
"inputs": [
{
"id": "youtubeWatchUrl",
"description": "YouTube watch URL",
"default": "",
"type": "promptString"
}
]
}

26
gfm.hbs
View File

@ -1,26 +0,0 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="/github-markdown.css">
<style>
.markdown-body {
box-sizing: border-box;
min-width: 200px;
max-width: 980px;
margin: 0 auto;
padding: 45px;
}
@media (max-width: 767px) {
.markdown-body {
padding: 15px;
}
}
</style>
</head>
<body>
<article class="markdown-body">
{{{markdown}}}
</article>
</body>

View File

@ -1,72 +0,0 @@
"use strict"
import "dotenv/config"
import express from "express"
import got from "got"
import fsExtra from "fs-extra"
import prettier from "prettier"
import handlebars from "handlebars"
const { pathExists, readFile } = fsExtra
const gfm = handlebars.compile(await readFile("./gfm.hbs", "utf8"))
const app = express()
app.get("/github-markdown.css", async (req, res, next) => {
try {
const path = "./node_modules/github-markdown-css/github-markdown.css"
const exists = await pathExists(path)
if (exists === false) {
return next()
}
const css = await readFile(path, "utf8")
res.setHeader("Content-Type", "text/css")
return res.send(css)
} catch (error) {
console.log(error)
return res.sendStatus(500)
}
})
app.get("*.md", async (req, res, next) => {
try {
const path = `.${req.url}`
const exists = await pathExists(path)
if (exists === false) {
return next()
}
const markdown = await readFile(path, "utf8")
const response = await got.post("https://api.github.com/markdown", {
json: {
mode: "gfm",
text: markdown,
},
})
res.setHeader("Content-Type", "text/html")
return res.send(
prettier.format(
gfm({
markdown: response.body,
}),
{
parser: "html",
}
)
)
} catch (error) {
console.log(error)
return res.sendStatus(500)
}
})
app.use(
express.static(".", {
dotfiles: "ignore",
index: false,
})
)
const server = app.listen(process.env.PORT ?? 8080, () => {
console.info(`Server listening on port ${server.address().port}`)
})

8567
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,46 +0,0 @@
{
"name": "privacy-guides",
"description": "Privacy guides tasks",
"version": "1.0.1",
"repository": {
"type": "git",
"url": "git+https://github.com/sunknudsen/privacy-guides.git"
},
"type": "module",
"engines": {
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
},
"scripts": {
"serve": "node index.js",
"ncu": "ncu --target minor --upgrade"
},
"author": "Sun Knudsen <hello@sunknudsen.com>",
"license": "MIT",
"bugs": {
"url": "https://github.com/sunknudsen/privacy-guides/issues"
},
"homepage": "https://github.com/sunknudsen/privacy-guides#readme",
"devDependencies": {
"@types/express": "^4.17.13",
"@types/fs-extra": "^9.0.13",
"clipboardy": "^3.0.0",
"dotenv": "^16.0.1",
"express": "^4.18.1",
"fs-extra": "^10.1.0",
"github-markdown-css": "^5.1.0",
"got": "^12.3.0",
"handlebars": "^4.7.7",
"npm-check-updates": "^12.5.12",
"open": "^8.4.0",
"prettier": "^2.7.1",
"readdirp": "^3.6.0",
"youtube-player-screenshot": "^0.3.0"
},
"prettier": {
"endOfLine": "lf",
"printWidth": 80,
"semi": false,
"tabWidth": 2,
"trailingComma": "es5"
}
}

View File

@ -1,61 +0,0 @@
"use strict"
import fsExtra from "fs-extra"
import { parse } from "path"
import execa from "execa"
const { readFile, writeFile } = fsExtra
if (
process.argv.length !== 5 ||
!process.argv[2].match(/\.md$/) ||
!process.argv[3].match(/^[0-9]+$/) ||
!process.argv[4].match(
/^https:\/\/www\.youtube\.com\/watch\?v=([\w-]+)(&t=(\d+))?$/
)
) {
console.info(
"Usage: node insert-youtube-player.js file lineNumber youtubeWatchUrl"
)
process.exit(1)
}
;(async () => {
try {
const file = process.argv[2]
const lineNumber = process.argv[3]
const youtubeWatchUrl = process.argv[4]
const content = await readFile(file, "utf8")
const lines = content.split("\n")
const { stdout } = await execa("node", [
"node_modules/youtube-player-screenshot/bin/youtube-player-screenshot.js",
"--url",
youtubeWatchUrl,
"--width",
1360,
"--height",
764,
"--type",
"jpeg",
"--output",
parse(file).dir,
"--privacy",
"--stdout",
])
let updatedContent = ""
lines.forEach(function (line, index) {
let lineBreak = "\n"
if (index === lines.length - 1) {
lineBreak = ""
}
if (index === parseInt(lineNumber) - 1) {
updatedContent += `${stdout}${lineBreak}`
} else {
updatedContent += `${line}${lineBreak}`
}
})
await writeFile(file, updatedContent)
} catch (error) {
console.error(error.message)
}
})()

View File

@ -1,49 +0,0 @@
"use strict"
import fsExtra from "fs-extra"
const { readFile, writeFile } = fsExtra
const headingRegExp = /^## /
const stepRegExp = /^### Step [1-9][0-9]*(.*?)?:/
if (process.argv.length !== 3 || !process.argv[2].match(/\.md$/)) {
console.info("Usage: node organize-steps.js file")
process.exit(1)
}
;(async () => {
try {
const file = process.argv[2]
const content = await readFile(file, "utf8")
const lines = content.split("\n")
let organizedContent = ""
let step = 1
lines.forEach(function (line, index) {
let lineBreak = "\n"
if (index === lines.length - 1) {
lineBreak = ""
}
if (line.match(headingRegExp)) {
step = 1
}
let match
if ((match = line.match(stepRegExp))) {
let disclaimer = ""
if (match[1]) {
disclaimer = match[1]
}
organizedContent += `${line.replace(
stepRegExp,
`### Step ${step}${disclaimer}:`
)}${lineBreak}`
step++
} else {
organizedContent += `${line}${lineBreak}`
}
})
await writeFile(file, organizedContent)
} catch (error) {
console.error(error)
}
})()

View File

@ -1,18 +0,0 @@
"use strict"
import "dotenv/config"
import open from "open"
if (process.argv.length < 3 || !process.argv[2].match(/\.md$/)) {
console.info("Usage: node open-preview.js file")
process.exit(1)
}
const file = process.argv[2]
const options = process.argv[3]
if (options === "use-proxy") {
open(`${process.env.LOCALHOST_PROXY}/${file}`)
} else {
open(`http://localhost:${process.env.PORT ?? 8080}/${file}`)
}

View File

@ -1,26 +0,0 @@
"use strict"
import "dotenv/config"
import clipboard from "clipboardy"
if (process.argv.length !== 3 || !process.argv[2].match(/http(s)?:\/\//)) {
console.info("Usage: node copy-link.js selectedText")
process.exit(1)
}
if (
process.env.REPO === undefined ||
process.env.LOCALHOST_PROXY === undefined
) {
console.info("Missing environment variables")
process.exit(1)
}
const selectedText = process.argv[2]
clipboard.write(
selectedText.replace(
`https://raw.githubusercontent.com/${process.env.REPO}/master`,
process.env.LOCALHOST_PROXY
)
)

View File

@ -1,61 +0,0 @@
"use strict"
import readdirp from "readdirp"
import fsExtra from "fs-extra"
import { parse, join } from "path"
import execa from "execa"
const { readFile, writeFile, remove } = fsExtra
const playerRegExp = /\[\!\[.*?\]\((.*?\.png)\)\]\((.*?) ".*?"\)/
;(async () => {
try {
console.info("Updating YouTube players…")
const options = {
fileFilter: "*.md",
directoryFilter: "!node_modules",
}
for await (const file of readdirp(process.cwd(), options)) {
const content = await readFile(file.fullPath, "utf8")
const lines = content.split("\n")
let updatedContent = ""
for (const [index, line] of lines.entries()) {
let lineBreak = "\n"
if (index === lines.length - 1) {
lineBreak = ""
}
let match
if ((match = line.match(playerRegExp))) {
const dir = parse(file.fullPath).dir
const imagePath = match[1]
const youtubeWatchUrl = match[2]
console.info(`Processing ${file.path}`)
await remove(join(dir, imagePath))
const { stdout } = await execa("node", [
"node_modules/youtube-player-screenshot/bin/youtube-player-screenshot.js",
"--url",
youtubeWatchUrl,
"--width",
1360,
"--height",
764,
"--type",
"jpeg",
"--output",
dir,
"--privacy",
"--stdout",
])
updatedContent += `${stdout}${lineBreak}`
} else {
updatedContent += `${line}${lineBreak}`
}
}
await writeFile(file.fullPath, updatedContent)
}
console.info("Done")
} catch (error) {
console.error(error.message)
}
})()