uniorg-rehype
Advanced tools
Comparing version 0.1.21 to 0.1.23
@@ -26,2 +26,15 @@ "use strict"; | ||
}; | ||
/** | ||
* Similar to `hast` but respects `hProperties`. | ||
*/ | ||
function h(node, selector, properties, children) { | ||
// TODO: hProperties is not respected in text nodes. | ||
var _a; | ||
const element = hastscript_1.default(selector, properties || {}, children || []); | ||
const hProperties = (_a = node === null || node === void 0 ? void 0 : node.data) === null || _a === void 0 ? void 0 : _a.hProperties; | ||
if (hProperties) { | ||
element.properties = Object.assign({}, element.properties, hProperties); | ||
} | ||
return element; | ||
} | ||
function orgToHast(org, opts = {}) { | ||
@@ -37,3 +50,3 @@ const options = Object.assign(Object.assign({}, defaultOptions), opts); | ||
case 'org-data': | ||
return hastscript_1.default('div', toHast(org.children)); | ||
return h(org, 'div', {}, toHast(org.children)); | ||
case 'headline': { | ||
@@ -51,3 +64,3 @@ // TODO: support other options that prevent export: | ||
? [ | ||
hastscript_1.default('span', { className: ['todo-keyword', org.todoKeyword] }, org.todoKeyword), | ||
h(org, 'span', { className: ['todo-keyword', org.todoKeyword] }, org.todoKeyword), | ||
' ', | ||
@@ -58,3 +71,3 @@ ] | ||
? [ | ||
hastscript_1.default('span', { className: ['priority', `priority-${org.priority}`] }, `[${org.priority}]`), | ||
h(org, 'span', { className: ['priority', `priority-${org.priority}`] }, `[${org.priority}]`), | ||
' ', | ||
@@ -66,7 +79,7 @@ ] | ||
unist_builder_1.default('text', { value: '\xa0\xa0\xa0' }), | ||
hastscript_1.default('span.tags', intersperse(org.tags.map((x) => hastscript_1.default('span.tag', { className: `tag-${x}` }, x)), '\xa0')), | ||
h(org, 'span.tags', {}, intersperse(org.tags.map((x) => h(org, 'span.tag', { className: `tag-${x}` }, x)), '\xa0')), | ||
] | ||
: null; | ||
return [ | ||
hastscript_1.default(`h${org.level}`, [todo, priority, toHast(org.title), tags].filter((x) => x)), | ||
h(org, `h${org.level}`, {}, [todo, priority, toHast(org.title), tags].filter((x) => x)), | ||
...toHast(org.children), | ||
@@ -79,21 +92,24 @@ ]; | ||
if (org.listType === 'unordered') { | ||
return hastscript_1.default('ul', toHast(org.children)); | ||
return h(org, 'ul', {}, toHast(org.children)); | ||
} | ||
else if (org.listType === 'ordered') { | ||
return hastscript_1.default('ol', toHast(org.children)); | ||
return h(org, 'ol', {}, toHast(org.children)); | ||
} | ||
else { | ||
return hastscript_1.default('dl', toHast(org.children)); | ||
return h(org, 'dl', {}, toHast(org.children)); | ||
} | ||
case 'item': | ||
if (org.tag !== null) { | ||
return [hastscript_1.default('dt', org.tag), hastscript_1.default('dd', toHast(org.children))]; | ||
return [ | ||
h(org, 'dt', {}, org.tag), | ||
h(org, 'dd', toHast(org.children)), | ||
]; | ||
} | ||
else { | ||
return hastscript_1.default('li', toHast(org.children)); | ||
return h(org, 'li', {}, toHast(org.children)); | ||
} | ||
case 'quote-block': | ||
return hastscript_1.default('blockquote', toHast(org.children)); | ||
return h(org, 'blockquote', {}, toHast(org.children)); | ||
case 'src-block': | ||
return hastscript_1.default('pre.src-block', hastscript_1.default('code', { | ||
return h(org, 'pre.src-block', {}, h(org, 'code', { | ||
className: org.language ? `language-${org.language}` : undefined, | ||
@@ -107,9 +123,9 @@ }, removeCommonIndent(org.value))); | ||
// is correctly preserved. | ||
return hastscript_1.default('pre.verse', toHast(org.children)); | ||
return h(org, 'pre.verse', {}, toHast(org.children)); | ||
case 'center-block': | ||
return hastscript_1.default('div.center', toHast(org.children)); | ||
return h(org, 'div.center', {}, toHast(org.children)); | ||
case 'comment-block': | ||
return null; | ||
case 'example-block': | ||
return hastscript_1.default('div.exampe', org.value); | ||
return h(org, 'div.exampe', {}, org.value); | ||
case 'export-block': | ||
@@ -121,7 +137,7 @@ if (org.backend === 'html') { | ||
case 'special-block': | ||
return hastscript_1.default('div', { className: ['special-block', `block-${org.blockType}`] }, toHast(org.children)); | ||
return h(org, 'div', { className: ['special-block', `block-${org.blockType}`] }, toHast(org.children)); | ||
case 'keyword': | ||
return null; | ||
case 'horizontal-rule': | ||
return hastscript_1.default('hr'); | ||
return h(org, 'hr', {}); | ||
case 'diary-sexp': | ||
@@ -134,20 +150,20 @@ return null; | ||
case 'paragraph': | ||
return hastscript_1.default('p', toHast(org.children)); | ||
return h(org, 'p', {}, toHast(org.children)); | ||
case 'bold': | ||
return hastscript_1.default('strong', toHast(org.children)); | ||
return h(org, 'strong', {}, toHast(org.children)); | ||
case 'italic': | ||
return hastscript_1.default('em', toHast(org.children)); | ||
return h(org, 'em', {}, toHast(org.children)); | ||
case 'superscript': | ||
return hastscript_1.default('sup', toHast(org.children)); | ||
return h(org, 'sup', {}, toHast(org.children)); | ||
case 'subscript': | ||
return hastscript_1.default('sub', toHast(org.children)); | ||
return h(org, 'sub', {}, toHast(org.children)); | ||
case 'code': | ||
return hastscript_1.default('code.inline-code', org.value); | ||
return h(org, 'code.inline-code', {}, org.value); | ||
case 'verbatim': | ||
// org-mode renders verbatim as <code> | ||
return hastscript_1.default('code.inline-verbatim', org.value); | ||
return h(org, 'code.inline-verbatim', {}, org.value); | ||
case 'strike-through': | ||
return hastscript_1.default('del', toHast(org.children)); | ||
return h(org, 'del', {}, toHast(org.children)); | ||
case 'underline': | ||
return hastscript_1.default('span.underline', { style: 'text-decoration: underline;' }, toHast(org.children)); | ||
return h(org, 'span.underline', { style: 'text-decoration: underline;' }, toHast(org.children)); | ||
case 'text': | ||
@@ -166,8 +182,8 @@ return org.value; | ||
// TODO: set alt | ||
return hastscript_1.default('img', { src: link }); | ||
return h(org, 'img', { src: link }); | ||
} | ||
return hastscript_1.default('a', { href: link }, org.children.length ? toHast(org.children) : org.rawLink); | ||
return h(org, 'a', { href: link }, org.children.length ? toHast(org.children) : org.rawLink); | ||
} | ||
case 'timestamp': | ||
return hastscript_1.default('span.timestamp', org.rawValue); | ||
return h(org, 'span.timestamp', {}, org.rawValue); | ||
case 'planning': | ||
@@ -182,9 +198,9 @@ return null; | ||
case 'fixed-width': | ||
return hastscript_1.default('pre.fixed-width', org.value); | ||
return h(org, 'pre.fixed-width', {}, org.value); | ||
case 'clock': | ||
return null; | ||
case 'latex-environment': | ||
return hastscript_1.default('div.math.math-display', org.value); | ||
return h(org, 'div.math.math-display', {}, org.value); | ||
case 'latex-fragment': | ||
return hastscript_1.default('span.math.math-inline', org.contents.trim()); | ||
return h(org, 'span.math.math-inline', {}, org.contents.trim()); | ||
case 'entity': | ||
@@ -196,3 +212,3 @@ // rehype does not allow html escapes, so we use utf8 value instead. | ||
// see https://orgmode.org/manual/Column-Groups.html | ||
const table = hastscript_1.default('table', []); | ||
const table = h(org, 'table', {}, []); | ||
let hasHead = false; | ||
@@ -204,7 +220,7 @@ let group = []; | ||
if (!hasHead) { | ||
table.children.push(hastscript_1.default('thead', group.map((row) => hastscript_1.default('tr', row.children.map((cell) => hastscript_1.default('th', toHast(cell.children))))))); | ||
table.children.push(h(org, 'thead', {}, group.map((row) => h(row, 'tr', {}, row.children.map((cell) => h(cell, 'th', toHast(cell.children))))))); | ||
hasHead = true; | ||
} | ||
else { | ||
table.children.push(hastscript_1.default('tbody', toHast(group))); | ||
table.children.push(h(org, 'tbody', toHast(group))); | ||
} | ||
@@ -216,3 +232,3 @@ group = []; | ||
if (group.length) { | ||
table.children.push(hastscript_1.default('tbody', toHast(group))); | ||
table.children.push(h(org, 'tbody', toHast(group))); | ||
} | ||
@@ -223,3 +239,3 @@ return table; | ||
if (org.rowType === 'standard') { | ||
return hastscript_1.default('tr', toHast(org.children)); | ||
return h(org, 'tr', toHast(org.children)); | ||
} | ||
@@ -230,3 +246,3 @@ else { | ||
case 'table-cell': | ||
return hastscript_1.default('td', toHast(org.children)); | ||
return h(org, 'td', toHast(org.children)); | ||
default: | ||
@@ -233,0 +249,0 @@ return org; |
@@ -174,3 +174,21 @@ "use strict"; | ||
hastTest('entity', `\\Agrave`); | ||
test('respects hProperties', () => { | ||
const s = unified_1.default() | ||
.use(uniorg_parse_1.default) | ||
.use(() => (node) => { | ||
const headline = node.children[0]; | ||
headline.data = { hProperties: { id: 'my-custom-id' } }; | ||
}) | ||
.use(_1.default) | ||
.use(rehype_format_1.default) | ||
.use(rehype_stringify_1.default) | ||
.processSync(`* headline`) | ||
.toString(); | ||
expect(s).toMatchInlineSnapshot(` | ||
<div> | ||
<h1 id="my-custom-id">headline</h1> | ||
</div> | ||
`); | ||
}); | ||
}); | ||
//# sourceMappingURL=org-to-hast.spec.js.map |
{ | ||
"name": "uniorg-rehype", | ||
"version": "0.1.21", | ||
"version": "0.1.23", | ||
"description": "uniorg plugin to transform to rehype", | ||
@@ -55,3 +55,3 @@ "keywords": [ | ||
}, | ||
"gitHead": "1a09b2a510df2236298d3568f3eadc508d414212" | ||
"gitHead": "28736ac25da7e3a121002782c3cfec7366841a58" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
68783
461