/* eslint-disable */ var marked = require('marked'); let prism = require('prismjs'); let loadLanguages = require('prismjs/components/'); loadLanguages([ 'bash', 'javascript', 'java', 'python', 'jsx', 'tsx', 'css', 'markup', 'json' ]); var yaml = (yaml = require('js-yaml')); var rYml = /^\s*---([\s\S]*?)---\s/; var renderer = new marked.Renderer(); marked.setOptions({ renderer: renderer, gfm: true, tables: true, breaks: false, pedantic: false, smartLists: true, smartypants: false }); // Synchronous highlighting with prism.js marked.setOptions({ highlight: function (code, lang) { if (lang) { return prism.highlight(code, prism.languages[lang], lang); } else { return code; } } }); // renderer.table = function(header, body) { // return '\n' // + '\n' // + header // + '\n' // + '\n' // + body // + '\n' // + '
\n'; // }; renderer.link = function (href, title, text) { if (this.options.sanitize) { try { var prot = decodeURIComponent(unescape(href)) .replace(/[^\w:]/g, '') .toLowerCase(); } catch (e) { return ''; } if (prot.indexOf('javascript:') === 0 || prot.indexOf('vbscript:') === 0) { return ''; } } if (href && href[0] === '#') { href = '#' + encodeURIComponent( href .substring(1) .toLowerCase() .replace(/[^\u4e00-\u9fa5_a-zA-Z0-9]+/g, '-') ); } var out = ''; return out; }; module.exports = function (content, file) { var m = rYml.exec(content); var info = {}; if (m && m[1]) { info = yaml.safeLoad(m[1]); content = content.substring(m[0].length); } var toc = { label: '目录', type: 'toc', children: [], level: 0 }; var stack = [toc]; renderer.heading = function (text, level) { var escapedText = encodeURIComponent( text.toLowerCase().replace(/[^\u4e00-\u9fa5_a-zA-Z0-9]+/g, '-') ); if (level < 5) { var menu = { label: text, fragment: escapedText, fullPath: '#' + escapedText, level: level }; while (stack.length && stack[0].level >= level) { stack.shift(); } stack[0].children = stack[0].children || []; stack[0].children.push(menu); stack.unshift(menu); } var anchor = ''; return '' + anchor + text + ''; // return '' + // text + ''; }; const placeholder = {}; let index = 1; content = content.replace( /```(schema|html)(?::(.*?))?\n([\s\S]*?)```/g, function (_, lang, attr, code) { const setting = {}; attr && attr.split(/\s+/).forEach(function (item) { var parts = item.split('='); if (parts[1] && /^('|").*\1/.test(parts[1])) { parts[1] = parts[1].substring(1, parts[1].length - 1); } setting[parts[0]] = parts[1] ? decodeURIComponent(parts[1]) : ''; if (parts[0] === 'height') { setting.height = parseInt(setting.height, 10) /*编辑器的高度*/; attr = attr.replace(item, `height="${setting.height}"`); } }); // placeholder[index] = ``; if (lang === 'html') { if (~code.indexOf('
${code}
${prism.highlight(
          code
            .replace(/"data:(\w+\/\w+);.*?"/g, '"data:$1; ..."')
            .replace(/]*)>[\s\S]*?<\/svg>/g, '...')
            .replace(/class="([^"]*?)\.\.\.([^"]*?)"/g, 'class="$1..."'),
          prism.languages[lang],
          lang
        )}
`; } else { placeholder[ index ] = `
`; } return `[[${index++}]]`; } ); content = marked(content).replace(/

\[\[(\d+)\]\]<\/p>/g, function (_, id) { return placeholder[id] || ''; }); content = fis.compile.partial(content, file, 'html'); // + `\n\n

文档内容有误?欢迎大家一起来编写,文档地址:${file.subpath}
`; info.html = '
' + content.replace( /<\!\-\-amis\-preview\-(start|end)\-\-\>/g, function (_, type) { return type === 'start' ? '
' : '
'; } ) + '
'; info.toc = toc; return 'module.exports = ' + JSON.stringify(info, null, 2) + ';'; };