diff --git a/README.md b/README.md
index bc298b7..b1a220b 100644
--- a/README.md
+++ b/README.md
@@ -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 (``) 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.
diff --git a/src/Markdom.php b/src/Markdom.php
index 6a1fae3..723d740 100644
--- a/src/Markdom.php
+++ b/src/Markdom.php
@@ -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));
}
+
});
});
@@ -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 = [
- ''
- ];
-
- $filtered = array_filter($parts);
-
- return implode(' ', $filtered);
+ return '';
}
protected function addCodeHighlights($dom)
diff --git a/src/config/markdom.php b/src/config/markdom.php
index 3204880..83a5da6 100644
--- a/src/config/markdom.php
+++ b/src/config/markdom.php
@@ -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:
diff --git a/tests/Unit/AddAnchorTest.php b/tests/Unit/AddAnchorTest.php
index b39b826..7c980ed 100644
--- a/tests/Unit/AddAnchorTest.php
+++ b/tests/Unit/AddAnchorTest.php
@@ -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',
]);
@@ -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',
]);
@@ -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',
]);
@@ -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 =
@@ -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',
]);
@@ -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 =
@@ -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 =
-<<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',
]);
@@ -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',
]);
diff --git a/tests/__snapshots__/AddAnchorTest__it_does_not_add_href_if_disabled__1.txt b/tests/__snapshots__/AddAnchorTest__it_does_not_add_href_if_disabled__1.txt
deleted file mode 100644
index 335456d..0000000
--- a/tests/__snapshots__/AddAnchorTest__it_does_not_add_href_if_disabled__1.txt
+++ /dev/null
@@ -1,3 +0,0 @@
-Hello World!
-Lorem ipsum
-Lorem ipsum
\ No newline at end of file
diff --git a/tests/__snapshots__/AddAnchorTest__it_removes_a_tag_if_not_needed__1.txt b/tests/__snapshots__/AddAnchorTest__it_works_without_achor__1.txt
similarity index 100%
rename from tests/__snapshots__/AddAnchorTest__it_removes_a_tag_if_not_needed__1.txt
rename to tests/__snapshots__/AddAnchorTest__it_works_without_achor__1.txt