pompy.dev/source/generate/util.ts
developomp b43094fef9 2022-12-18 update
- increased text size a bit
- removed unused css from kbd tag
- removed unused white link css
- added space before all headers except h1
- moved type decclaration files to one directory
- replaced tag icon to hashtag
- redesigned post card
- removed unnecessary margin from post card titles
- removed post card content preview
- organized recursive parsing code
2021-12-18 16:54:51 +09:00

50 lines
1.3 KiB
TypeScript

import fs from "fs"
import { relative } from "path"
import { markdownPath } from "./config"
/**
* converts file path to url path that will be used in the url (starts with a slash)
*
* @param {string} pathToConvert
* @returns {string}
*/
export function path2URL(pathToConvert: string): string {
return `/${relative(markdownPath, pathToConvert)}`
.replace(/\.[^/.]+$/, "") // remove the file extension
.replace(/ /g, "-") // replace all space with a dash
}
// gets the text after the last slash
export function path2FileOrFolderName(inputPath: string): string {
// remove trailing slash
if (inputPath[-1] == "/")
inputPath = inputPath.slice(0, inputPath.length - 1)
// get the last section
return inputPath.slice(inputPath.lastIndexOf("/") + 1)
}
// gets the nth occurance of a pattern in string
// returns -1 if nothing is found
// https://stackoverflow.com/a/14482123/12979111
export function nthIndex(str: string, pat: string, n: number) {
let i = -1
while (n-- && i++ < str.length) {
i = str.indexOf(pat, i)
if (i < 0) break
}
return i
}
export function writeToJSON(JSONFilePath: string, dataToWrite: string) {
// create directory to put json content files
fs.mkdirSync(JSONFilePath.slice(0, JSONFilePath.lastIndexOf("/")), {
recursive: true,
})
// write content to json file
fs.writeFileSync(JSONFilePath, dataToWrite)
}