svelte-markdoc-preprocess
Version:
A Svelte preprocessor that allows you to use Markdoc.
53 lines (52 loc) • 1.68 kB
JavaScript
import { existsSync, lstatSync, readFileSync, readdirSync, writeFileSync, } from 'fs';
import { dirname, join, relative, sep } from 'path';
import { sep as posix_sep } from 'path/posix';
export function get_all_files(path) {
const files = [];
for (const file of readdirSync(path)) {
const fullPath = path + posix_sep + file;
if (lstatSync(fullPath).isDirectory()) {
get_all_files(fullPath).forEach((x) => files.push(file + posix_sep + x));
}
else {
files.push(file);
}
}
return files;
}
export function read_file(...target) {
return readFileSync(join(...target), 'utf8');
}
export function write_to_file(file, content) {
writeFileSync(file, content);
}
export function path_exists(path) {
return existsSync(path);
}
export function relative_posix_path(from, to) {
return relative(dirname(from), to).split(sep).join(posix_sep);
}
export function is_external_url(url) {
return url.startsWith('http://') || url.startsWith('https://');
}
export function is_absolute_path(url) {
return url.startsWith('/');
}
export function is_relative_path(path) {
return !(is_absolute_path(path) || is_external_url(path));
}
export function parse_query_params_from_string(string) {
const index = string.indexOf('?');
if (index === -1) {
return new URLSearchParams();
}
string = string.slice(index);
return new URLSearchParams(string);
}
export function replace_query_params_from_string(string, params) {
const index = string.indexOf('?');
if (index !== -1) {
string = string.slice(0, index);
}
return string + '?' + params.toString();
}