定制 mistralys/markdown-viewer 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

mistralys/markdown-viewer

Composer 安装命令:

composer require mistralys/markdown-viewer

包简介

PHP based viewer for Markdown files, to view them with fenced code highlighting and navigation.

README 文档

README

PHP based viewer for Markdown files, to view them with fenced code highlighting and navigation.

It is designed to be used for viewing markdown-based documentation files, in a fire and forget way. The layout is based on Bootstrap 5, and does not need any additional configuration.

Requirements

  • PHP7.4+

Features

  • Automatic jump navigation built using the document's headers.
  • Easily switch between the available documents.
  • Syntax highlighted fenced code blocks.
  • Light and dark modes.
  • Additional support for "1)" style ordered lists.

Installing

The package is made to be used as a dependency in a documentation project: Put it in a folder in a webserver, point it to some markdown files, and it will display them.

  1. Create a folder in your webroot from which to serve the documentation.
  2. Create a composer project there.
  3. Require the package: composer require mistralys/markdown-viewer.
  4. Create a PHP file (index.php) as endpoint for the documentation.
  5. Paste the following code into the file
  6. Edit the list of files you wish to view.
  7. Point your browser to the file.
<?php

declare(strict_types=1);

use Mistralys\MarkdownViewer\DocsManager;
use Mistralys\MarkdownViewer\DocsViewer;

if(!file_exists(__DIR__.'/vendor/autoload.php')) {
    die('Please run <code>composer install</code> first.');
}

require_once __DIR__.'/vendor/autoload.php';

$manager = new DocsManager();

// Add all the files you wish to view here, along with
// a title that will be shown in the UI.
$manager->addFile('Title of the file', '/path/to/documentation.md');

// The viewer needs to know the URL to the vendor/ folder, relative
// to the script. This is needed to load the clientside dependencies,
// like jQuery and Bootstrap.
(new DocsViewer($manager, '/url/to/vendor'))
    ->setTitle('Documentation')
    ->display();

Adding single files

Single files can be added using addFile(). This allows specifying the name that the file will be listed under in the UI.

use Mistralys\MarkdownViewer\DocsManager;

$manager = new DocsManager();

// Add a single folder, non-recursive.
$manager->addFile('Name of the file', '/path/to/file.md');

Adding folders

To add multiple files, use the addFolder() method:

use Mistralys\MarkdownViewer\DocsManager;

$manager = new DocsManager();

// Add a single folder, non-recursive.
$manager->addFolder('/path/to/files');

// Add a folder and all its subfolders
$manager->addFolder('/path/to/files', true);

By default, all files with the md extension will be added. A different extension can be specified using the third parameter:

use Mistralys\MarkdownViewer\DocsManager;

$manager = new DocsManager();

// Add all TXT files from a single folder, non-recursive.
$manager->addFolder('/path/to/files', false, 'txt');

NOTE: Adding files this way means you cannot specify file IDs (see "Consistent file permalinks"). Please double-check that this is okay in your use case.

Consistent file permalinks

By default, the viewer will create an ID for each file based on its absolute path on disk. This means that the ID will change if the file is moved at some point, or if the viewer is used on different systems. Sharing permalinks risks the links being broken at some point.

To avoid this issue, specify a unique file ID manually when adding single files:

use Mistralys\MarkdownViewer\DocsManager;

$manager = new DocsManager();

$manager->addFile(
    'Name of the file', 
    '/path/to/file.md',
    '(Unique file ID)'
);

The ID can be any string; the viewer uses it to create the hash that is used in the UI to identify the files. This way, permalinks will always stay consistent.

Dark mode

To turn on dark mode, simply use makeDarkMode():

use Mistralys\MarkdownViewer\DocsViewer;
use Mistralys\MarkdownViewer\DocsManager;

$manager = new DocsManager();

// Configure the files

(new DocsViewer($manager, '/url/to/vendor'))
    ->setTitle('Documentation')
    ->makeDarkMode()
    ->display();

Viewing the example

The bundled example is built exactly like the example above, and will display this README.md file. To get it running, follow these steps:

  1. Clone the repository into a webserver's document root
  2. Run composer install in the package folder to install the dependencies
  3. Point your browser to the package folder's example.php file

Setting options

All options regarding the parsing of markdown files are handled by the DocsConfig class. You can optionally pass a configuration instance to the manager to customize settings:

use Mistralys\MarkdownViewer\DocsManager;
use Mistralys\MarkdownViewer\DocsConfig;

