implement macros
This commit is contained in:
1 parent
a306820a78
commit
241f67fb4d
27 files changed
+597
-257
No files matched your search
@@ -0,0 +1,110 @@
|
||||
import { expect, test, suite, describe } from 'vitest'
|
||||
import { HopDown } from '../dist';
|
||||
import { Macro, TagCollection } from "../dist/tags";
|
||||
import dedent from "dedent";
|
||||
|
||||
var hopdown = new HopDown();
|
||||
|
||||
var jsonMacro = {
|
||||
name: 'json',
|
||||
template: dedent(`
|
||||
::: json
|
||||
: indent=2
|
||||
|
||||
{
|
||||
"Foo": "BAR",
|
||||
"one": 1
|
||||
}
|
||||
:::
|
||||
`),
|
||||
renderer: (macro) => {
|
||||
var src = macro.data.trim();
|
||||
return JSON.stringify(JSON.parse(src), null, parseInt(macro.params.indent));
|
||||
}
|
||||
};
|
||||
|
||||
var errorMacro = {
|
||||
name: 'error',
|
||||
template: dedent(`
|
||||
::: error
|
||||
: message=I AM ERROR
|
||||
|
||||
|
||||
:::
|
||||
`),
|
||||
renderer: (macro) => {
|
||||
return (
|
||||
'<h1>An Error Occurred</h1>'
|
||||
+ `<strong>${macro.params.message}</strong>`
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
hopdown.macros.register(jsonMacro);
|
||||
hopdown.macros.register(errorMacro);
|
||||
|
||||
|
||||
suite('Markdown <=> HTML Roundtrips', () => {
|
||||
|
||||
function assert(name, input, output, expected = undefined) {
|
||||
return test(name, () => {
|
||||
const html = hopdown.toHTML(input);
|
||||
const markdown = hopdown.toMarkdown(html);
|
||||
expect(html).toBe(output);
|
||||
expect(markdown).toBe(expected || input);
|
||||
});
|
||||
}
|
||||
|
||||
describe('JSON', () => {
|
||||
var html = (
|
||||
'<p><ribbit-macro name="json" data-indent="2" ' +
|
||||
'data-macro-source="eyJwYXJhbXMiOnsiaW5kZW50IjoiMiJ9LCJkYXRhIjoiXG57XG4gICAgXCJGb29cIjogXCJCQVJcIixcbiAgICBcIm9uZVwiOiAxXG59XG4ifQ==">'
|
||||
+ '\n'
|
||||
+ '{\n'
|
||||
+ ' "Foo": "BAR",\n'
|
||||
+ ' "one": 1\n'
|
||||
+ '}\n'
|
||||
+ '</ribbit-macro></p>'
|
||||
);
|
||||
assert('macro', jsonMacro.template, html);
|
||||
});
|
||||
|
||||
describe('Messages', () => {
|
||||
var html = (
|
||||
'<p><ribbit-macro name="error" data-message="I AM ERROR" '
|
||||
+ 'data-macro-source="eyJwYXJhbXMiOnsibWVzc2FnZSI6IkkgQU0gRVJST1IifSwiZGF0YSI6IlxuXG4ifQ==">'
|
||||
+ '\n'
|
||||
+ "<h1>An Error Occurred</h1><strong>I AM ERROR</strong>\n"
|
||||
+ "</ribbit-macro></p>"
|
||||
);
|
||||
assert('Error', errorMacro.template, html);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
suite('Markdown <=> Editor Node Roundtrips', () => {
|
||||
|
||||
function assertBlock(name, input, output) {
|
||||
return test(name, () => {
|
||||
const div = document.createElement('div');
|
||||
div.appendChild(hopdown.toEditorNode(input));
|
||||
expect(div.innerHTML).toStrictEqual(output);
|
||||
});
|
||||
}
|
||||
|
||||
describe('json', () => {
|
||||
var html = (
|
||||
'<ribbit-macro name="json" data-indent="2" '
|
||||
+ 'data-macro-source="eyJwYXJhbXMiOnsiaW5kZW50IjoiMiJ9LCJkYXRhIjoiXG57XG4gICAgXCJGb29cIjogXCJCQVJcIixcbiAgICBcIm9uZVwiOiAxXG59XG4ifQ==">'
|
||||
+ '\n'
|
||||
+ '{\n'
|
||||
+ ' "Foo": "BAR",\n'
|
||||
+ ' "one": 1\n'
|
||||
+ '}\n'
|
||||
+ '</ribbit-macro>'
|
||||
);
|
||||
assertBlock('Macro', jsonMacro.template, html);
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
Reference in new issue
Block a user