在线markdown生成html,从markdown生成静态html页面的最简单的方法

Pagic

The easiest way to generate static html page from markdown

Getting Started

Installation

npm install pagic -g

Markdown + Layout => HTML

Let's say we have a project like this:

docs/

├── public/

└── src/

├── _layout.js

└── index.md

The _layout.js is a simple javascript module which contains a template string:

module.exports = function ({ title, content }) {

return `

${title}

${content}

`

};

The index.md is a simple markdown file:

#Pagic

The easiest way to generate static html page from markdown

Then run

pagic

We'll get an index.html file in public directory:

docs/

├── public/

| └── index.html

└── src/

├── _layout.js

└── index.md

The content should be:

Pagic

Pagic

The easiest way to generate static html page from markdown

Here we use markdown-it with plugins markdown-it-anchor and markdown-it-title to parse the markdown file.

Copy Other Files

If there are other files which are not ended with .md or start with _, we will simply copy them:

docs/

├── public/

| ├── css

| | └── site.css

| └── index.html

└── src/

├── css

| └── site.css

├── _layout.js

└── index.md

Sub Page and Sub Layout

We can have sub directory which contains markdown files.

Sub directory can also have a _layout.js file.

In this case, only markdown files in sub directory will use sub/_layout.js as the template.

If we cannot find a _layout.js in the sub directory, we'll recursively find the _layout.js in the parent directory.

docs/

├── public/

| ├── css

| | └── site.css

| └── index.html

| └── sub

| └── index.html

└── src/

├── css

| └── site.css

├── _layout.js

|── index.md

└── sub

├── _layout.js

└── index.md

relativeToRoot

The last thing, we can get the relativeToRoot variable in the _layout.js, this helps us insert the relative resources:

module.exports = function ({ title, content, relativeToRoot }) {

return `

${title}

${content}

`

};

Options

There are some options while using the cli:

-s , --src-dir=: Change the src directory, default is src

-d , --dist-dir=: Change the dist directory, default is public

-w, --watch: Watch for src directory change

Use It as a Node Module

It's also able to use it as a node module:

npm install pagic --save

const pagic = requrie('pagic');

pagic({

srcDir: 'src',

distDir: 'public'

});

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Markdown是一种轻量级的标记语言,广泛用于编写文档和格式化文本。通过使用Markdown,我们可以快速简单地创建文档,并且可以将其转换为静态HTML网页。 Markdown生成静态HTML具有以下优势: 1. 简单易懂:Markdown的语法非常简洁直观,使用者无需掌握复杂的HTML标记语言,只需使用简单的文本标记即可实现文档的格式化。 2. 跨平台兼容:无论是在Windows、Mac还是Linux等操作系统上,Markdown都可以被使用。生成静态HTML也可以在不同的平台和浏览器上进行浏览,具有良好的兼容性。 3. 易于维护:Markdown文档的纯文本格式使得修改和维护变得非常容易。只需对源文件进行修改,再次转换为HTML即可更新整个网页,这样可以节省大量时间和精力。 4. 体积小、加载速度快:由于Markdown生成HTML文件不包含复杂的样式和冗余的标记,因此文件体积较小,加载速度快,有助于提升用户的浏览体验。 5. 支持Github等平台:很多在线平台和代码仓库(如Github)都支持Markdown语法,并且能够直接将Markdown文件显示为HTML格式,使得分享和展示代码和文档更加方便。 总结起来,使用Markdown生成静态HTML具有简单易懂、跨平台兼容、易于维护、加载速度快以及支持各种在线平台等优势。这使得Markdown成为了一种广泛应用于文档编写和展示的强大工具。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值