$config = (new DocsConfig())
    ->addIncludePath(__DIR__.'/documentation/includes')
    ->addIncludeExtension('php');

$manager = (new DocsManager($config))
    ->addFile('Package readme', 'README.md');

Including external files

The include command

The {include-file} command allows you to import the content of external files into your documents. This is especially handy for code examples, as it allows you to maintain them separately from the main document.

If viewed through example.php, the following code sample is loaded dynamically, for example:

{include-file: test-php-highlight.php}

The command looks like this:

\{include-file: test-php-highlight.php\}

NOTE: It is easy to go overboard with includes. Keep in mind that Markdown files are meant to be read as-is. Splitting them up too much will make them unreadable without the UI. Use them where it makes sense, like for large code samples.

Setting allowed paths

Include commands are disallowed by default, as long as no include folders have been configured:

use Mistralys\MarkdownViewer\DocsConfig;

$config = (new DocsConfig())
    ->addIncludePath('/documentation/includes');

Paths in the {include-file} command are relative to the configured include paths. Multiple folders can be added, and all of them are searched. The first matching file name is then used.

Setting allowed extensions

By default, only md and txt files are allowed to be included. Additional extensions can easily be added:

use Mistralys\MarkdownViewer\DocsConfig;

$config = (new DocsConfig())
    ->addIncludeExtension('php');

Several extensions can also be added at once:

use Mistralys\MarkdownViewer\DocsConfig;

$extensions = array(
    'php',
    'js',
    'css'
);

$config = (new DocsConfig())
    ->addIncludeExtensions($extensions);

Restricting file sizes

To avoid including large files, only files up to 6Kb may be included by default. This can be adjusted with the configuration class:

use Mistralys\MarkdownViewer\DocsConfig;

// Allow files up to 12Kb (12.000 bytes)
$config = (new DocsConfig())
    ->setMaxIncludeSize(12000);

Filtering include file contents

Filters allow the pre-processing of included files before they are rendered by the Markdown renderer. The whole content of the files can be filtered to prepare them for rendering.

Example filter class

<?php

declare(strict_types=1);

use AppUtils\FileHelper\FileInfo;
use Mistralys\MarkdownViewer\DocFile;
use Mistralys\MarkdownViewer\DocsConfig;
use Mistralys\MarkdownViewer\Parser\BaseIncludeFilter;

class ExampleIncludeFilter extends BaseIncludeFilter
{
    public function getExtensions(): array
    {
        return array('example');
    }
    
    public function isValidFor(DocFile $sourceFile, DocsConfig $config, FileInfo $includeFile): bool
    {
        return true;
    }

    public function filter(string $content): string
    {
        return str_replace('EXAMPLE', '**EXAMPLE**', $content);
    }
}

This filter ensures that all occurrences of EXAMPLE in the text are formatted as bold text.

Adding filters

Adding filters is very easy. Simply add a filter instance to the configuration, it will automatically be applied.

<?php
use Mistralys\MarkdownViewer\DocsConfig;

$config = (new DocsConfig())
    ->addIncludeFilter(new ExampleIncludeFilter());

Only filter specific files

By default, filters are applied to all files that match the filter's file extension(s). However, if the extension matches, the filter is only applied if the isValidFor() method returns true. This makes it possible to use the provided file and configuration information to decide whether to filter the content.

The following example limits the filtering to included files found in the folder /path/to/target/folder, or any of its subfolders.

<?php

declare(strict_types=1);

use AppUtils\FileHelper\FileInfo;
use Mistralys\MarkdownViewer\DocFile;
use Mistralys\MarkdownViewer\DocsConfig;
use Mistralys\MarkdownViewer\Parser\BaseIncludeFilter;

class ExampleIncludeFilter extends BaseIncludeFilter
{
    public function isValidFor(DocFile $sourceFile, DocsConfig $config, FileInfo $includeFile): bool
    {
        return $includeFile->isWithinPath('/path/to/target/folder');
    }
}

mistralys/markdown-viewer 适用场景与选型建议

mistralys/markdown-viewer 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.48k 次下载、GitHub Stars 达 10, 最近一次更新时间为 2021 年 03 月 15 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 mistralys/markdown-viewer 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 mistralys/markdown-viewer 我们能提供哪些服务?
定制开发 / 二次开发

基于 mistralys/markdown-viewer 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 1.48k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 10
  • 点击次数: 2
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 10
  • Watchers: 1
  • Forks: 4
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-03-15