christianblos/markdown2html

将markdown文件转换为静态HTML页面

0.4 2020-10-13 16:49 UTC

This package is auto-updated.

Last update: 2024-09-14 01:22:34 UTC


README

将markdown文件转换为静态HTML页面。 查看演示(我使用它来记录我的另一个项目)。

安装

使用composer安装最新版本:composer require --dev christianblos/markdown2html

通过命令行创建HTML

首先,您必须在您的仓库根目录中添加一个 markdown2html.config.php 文件来配置markdown2html。

<?php
$theme        = new Markdown2Html\Theme\DefaultTheme();
$theme->title = 'My Project';

$config = new \Markdown2Html\Config();

$config->src   = '/path/to/markdown-files';
$config->dest  = '/path/to/destination-folder'; 
$config->theme = $theme;

return $config;

现在您可以通过执行 vendor/bin/markdown2html 在您的目标文件夹中创建HTML文件。

注意:如果您的配置文件不在项目的根目录中,您可以将其作为第一个参数传递:vendor/bin/markdown2html /path/to/config.php

通过代码创建HTML

也许您有自己的命令行工具,并且想直接使用PHP代码来生成HTML文件。这没问题

<?php
$src   = '/path/to/markdown-files';
$dest  = '/path/to/destination-folder'; 

$theme        = new Markdown2Html\Theme\DefaultTheme();
$theme->title = 'My Project';

$builder = new Markdown2Html\Builder();
$builder->build($src, $dest, $theme);

Markdown文件的结构

生成的HTML中的导航基于您的文件夹结构。让我们假设您有以下文件结构

markdown
   |- 00.Installation.md
   |- 01.Configuration.md
   |- 02.Usage.md
   |- 02.Usage
   |     |- 00.Via-command--line.md
   |     |- 01.Via-PHP.md
   |
   |- index.md

数字前缀(如“01.”)表示导航项的顺序。如果顺序无关紧要,您可以省略它。

所有破折号都被替换为空格(“Via-PHP”→“Via PHP”)。如果您想在导航中使用破折号,请使用2或3个破折号。

  • "00.Via-command--line.md" → "Via command-line"
  • "00.Via-command---line.md" → "Via command - line"

如果有一个文件与文件夹有相同的名称(如02.Usage和02.Usage.md),则该文件将是该文件夹的索引页面。如果您没有该文件,则将自动创建索引,并包含子导航。

index.md的内容包含HTML索引页面的文本。

在上面的例子中,生成的导航将如下所示

Installation
Configuration
Usage
   Via command-line
   Via PHP

默认主题

DefaultTheme有一些额外的配置您可以使用

<?php
$theme                = new Markdown2Html\Theme\DefaultTheme();

// Add additional links to the navigation
$theme->naviLinks = [
    'Github' => 'https://github.com/christianblos'
];

// overwrite styles
$theme->additionalCss = 'a#header {background-color:red}';