Skip to content

Commit

Permalink
Linkable headers
Browse files Browse the repository at this point in the history
  • Loading branch information
sinnbeck committed Nov 11, 2020
1 parent 62b21a0 commit 00fd8a3
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 91 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ Result:
```

### Adding id and href
Markdom makes it simple to add links to headings (for use in documentation).
Just set the `anchor_tags.enabled` to `true` in the `markdom.php` config file, and Markdom takes care of the rest.
Markdom makes it simple to add id and links (`<a href />`) to headings (for use in documentation).
Just set the `links.enabled` to `true` in the `markdom.php` config file, and Markdom takes care of the rest.

Check the documentation if the config, for configuration options.

Expand Down
39 changes: 13 additions & 26 deletions src/Markdom.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,30 +60,31 @@ public function toHtml($markdown)

protected function addAnchorTags($dom)
{
if (!config('markdom.anchor_tags.enabled')) {
if (!config('markdom.links.enabled')) {
return;
}

$method = config('markdom.anchor_tags.position', 'before');
$method = config('markdom.links.position', 'before');

collect(config('markdom.anchor_tags.elements'))
collect(config('markdom.links.elements'))
->each(function ($tag) use ($dom, $method) {
$dom->filter($tag)->each(function($element) use ($method){
throw_if(!method_exists($element, $method),
MethodNotAllowedException::class
);

$slug = Str::slug(
$element->html(),
config('markdom.anchor_tags.slug_delimiter')
config('markdom.links.slug_delimiter')
);

$element->$method($this->makeAnchorTag($slug));
$element->setAttribute('id', $slug);

if (config('markdom.anchor_tags.add_id_to') === 'element') {
$element->setAttribute('id', $slug);
if (config('markdom.links.add_anchor')) {
throw_if(!method_exists($element, $method),
MethodNotAllowedException::class
);

$element->$method($this->makeAnchorTag($slug));

}

});
});

Expand All @@ -98,21 +99,7 @@ protected function addClasses($dom)

protected function makeAnchorTag($slug)
{
$id = config('markdom.anchor_tags.add_id_to') === 'a' ? 'id="' . $slug . '"' : '';
$href = config('markdom.anchor_tags.disable_href') ? '' : 'href="#' . $slug . '"';
if (!$id && !$href) {
return '';
}
$parts = [
'<a',
$id,
$href,
'/>'
];

$filtered = array_filter($parts);

return implode(' ', $filtered);
return '<a href="#' . $slug . '" />';
}

protected function addCodeHighlights($dom)
Expand Down
28 changes: 8 additions & 20 deletions src/config/markdom.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,41 +55,29 @@
],

/**
* This being enabled adds an (invisible) anchor tag to configured elements
* by default this is h1, h2 and h3 to make it easy to target them in a
* navigation
* This being enabled adds an id and an (invisible) anchor tag to configured elements
*/
'anchor_tags' => [
'links' => [
'enabled' => env('MARKDOM_ADD_ANCHORS', false),

/**
* Here you can define which elements will receive anchor tags
* Here you can define which elements will receive id tags
*/
'elements' => [
'h1',
'h2',
'h3',
'h4',
],

/**
* Where to add the id attribute
*
* Allowed values are 'element' and 'a'
*/
'add_id_to' => 'element',

/**
* Disable the href attribute
*
* If the id is added to element and href
* is disabled, no a tag will be rendered
* Set the delimiter to use when creating id and href slugs
*/
'disable_href' => false,
'slug_delimiter' => '-',

/**
* Set the delimiter to use when creating id and href slugs
* Whether to add an achor tag
*/
'slug_delimiter' => '-',
'add_anchor' => true,

/**
* Here you can define where the anchor shall be placed, possible values:
Expand Down
58 changes: 18 additions & 40 deletions tests/Unit/AddAnchorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
uses(Tests\TestCase::class);

beforeEach(function () {
app()->config->set('markdom.anchor_tags.enabled', true);
app()->config->set('markdom.links.enabled', true);
});

test('it adds an anchor tag before', function () {
app()->config->set('markdom.anchor_tags.position', 'before');
app()->config->set('markdom.anchor_tags.elements', [
app()->config->set('markdom.links.position', 'before');
app()->config->set('markdom.links.elements', [
'h1',
'h2',
]);
Expand All @@ -31,8 +31,8 @@
});

test('it adds an anchor tag after', function () {
app()->config->set('markdom.anchor_tags.position', 'after');
app()->config->set('markdom.anchor_tags.elements', [
app()->config->set('markdom.links.position', 'after');
app()->config->set('markdom.links.elements', [
'h1',
'h2',
]);
Expand All @@ -53,8 +53,8 @@
});

test('it wraps an anchor tag', function () {
app()->config->set('markdom.anchor_tags.position', 'wrap');
app()->config->set('markdom.anchor_tags.elements', [
app()->config->set('markdom.links.position', 'wrap');
app()->config->set('markdom.links.elements', [
'h1',
'h2',
]);
Expand All @@ -75,13 +75,12 @@
});

test('it add an anchor tag prepend', function () {
app()->config->set('markdom.anchor_tags.position', 'prepend');
app()->config->set('markdom.anchor_tags.elements', [
app()->config->set('markdom.links.position', 'prepend');
app()->config->set('markdom.links.elements', [
'h1',
'h2',
]);


$converter = app('markdom');

$markdown =
Expand All @@ -97,8 +96,8 @@
});

test('it add an anchor tag wrapping content', function () {
app()->config->set('markdom.anchor_tags.position', 'wrapInner');
app()->config->set('markdom.anchor_tags.elements', [
app()->config->set('markdom.links.position', 'wrapInner');
app()->config->set('markdom.links.elements', [
'h1',
'h2',
]);
Expand All @@ -119,13 +118,12 @@
});

test('it add an anchor tag append', function () {
app()->config->set('markdom.anchor_tags.position', 'append');
app()->config->set('markdom.anchor_tags.elements', [
app()->config->set('markdom.links.position', 'append');
app()->config->set('markdom.links.elements', [
'h1',
'h2',
]);


$converter = app('markdom');

$markdown =
Expand All @@ -140,29 +138,9 @@
assertMatchesTextSnapshot($converter->toHtml($markdown));
});

test('it does not add href if disabled', function () {
app()->config->set('markdom.anchor_tags.disable_href', true);
app()->config->set('markdom.anchor_tags.add_id_to', 'a');
app()->config->set('markdom.anchor_tags.elements', [
'h1',
'h2',
]);

$converter = app('markdom');

$markdown =
<<<MARKDOWN
# Hello World!
## Lorem ipsum
Lorem ipsum
MARKDOWN;

assertMatchesTextSnapshot($converter->toHtml($markdown));
});

test('it removes a tag if not needed', function () {
app()->config->set('markdom.anchor_tags.disable_href', true);
app()->config->set('markdom.anchor_tags.elements', [
test('it works without achor', function () {
app()->config->set('markdom.links.add_anchor', false);
app()->config->set('markdom.links.elements', [
'h1',
'h2',
]);
Expand All @@ -183,8 +161,8 @@

$this->expectException(MethodNotAllowedException::class);

app()->config->set('markdom.anchor_tags.position', 'foo');
app()->config->set('markdom.anchor_tags.elements', [
app()->config->set('markdom.links.position', 'foo');
app()->config->set('markdom.links.elements', [
'h1',
'h2',
]);
Expand Down

This file was deleted.

0 comments on commit 00fd8a3

Please sign in to comment.