Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const filesToCopy = new Map()
const highlight = pluginOptions.noHighlight !== true
const handlers = { link: handleLink }
// offset the levels
const firstHeadline = select('headline', body)
const offset = firstHeadline ? firstHeadline.level - 1 : 0
if (offset > 0) {
body = map(body, node => {
if (node.type !== `headline`) return node
return { ...node, level: node.level - offset }
})
}
const hast = toHAST(body, { highlight, handlers })
const html = hastToHTML(hast, { allowDangerousHTML: true })
await Promise.all(Array.from(filesToCopy, async ([linkPath, newFilePath]) => {
// Don't copy anything is the file already exists at the location.
if (!fsExtra.existsSync(newFilePath)) {
try {
await fsExtra.ensureDir(dirname(newFilePath))
await fsExtra.copy(linkPath, newFilePath)
} catch (err) {
console.error(`error copying file`, err)
}
}
}))
return html
function handleLink(h, node) {
const { uri, desc } = node
function visitor(node, i, parent) {
if (node.tagName === 'div' && node.properties?.type === 'previewer') {
const raw = node.children?.[0]?.value;
const jsx = (node.children?.[1] && toHtml(node.children?.[1])) || undefined;
const tsx = (node.children?.[2] && toHtml(node.children?.[2])) || undefined;
const yaml = node.properties?.meta || {};
let transformCode = raw;
// use import way rather than source code way for external demo (for HMR & sourcemap)
if (node.properties.filePath) {
transformCode = `
import Demo from '${node.properties.filePath}';
export default () => ;`
}
const code = transformer(transformCode, Boolean(tsx));
// replace original node
parent.children[i] = {
previewer: true,
function visitor(node, i, parent) {
if (node.tagName === 'div' && node.properties?.type === 'previewer') {
const raw = node.children?.[0]?.value;
const jsx = (node.children?.[1] && toHtml(node.children?.[1])) || undefined;
const tsx = (node.children?.[2] && toHtml(node.children?.[2])) || undefined;
const yaml = node.properties?.meta || {};
let transformCode = raw;
// use import way rather than source code way for external demo (for HMR & sourcemap)
if (node.properties.filePath) {
transformCode = `
import Demo from '${node.properties.filePath}';
export default () => ;`
}
const code = transformer(transformCode, Boolean(tsx));
// replace original node
parent.children[i] = {
previewer: true,
type: 'raw',
counter = max_length;
return Object.assign({}, node, {
value: node.value.substr(0, nb_addable) + "…",
});
});
remove(truncated_tree, (node) => {
return typeof node.type === "undefined";
});
if (images_counter > 0 && toString(truncated_tree).trim().length === 0) {
return "<p><i>" + placeholder_image_text + "</i></p>";
}
return toHtml(truncated_tree);
}
let result = node
try {
parent.properties.className = (parent.properties.className || []).concat(
'language-' + lang,
)
result = refractor.highlight(nodeToString(node), lang)
} catch (err) {
if (/Unknown language/.test(err.message)) {
return
}
throw err
}
node.children = []
node.properties.dangerouslySetInnerHTML = {
__html: nodeToHTML({
type: 'root',
children: result,
}),
}
}
}
parent.properties.className = (parent.properties.className || []).concat(
`language-${lang}`
);
result = highlight(nodeToString(node), lang);
} catch (err) {
if (/Unknown language/.test(err.message)) {
return;
}
throw err;
}
node.children = [];
node.properties.dangerouslySetInnerHTML = {
__html: nodeToHTML({
type: 'root',
children: result,
}),
};
}
}
function nodeToHtml(node) {
const hast = toHAST(node)
return hast ? toHTML(hast) : ''
}
export default () => {
const [text, setText] = useState('')
const oast = parse(text)
const hast = toHAST(oast)
const html = stringify(hast)
return (
<input value="{text}" rows="10"> setText(e.target.value)}/>
<h1>OAST</h1>
<p>Org Abstract Syntax Tree</p>
const stringify = (root, builder) => {
builder(toHTML(this._vdom.getContent(root)));
};
return {stringify};