-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Dennis Kerzig
committed
Nov 7, 2017
0 parents
commit 027e5fb
Showing
8 changed files
with
230 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2017 | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# ⚓ Broken Links Checker Widget by [@wottpal](https://twitter.com/wottpal) | ||
|
||
<!-- Buttons --> | ||
![Release](https://img.shields.io/github/release/wottpal/kirby-broken-links-widget/all.svg) | ||
[![MIT](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/wottpal/kirby-broken-links-widget/master/LICENSE) | ||
[![Tweet](https://img.shields.io/twitter/url/https/github.com/wottpal/kirby-broken-links-widget.svg?style=social)](https://twitter.com/intent/tweet?text=⚓ Anchor-Headings for @getkirby by @wottpal&url=https://git.io/v7aFU) | ||
|
||
|
||
A panel-widget for the [Kirby CMS](https://getkirby.com) which shows broken links within the pages. | ||
|
||
|
||
![Screenshot of the Broken-Links Panel-Widget](demo.png) | ||
|
||
_Disclaimer:_ This is a pre-release and it is not feature-complete yet. (see *Roadmap* below) | ||
|
||
|
||
# Installation | ||
|
||
Use [Kirby's CLI](https://github.com/getkirby/cli) and install the plugin via: `kirby plugin:install wottpal/kirby-broken-links-widget` or place the repo manually under `site/plugins` (without the `kirby-` prefix). | ||
|
||
🎉 **That's it.** | ||
|
||
|
||
# Options | ||
|
||
The following options can be set globally in your `config.php` with `c::set($key, $value = null)`. You can also set multiple keys with `c::set([$key => $value, ..])`. 🤓 | ||
|
||
**Please prefix every key with `broken-links.`!** | ||
|
||
key | default | description | ||
----------------- | ------- | ------------------------------------------------ | ||
`include-external` | `false` | Not only test for internal links but also for external ones.* | ||
|
||
\* **Not recommended yet because it only works synchonously with the page-load of your panel which will slow everything quite down a bit.** | ||
|
||
|
||
# Changelog | ||
|
||
Have a look at the [releases page](https://github.com/wottpal/kirby-anchor-headings/releases). | ||
|
||
|
||
# Roadmap | ||
|
||
- [ ] Do checks asynchronously | ||
- [ ] Make pages/links excludable | ||
- [ ] At the moment it only looks at fields with the name `text`. Make this user-definable per page. | ||
- [ ] Check for internet-connection (if external links are enabled) | ||
|
||
|
||
|
||
# 💰 # | ||
Just kidding. This plugin is totally free. Please consider following [me](https://twitter.com/wottpal) on Twitter if it saved your day. | ||
|
||
[![Twitter Follow](https://img.shields.io/twitter/follow/wottpal.svg?style=social&label=Follow)](https://twitter.com/wottpal) | ||
|
||
You can also check out one of [my other Kirby-plugins](https://wottpal.com/items/my-kirby-plugins): | ||
|
||
* [Lightbox-Gallery](https://github.com/wottpal/kirby-lightbox-gallery) - Easily inline beautifully aligned galleries with lightbox-support powered by PhotoSwipe. | ||
* [HTML5-Video Kirbytag](https://github.com/wottpal/kirby-video) - Adds a kirbytag for embedding HTML5-videos with a variety of features. | ||
* [Anchor-Headings](https://github.com/wottpal/kirby-anchor-headings) - A kirby field-method which enumerates heading-elements, generates IDs for anchor-links and inserts custom markup based on your needs. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
/** | ||
* A Kirby panel-widget which shows broken links within the pages. | ||
* | ||
* @package Kirby CMS | ||
* @author Dennis Kerzig <hi@wottpal.com> | ||
* @version 0.1.0 | ||
* | ||
*/ | ||
|
||
|
||
$kirby->set('widget', 'broken-links', __DIR__ . DS . 'broken-links'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
include_once __DIR__ . DS . '..' . DS . 'helpers.php'; | ||
|
||
|
||
return [ | ||
|
||
'title' => 'Broken Links', | ||
|
||
// 'options' => [ | ||
// [ | ||
// 'text' => 'Check now', | ||
// 'icon' => 'play-circle-o', | ||
// 'link' => '#imagekit-action-create', | ||
// ], | ||
// ], | ||
|
||
'html' => function () { | ||
$site = panel()->site(); | ||
$all_links = []; | ||
$broken_links = []; | ||
$regex = '/https?\:\/\/[^\" \n]+/i'; | ||
$base_url = $site->url(); | ||
|
||
// Gather Plugin-Options | ||
$include_external = c::get('broken-links.include-external', false); | ||
|
||
// Determine all links in all pages | ||
foreach($site->index() as $page) { | ||
if ($page->text()->isNotEmpty()) { | ||
$text = $page->text()->kt(); | ||
preg_match_all($regex, $text, $matches); | ||
|
||
// Determine internal & broken links | ||
foreach($matches[0] as $link) { | ||
$is_internal = substr($link, 0, strlen($base_url)) === $base_url; | ||
if (!$is_internal && !$include_external) continue; | ||
|
||
$is_broken = get_http_response_code($link) == '404'; | ||
if (!$is_broken) continue; | ||
|
||
// Remove Base-URL Prefix (internal) | ||
if ($is_internal) { | ||
$link = substr($link, strlen($base_url)); | ||
} | ||
|
||
$broken_links[$page->id()][] = $link; | ||
} | ||
} | ||
} | ||
|
||
// Populate the Widget-Template | ||
return tpl::load(__DIR__ . DS . 'template.php', [ | ||
'broken_links' => $broken_links, | ||
'has_broken_links' => !empty(array_filter($broken_links)) | ||
]); | ||
} | ||
|
||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php include_once __DIR__ . DS . '..' . DS . 'helpers.php'; ?> | ||
|
||
|
||
<style> | ||
.broken-link-list { | ||
margin-left: 3rem; | ||
font-size: .8em; | ||
padding-bottom: 1rem; | ||
font-style: italic; | ||
} | ||
.nav-list > li:last-of-type .broken-link-list { | ||
padding-bottom: 0; | ||
} | ||
</style> | ||
|
||
|
||
<?php if(!$has_broken_links): ?> | ||
<div class="dashboard-box" style="margin-top: 1rem;"> | ||
<div class="text">All internal links seem to be okay.</div> | ||
</div> | ||
|
||
<?php else: ?> | ||
|
||
<ul class="nav nav-list sidebar-list"> | ||
<?php foreach($broken_links as $page_id => $links): ?> | ||
<?php $page = panel()->page($page_id) ?> | ||
|
||
<li> | ||
<a href="<?= $page->url('edit') ?>"> | ||
<?= $page->icon() ?> | ||
<span><?= $page->title() ?></span> | ||
<small class="marginalia shiv shiv-left shiv-white" style="color: red; font-weight:bold;"><?= count($links) ?> <?= link_string(count($links)) ?></small> | ||
</a> | ||
|
||
<ul class="broken-link-list"> | ||
<?php foreach($links as $link): ?> | ||
<li><?= $link ?></li> | ||
<?php endforeach ?> | ||
</ul> | ||
|
||
</li> | ||
|
||
<?php endforeach ?> | ||
</ul> | ||
|
||
<!-- <div class="dashboard-box"> | ||
|
||
</div> --> | ||
|
||
<?php endif ?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
|
||
/** | ||
* Returns either singular or plural "Links" string. | ||
* TODO Has to be localized. | ||
*/ | ||
function link_string($count) { | ||
return $count == 1 ? "Link" : "Links"; | ||
} | ||
|
||
|
||
/** | ||
* Returns the HTTP-Response-Code of a given Domain. | ||
*/ | ||
function get_http_response_code($domain1) { | ||
$headers = get_headers($domain1); | ||
return substr($headers[0], 9, 3); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"name": "broken-links-widget", | ||
"description": "A Kirby panel-widget which shows broken links within the pages.", | ||
"author": "Dennis Kerzig <hi@wottpal.com> (https://wottpal.com)", | ||
"version": "0.1.0", | ||
"type": "kirby-plugin", | ||
"license": "MIT" | ||
} |