前言
这是一个更新的版本,演示站点仍为:CFLMY-生成至美PPT
更新的配置
本次更新主要是将各部分进行了模块化,发生变化的部分分为:
main.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
| const fs = require('fs'); const path = require('path'); const MarkdownIt = require('markdown-it');
const Head = require('./js/Head'); const Hr = require('./js/Hr'); const Image = require('./js/Image'); const OrderList = require('./js/OrderList'); const BulletList = require('./js/BulletList'); const Code = require('./js/Code'); const Link = require('./js/Link'); const Quote = require('./js/Quote');
const md = new MarkdownIt({ html: true });
const headerFilePath = 'decorate/header.html'; const footerFilePath = 'decorate/footer.html'; const markdownFilePath = 'index.md'; const markdownDirPath = 'md';
const headerContent = fs.readFileSync(headerFilePath, 'utf-8'); const footerContent = fs.readFileSync(footerFilePath, 'utf-8');
const markdownContent = fs.readFileSync(markdownFilePath, 'utf-8');
const markdownFiles = fs.readdirSync(markdownDirPath).filter(file => { return path.extname(file) === '.md'; });
md.use(Head, { level: 1, wrapperTag: 'section' }) md.use(Hr); md.use(Image, { defaultImage:'Assert/CFLMY.webp' }); md.use(OrderList); md.use(BulletList); md.use(Code); md.use(Link); md.use(Quote);
const htmlContent = md.render(markdownContent);
const fullHtml = headerContent + htmlContent + footerContent;
const outputFilePath = 'index.html'; fs.writeFileSync(outputFilePath, fullHtml, 'utf-8');
console.log(`Index HTML 文件已生成: ${outputFilePath}`);
markdownFiles.forEach(markdownFile => { const markdownFilePath = path.join(markdownDirPath, markdownFile); const markdownContent = fs.readFileSync(markdownFilePath, 'utf-8');
md.use(Image, { defaultImage:'../Assert/CFLMY.webp' });
const htmlContent = md.render(markdownContent);
const outputHtml = headerContent + htmlContent + footerContent;
const outputFileName = path.basename(markdownFile, '.md') + '.html'; const outputFilePath = path.join('html', outputFileName);
if (!fs.existsSync('html')) { fs.mkdirSync('html'); }
fs.writeFileSync(outputFilePath, outputHtml, 'utf-8');
console.log(`md 目录下的HTML 文件已生成: ${outputFilePath}`); });
|
Head.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| module.exports = function(md, options) { const opts = Object.assign({ level: 1, wrapperTag: 'section' }, options); md.renderer.rules.heading_open = function (tokens, idx, options, env, self) { const level = tokens[idx].tag.slice(1); if (parseInt(level) === opts.level) { return `\n<${opts.wrapperTag}>\n<h1>`; } return self.renderToken(tokens, idx, options); }; };
|
Hr.js
1 2 3 4 5 6
| module.exports = function(md) { md.renderer.rules.hr = function () { return ' <hr> \n </div> \n </section> \n'; }; };
|
Image.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| module.exports = function(md, options) { const defaultImage = options.defaultImage || 'Assert/CFLMY.webp'; md.renderer.rules.image = function (tokens, idx, options, env, self) { const token = tokens[idx]; const alt = token.content; const src = token.attrs[token.attrIndex('src')][1]; const backgroundStyles = { 'BackGround': '', 'BackGround-aligncenter': ' aligncenter', }; const className = backgroundStyles[alt]; const backgroundImage = src ? `url('${src}')` : `url('${defaultImage}')`; if (className !== undefined) { return `<span class="background" style="background-image:${backgroundImage};"></span>\n<div class="wrap${className}">`; } return self.renderToken(tokens, idx, options); }; };
|
OrderList.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| module.exports = function(md) { md.renderer.rules.ordered_list_open = function (tokens, idx, options, env, self) { return `<div class="bg-white shadow"> <ul class="flexblock reasons">`; }; md.renderer.rules.ordered_list_close = function (tokens, idx, options, env, self) { return `</ul> </div>`; }; };
|
BulletList.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| module.exports = function(md) { function WhetherGallery(tokens, idx) { for (let i = idx + 1; i < tokens.length; i++) { const token = tokens[i]; if (token.content) { content = token.content; const match1 = content.match(/^\s*\[IMG-.*/); if (match1) { return true; } } else if (token.type === 'list_item_close') { continue; } else if (token.type === 'bullet_list_close') { break; } } return false; }
md.renderer.rules.bullet_list_open = function(tokens, idx, options, env, self) { const isPlusList = WhetherGallery(tokens, idx); return isPlusList ? `<ul class="flexblock gallery">` : `<ul class="flexblock features">`; };
md.renderer.rules.bullet_list_close = function() { return `</ul>`; }; };
|
Code.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| module.exports = function(md) { md.renderer.rules.fenced_code_open = function(tokens, idx, options, env, self) { return `<div class="column"> <pre>`; }; md.renderer.rules.fenced_code_close = function(tokens, idx, options, env, self) { return `</pre> </div>`; }; };
|
Link.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| module.exports = function(md) { md.renderer.rules.link_open = function (tokens, idx, options, env, self) { const token = tokens[idx]; const alt = tokens[idx + 1].content; const href = tokens[idx].attrs[token.attrIndex('href')][1]; const match1 = alt.match(/^(IMG)-(.*?)-(.*)$/); if (match1) { const prefix = match1[1]; const imgHref = match1[2]; const description = match1[3]; tokens[idx + 1].content = description; token.customMatch = true; return `<a href="${href}"> <figure> <img alt="" src="${imgHref}"> <figcaption> <h2> `; } return self.renderToken(tokens, idx, options); }; md.renderer.rules.link_close = function (tokens, idx, options, env, self) { const token = tokens[idx - 1]; if (token.customMatch) { delete token.customMatch; return `</h2> </figcaption> </figure> </a>`; } return self.renderToken(tokens, idx, options); }; };
|
Quote.js
1 2 3 4 5 6 7 8 9 10 11 12 13
| module.exports = function(md) { md.renderer.rules.blockquote_open = function (tokens, idx, options, env, self) { return '<ul class="flexblock gallery"><li>'; }; md.renderer.rules.blockquote_close = function (tokens, idx, options, env, self) { return '</li></ul>'; }; };
|
更新内容
- 模块化各部分
- 修改无序列表的生成逻辑,使得当无序列表的首个引导为
[IMG-
时自动替换为gallery
选项,从而较好的保持原有的样式的基础上,支持了插入图片式链接的需求。 - 修改标题的逻辑,便于进行拓展和更新
- 修改引用的格式
- 修改Link部分的匹配,便于后续不断增加更多的链接有关语法。
后记
0.0.3版本注重与模块化,方便了调试和修改。
同系列
CFLMY-PPT自动生成记录文档001
CFLMY-PPT自动生成记录文档002
CFLMY-PPT自动生成记录文档003
CFLMY-PPT自动生成记录文档003