add comments

This commit is contained in:
Joe Mulvaney 2023-07-08 18:54:14 +00:00
parent aa7d5d7146
commit 5f2bd64b02
1 changed files with 18 additions and 0 deletions

View File

@ -1,5 +1,11 @@
const { execSync } = require('node:child_process')
/** the export is a higher-order function that takes
* a gemini template as an arg. When the resulting template
* is run, it first runs the gemini tempate and then the rest
* of the code in this module, which transforms the gemini
* output to a gophermap
*/
module.exports = geminiTemplate => (...args) => {
const geminiText = geminiTemplate(...args)
const geminiLines = geminiText.split(/\r?\n/)
@ -10,6 +16,13 @@ module.exports = geminiTemplate => (...args) => {
else if (gemLine.trim().length) {
// handle first chars that do something weird in a gophermap
gemLine = gemLine.replace(/^#+/, '').replace(/^\*/, ' *')
// in gemini, paragraphs have no line-ending character returns, so
// that the client can soft wrap paragraph text as necessary. This
// is not the gopher way, so so I'm using fmt command to hard-wrap
// to 67 chars.
//
// This is done per line because `fmt` seems to mangle links
const paragraph = execSync('fmt -w 67', { input: gemLine }).toString()
const paragraphLines = paragraph.split('\n')
return paragraphLines
@ -23,6 +36,11 @@ module.exports = geminiTemplate => (...args) => {
const geminiLinkRegex = /^=>\s+(\S+)(?:\s+(.*))?$/
const fileExtensionRegex = /(.*)(?:\.([^\/.]+))$/
const urlHasProtocolRegex = /^\w+\:\/\/.*$/
// convert gemini link to gopher.
// gopher uses a special first character for different kinds of links
// and the line must have a tab character in it.
const gopherLink = (gemLine) => {
// line is format `=>\s+url\s+optional free text description
const [_, url, alt] = gemLine.match(geminiLinkRegex) || []