laravelista / sherlock
该软件包已被废弃,不再维护。未建议替代软件包。
减去一个markdown文档,获取特定的章节和/或目录。
0.5.3
2016-10-29 20:32 UTC
Requires
- php: >=7.0.0
- tightenco/collect: ^5.2
- twig/twig: ~1.0
Requires (Dev)
- phpspec/phpspec: ^3.1
README
Sherlock是一个PHP软件包,它为markdown提供独特的功能。
它可以创建目录或获取特定的章节。
概述
我在我的网站 Laravelista 上使用此软件包来创建课程、文章和软件包的目录。我还用它来提供我的课程免费样本。
获取目录
给定这个markdown
# Book Title ## Chapter 1 This is chapter *one*. ## Chapter 2 This is chapter two.
您可以生成目录
use Laravelista\Sherlock\Sherlock; $sherlock = new Sherlock; return $sherlock->deduct($markdown)->getToc();
HTML输出
<ul class="sherlock-toc"> <li><a href="#book-title">Book Title</a> <ul> <li><a href="#chapter-1">Chapter 1</a></li> <li><a href="#chapter-2">Chapter 2</a></li> </ul> </li> </ul>
获取特定的章节
给定与上面示例相同的markdown,我们可以通过名称从我们的markdown文档中获取特定的章节。
use Laravelista\Sherlock\Sherlock; $sherlock = new Sherlock; return $sherlock->deduct($markdown)->get('Chapter 1');
Markdown输出
This is chapter *one*.
Laravel在视图中使用
如果您正在使用Laravel,有一种方便的方法在视图中加载Sherlock。在您想显示目录的视文件顶部添加以下代码以注入Sherlock和减去内容
@inject('sherlock', 'Laravelista\Sherlock\Sherlock') <?php $sherlock->deduct($lesson->content) ?>
然后,在您想显示实际目录的地方添加以下内容
{!! $sherlock->getToc() !!}
要获取特定的章节,请使用 $sherlock->get()
。请记住,get()
返回markdown,因此请确保将markdown解析为HTML。
安装
从命令行
composer require laravelista/sherlock
API
deduct
读取给定的markdown字符串并生成文档的索引(库)。
$sherlock->deduct(string $content)
您可以将此方法与其他API方法链接起来,但此方法必须始终首先调用。如果需要,可以通过 getLibrary()
获取库。
getToc
以HTML格式返回目录。
$sherlock->deduct($markdown)->getToc()
示例
给定这个markdown
# Book Title ## Chapter 1 This is chapter *one*. ## Chapter 2 This is chapter two.
它返回此HTML输出
<ul class="sherlock-toc"> <li><a href="#book-title">Book Title</a> <ul> <li><a href="#chapter-1">Chapter 1</a></li> <li><a href="#chapter-2">Chapter 2</a></li> </ul> </li> </ul>
get
返回特定章节的markdown。
$sherlock->deduct($markdown)->get('Chapter 1')
示例
给定这个markdown
# Book Title ## Chapter 1 This is chapter *one*. ## Chapter 2 This is chapter two.
它返回此Markdown输出
This is chapter *one*.
getLibrary
它返回文档的索引(库),该索引是从给定的markdown在 deduct()
方法中提取的;作为数组。
$sherlock->deduct($markdown)->getLibrary()
示例
这是您获取的库的示例
[ [ 'level' => 1, 'name' => 'This is the document title', 'starts_at' => 0, 'ends_at' => 3, 'slug' => 'this-is-the-document-title' ], [ 'level' => 2, 'name' => 'Introduction', 'starts_at' => 4, 'ends_at' => 7, 'slug' => 'introduction' ], [ 'level' => 3, 'name' => 'Another introduction', 'starts_at' => 8, 'ends_at' => 11, 'slug' => 'another-introduction' ], [ 'level' => 4, 'name' => 'Deep introduction', 'starts_at' => 12, 'ends_at' => 19, 'slug' => 'deep-introduction' ], [ 'level' => 2, 'name' => 'Plot', 'starts_at' => 20, 'ends_at' => 23, 'slug' => 'plot' ], [ 'level' => 2, 'name' => 'Conclusion', 'starts_at' => 24, 'ends_at' => 27, 'slug' => 'conclusion' ], ];
有了它,您可以做所有 sorts of things。