Skip to content

Commit

Permalink
feat: options.target
Browse files Browse the repository at this point in the history
  • Loading branch information
Airkro committed Dec 1, 2023
1 parent e589360 commit ba02c4b
Show file tree
Hide file tree
Showing 11 changed files with 300 additions and 30 deletions.
22 changes: 18 additions & 4 deletions lib/ast.mjs
Original file line number Diff line number Diff line change
@@ -1,16 +1,30 @@
function attrString(attr) {
return attr
? Object.entries(attr)
.map(([name, value]) => `${name}="${value}"`)
.join(' ')
.map(([name, value]) => ` ${name}="${value}"`)
.join('')
: '';
}

export function create(target, { tag, attr, child }) {
if (target === 'mdx3') {
return {
type: 'mdxJsxFlowElement',
name: tag,
attributes: [
{
type: 'mdxJsxAttribute',
name: 'label',
value: 'label',
},
],
};
}

return {
type: target,
value: child
? [`<${tag} ${attrString(attr)}>`, child, `</${tag}>`].join('')
: `<${tag} ${attrString(attr)} />`,
? [`<${tag}${attrString(attr)}>`, child, `</${tag}>`].join('')
: `<${tag}${attrString(attr)} />`,
};
}
9 changes: 5 additions & 4 deletions lib/transform.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ const modes = {
);
},
'img-html-base64': ({ target, node, diagramType, data, alt }) => {
Object.assign(
node,
node.type = 'paragraph';
node.children = [
create(target, {
tag: 'img',
attr: {
Expand All @@ -49,13 +49,13 @@ const modes = {
src: toDataURL(data),
},
}),
);
];
},
'inline-svg': ({ target, node, diagramType, data, alt }) => {
Object.assign(
node,
create(target, {
tag: 'div',
tag: 'p',
attr: {
class: 'kroki-inline-svg',
'data-type': diagramType,
Expand Down Expand Up @@ -89,6 +89,7 @@ export async function transform({ node, server, headers, output, target }) {
delete node.lang;
delete node.value;
delete node.meta;
delete node.type;

modes[output]({ node, diagramType, data, alt, target });
}
2 changes: 1 addition & 1 deletion lib/validate.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import isPlainObject from 'is-plain-obj';

import { outputType } from './transform.mjs';

const targets = ['html', 'raw', 'mdx1', 'mdx2+'];
const targets = ['html', 'mdx3'];

export function validate({ server, headers, alias, output, target }) {
try {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
"garou": "^0.6.19",
"prettier": "^3.1.0",
"remark": "^15.0.1",
"remark-mdx": "^3.0.0",
"unist-util-remove-position": "^5.0.0"
},
"engines": {
Expand Down
Loading

0 comments on commit ba02c4b

Please sign in to comment.