Implemented privacy guides tasks

This commit is contained in:
Sun Knudsen 2022-02-10 16:21:06 -05:00
parent 3020080acd
commit 35ea7a7b16
No known key found for this signature in database
GPG Key ID: 02C43AD072D57783
7 changed files with 6865 additions and 0 deletions

2
.gitignore vendored Normal file
View File

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

40
.vscode/tasks.json vendored Normal file
View File

@ -0,0 +1,40 @@
{
"version": "2.0.0",
"tasks": [
{
"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": []
},
{
"type": "shell",
"label": "Organize steps",
"command": "node ./tasks/organize-steps.js \"${file}\"",
"presentation": {
"reveal": "silent"
},
"problemMatcher": []
}
],
"inputs": [
{
"id": "youtubeWatchUrl",
"description": "YouTube watch URL",
"default": "",
"type": "promptString"
}
]
}

6624
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

36
package.json Normal file
View File

@ -0,0 +1,36 @@
{
"name": "privacy-guides",
"description": "Privacy guides tasks",
"version": "1.0.0",
"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": {
"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/fs-extra": "^9.0.13",
"fs-extra": "^10.0.0",
"npm-check-updates": "^12.3.0",
"readdirp": "^3.6.0",
"youtube-player-screenshot": "^0.3.0"
},
"prettier": {
"endOfLine": "lf",
"printWidth": 80,
"semi": false,
"tabWidth": 2,
"trailingComma": "es5"
}
}

View File

@ -0,0 +1,57 @@
"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,
"--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)
}
})()

49
tasks/organize-steps.js Normal file
View File

@ -0,0 +1,49 @@
"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

@ -0,0 +1,57 @@
"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,
"--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)
}
})()