Skip to content

Commit 324e71e

Browse files
committed
feat: add a basic function to find out the output file extension
1 parent 71b0b05 commit 324e71e

File tree

5 files changed

+58
-3
lines changed

5 files changed

+58
-3
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ Muh (say: Moo!) stands for "mustached hypertext" and is the template language th
66

77
Muh comes in 2 flavors. Both of them live in the `@sissijs/muh` package.
88

9-
`@sissijs/muh` includes everything. A full-fledged `processTemplateFile` function that supports SmolYAML/JSON frontmatter, layout files, basic preprocessing such as (a minimal) markdown, handling css imports and resolving `<html-include src="">` directives. In roughly 9KB of JavaScript
9+
`@sissijs/muh` includes everything. A full-fledged `processTemplateFile` function that supports SmolYAML/JSON frontmatter, layout files, basic preprocessing such as (a minimal) markdown, handling css imports and resolving `<html-include src="">` directives. In roughly 10KB of JavaScript.
1010

11-
`@sissijs/muh/template` is just the `template` function that implements the mustache syntax. It also provides the built-in filters for formatting dates/numbers, and built-in helper functions. The helper functions provided are small wrappers around fetch, namely `fetchText()` and `fetchJson()`. All that is contained in roughly 3KB of JavaScript.
11+
`@sissijs/muh/template` is just the `template` function that implements the mustache syntax. It also provides the built-in filters for formatting dates/numbers, and built-in helper functions. The helper functions provided are small wrappers around fetch, namely `fetchText()` and `fetchJson()`. All that is contained in roughly 4KB of JavaScript.
1212

1313
Sizes are measured with minification and without compression.
1414

src/get-output-file-extension.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
///<reference path="typedefs.js"/>
2+
import { htmlPreprocessor } from "./preprocessors/html.js";
3+
import { cssPreprocessor } from "./preprocessors/css.js";
4+
import { markdownPreprocessor } from "./preprocessors/markdown.js";
5+
6+
/**
7+
* Get the output file extension for an input file.
8+
* @param {string} inputFilePath
9+
* @param {ProcessTemplateFileConfig} [config] optional configuration object with preprocessors property
10+
*/
11+
export function getOutputFileExtension(inputFilePath, config) {
12+
const extensionIdx = inputFilePath ? inputFilePath.lastIndexOf('.') : -1;
13+
if (extensionIdx < 0) {
14+
return '';
15+
}
16+
const preprocessors = config?.preprocessors ?? [htmlPreprocessor, cssPreprocessor, markdownPreprocessor];
17+
const preprocessor = preprocessors.find(p => {
18+
if (typeof p.extension === 'string' &&
19+
inputFilePath.endsWith(p.extension)) {
20+
return true;
21+
}
22+
if (p.extension instanceof RegExp &&
23+
p.extension.test(inputFilePath)) {
24+
return true;
25+
}
26+
return false;
27+
});
28+
return (preprocessor?.outputExtension) ?
29+
preprocessor?.outputExtension : inputFilePath.slice(extensionIdx);
30+
}

src/muh.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
///<reference path="typedefs.js"/>
22
import { processTemplateFile } from "./process-template-file.js";
3+
import { getOutputFileExtension } from "./get-output-file-extension.js";
34
import { markdown, markdownPreprocessor } from "./preprocessors/markdown.js";
45
import { cssPreprocessor } from "./preprocessors/css.js";
56
import { htmlPreprocessor } from "./preprocessors/html.js";
@@ -14,4 +15,5 @@ export { smolYAML };
1415
export { frontmatter };
1516

1617
export { processTemplateFile };
18+
export { getOutputFileExtension };
1719
export { template };

src/preprocessors/html.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import { replaceAsync } from "../utils/replace-async.js";
22

33
export const htmlPreprocessor = {
44
name: 'html',
5-
extension: /.html?$/,
5+
extension: /(\.muh$)|(\.html?$)/,
6+
outputExtension: '.html',
67
/**
78
* Process HTML. Resolves `<html-include>` directives via an `include` function provided in data.
89
*
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { describe, it } from 'node:test';
2+
import assert from 'node:assert/strict';
3+
4+
import { getOutputFileExtension } from '../src/get-output-file-extension.js';
5+
6+
describe('getOutputFileExtension', () => {
7+
it('should find out that markdown files transform to .html by default', () => {
8+
assert.equal(getOutputFileExtension('readme.md'), '.html');
9+
});
10+
11+
it('should find out that .muh files transform to .html by default', () => {
12+
assert.equal(getOutputFileExtension('readme.muh'), '.html');
13+
});
14+
15+
it('should find out that html files stay .html', () => {
16+
assert.equal(getOutputFileExtension('readme.html'), '.html');
17+
});
18+
19+
it('should find out that css files stay .css', () => {
20+
assert.equal(getOutputFileExtension('styles.css'), '.css');
21+
});
22+
})

0 commit comments

Comments
 (0)