0 directories, 46 files

util

Home / tinai / util
/**
 * Shared plumbing for the file-export dropdowns: filename sanitizing and the
 * format dispatch (pdf / html / html-mobile / markdown), so every exporting
 * surface only supplies its content builders and title.
 */

import { download_file } from './dom-utils.js';

/**
 * Strips filesystem-hostile characters from a title for use in filenames.
 * @param {string} name - The raw title.
 * @returns {string} The safe filename stem.
 */
export function sanitize_filename(name) {
	return String(name || '').replace(/[\\/:*?"<>|]/g, '').trim();
}

/**
 * Exports content in the selected format and downloads the file. html and
 * html-mobile share the html builder with a mobile flag; markdown and pdf
 * have their own builders. The title is sanitized for the filename.
 * @param {string} format - 'html', 'html-mobile', 'markdown', or 'pdf'.
 * @param {string} title - The resolved content title.
 * @param {Object} builders - { html: (mobile) => string|Promise<string>, markdown: () => string|Promise<string>, pdf: () => Promise<void> }.
 * @returns {Promise<void>}
 */
export async function export_content(format, title, builders) {
	if (format === 'pdf') {
		await builders.pdf();
		return;
	}
	const name = sanitize_filename(title) || 'document';
	if (format === 'html' || format === 'html-mobile') {
		download_file(name + (format === 'html-mobile' ? '.mobile' : '') + '.html', 'text/html', await builders.html(format === 'html-mobile'));
		return;
	}
	if (format === 'markdown') {
		download_file(name + '.md', 'text/markdown', await builders.markdown());
	}
}
🌐
export-utils.js ×
Type: Web, text/x-java
1.53 Kilobytes
Last Modified 2026-11-04 09:48:28
⬇ Download File