Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
return function(node, nodes, i) {
var opts = extend({}, this.options, options);
var replaceRe = /(?:lang(uage)?|highlight|source|brush:\s*)[-]?| /g;
var testRe = /(?:lang(uage)?|highlight|source|brush:\s*)/;
var pre = opts.literalPre || !util.hasType(node, 'code');
if (pre) {
node.lang = (node.attr && node.attr['data-lang']) || '';
var attribs = node.attribs;
if (!node.lang && attribs && attribs.class) {
var cls = attribs.class;
if (cls && testRe.test(cls)) {
node.lang = cls.replace(replaceRe, '');
}
}
var parent = node.parent || {};
if (!node.lang && parent && parent.attribs) {
cls = parent.attribs.class;
if (cls && testRe.test(cls)) {
node.lang = cls.replace(replaceRe, '');
function tableize(ast) {
util.visit(ast, {recurse: true}, function(node) {
if (node.type === 'text') {
node.val = node.val.trim();
}
return node;
});
if (!util.hasType(ast, 'thead')) {
createHead(ast);
}
combineRows(ast);
if (!util.hasType(ast, 'tbody')) {
createBody(ast);
}
if (util.hasType(ast, 'tfoot')) {
tfootNodes(ast);
}
util.mapVisit(ast, {recurse: true}, function(node) {
if (node.type === 'br') {
node.type = 'text';
node.val = '';
}
});
}
util.visit(ast, {recurse: true}, function(node) {
if (node.type === 'text') {
node.val = node.val.trim();
}
return node;
});
if (!util.hasType(ast, 'thead')) {
createHead(ast);
}
combineRows(ast);
if (!util.hasType(ast, 'tbody')) {
createBody(ast);
}
if (util.hasType(ast, 'tfoot')) {
tfootNodes(ast);
}
util.mapVisit(ast, {recurse: true}, function(node) {
if (node.type === 'br') {
node.type = 'text';
node.val = '';
}
});
}
if (ast.nodes.length === 3) {
var text = { type: 'text', val: '' };
var th = { type: 'th', attribs: { align: 'center'}, nodes: [text] };
define(text, 'parent', th);
define(th, 'parent', thead);
thead.nodes.push(th);
util.wrapNodes(th, Node);
util.wrapNodes(thead, Node);
ast.nodes.splice(1, 0, thead);
return ast;
}
for (var i = 0; i < ast.nodes.length; i++) {
var node = ast.nodes[i];
if (node.nodes && util.hasType(node, 'th')) {
define(node, 'parent', thead);
thead.nodes.push(node);
util.wrapNodes(thead, Node);
ast.nodes[i] = thead;
break;
}
}
return ast;
}
function createBody(ast) {
if (util.hasType(ast, 'tbody')) return;
var open = ast.nodes.shift();
var close = ast.nodes.pop();
var started;
var nodes = [];
var tbody;
var len = ast.nodes.length;
var idx = -1;
while (++idx < len) {
var node = ast.nodes[idx];
if (node.type === 'tr' && !started) {
started = true;
tbody = { type: 'tbody', nodes: [] };
define(tbody, 'parent', ast);
nodes.push(tbody);
}
Node.prototype.hasType = function(type) {
return utils.hasType(this, type);
};