This is an old revision of the document!
Table of Contents
anchor Plugin
Compatible with DokuWiki
2010-11-07, 2011-05-25, angua, weatherwax, binky, ponder stibbons, hrun, detritus, frusterick
The missing download url means that this extension cannot be installed via the Extension Manager. Please see Publishing a Plugin on dokuwiki.org. Recommended are public repository hosts like GitHub, GitLab or Bitbucket.
This extension has not been updated in over 2 years. It may no longer be maintained or supported and may have compatibility issues.
Normally, you can only link to a wiki page, or a specific section header. This is impractical when header names are long, or you expect that they might change. This plugin lets you create links to any part of your page.
This plugin is also a useful sample, if you're having trouble figuring out how to create a syntax plugin.
Installation
This plugin is so simple, and I'm so lazy, that I'm just going to include the source code. The following text should be in the file lib/plugins/anchor/syntax.php
- lib/plugins/anchor/syntax.php
<?php if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/'); if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/'); require_once(DOKU_PLUGIN.'syntax.php'); class syntax_plugin_anchor extends DokuWiki_Syntax_Plugin { function getType() {return 'substition';} function getPType() {return 'block';} // Alternatively: return 'normal'; if you don't want new paragraph. function getSort() {return 167;} function getInfo() {return array('author' => 'Eli Fenton', 'name' => 'Anchor Plugin', 'url' => 'http://dokuwiki.org/plugin:anchor');} function connectTo($mode){ $this->Lexer->addSpecialPattern('\{\{anchor:[^}]*\}\}', $mode, 'plugin_anchor'); } function handle($match, $state, $pos, Doku_Handler $handler) { return explode(':', substr($match, strlen('{{anchor:'), -2)); } function render($mode, Doku_Renderer $renderer, $data) { $renderer->doc .= '<a name="' .htmlspecialchars($data[0]) . '">' .htmlspecialchars($data[1]) . '</a>'; } } ?>
plugin.info.txt
- plugin.info.txt
base anchor author Eli Fenton date 2013-08-16 name anchor desc Add HTML anchors to a page. url https://www.dokuwiki.org/plugin:anchor
Examples/Usage
Here's how you create an anchor:
{{anchor:anchor_name:text}}
The anchor_name part is the name of the anchor to use in links. The text part is optional, and, for the most part, not very useful. It lets you wrap the anchor around a specific piece of text.
To link to an anchor from within DokuWiki:
[[namespace:page_name#anchor_name]]
To link to an anchor from outside DokuWiki:
http://server.com/doku/doku.php?id=page_name#anchor_name
Changelog
- 2017-06-20 Changed signature of methods
handle
andrender
to suppress warnings in PHP 7.1. - 2013-05-23 Integrated changes made by Peter B to fix escaping vulnerability and paragraph formatting.
Discussion
Case sensitivity is not compatible with DW
While using anchor I found that you have to type the anchor_name in all lowercase letters to be compatible with the DW internal link mechanics, which converts the letters of a link to lowercase. To enable copy&paste of anchor names from other text-parts, it would be more convenient if the plugin did that itself.
Avoiding that Extension Managers erroneously thinks there is an update for this plugin
If you install the plugin from the source code given above, the Extension Manager of Dokuwiki (I'm using release 2014-05-05 “Ponder Stibbons”) erroneously thinks that there is an update of the plugin available. The reason is that the plugin code does not provide a version data for comparison.
You can fix this by installing the following file in the lib/plugins/anchor
directory:
- plugin.info.txt
base anchor author Eli Fenton date 2013-05-23 name Anchor Plugin url http://dokuwiki.org/plugin:anchor
Compatible with "Frusterick Manners"
IMO: Works fine with Release 2017-02-19 “Frusterick Manners”
Sig
Compatible with "Anteater"
In my opinion, it works fine with Anteater
AlekS
Compatible with "Angua"
Works also with “Angua” — aleks 2012/01/10 09:48
Compatible with "Elenor Of Tsort"
Works fine with 2016-06-26 “Elenor Of Tsort” — Mr. Magpie 2016/09/14
Jumps to top of the page
I put an anchor at the bottom of a page.
If I use this internal link, DW jumps to the top of the page.
Isn't it possible to jump to the part of the page, where the anchor is defined?27.08.2013, Joe
/exe/fetch...
I created the plugin files and tried to create a link.
Instead of creating an anchor is makes a link to /lib/exe/fetch.php?media=anchor:anchor_name:text
What am I doing wrong?
EDIT: My bad, I was using the syntax incorrectly! :) Great work!
2013/11/03/ 01:56 Stephen V
Abbreviated use in page
Just to tell that it works fine and mostly to tell that even the short notation for the anchor works.
This makes the anchors within the page independent of the source page.
example: [[#anchorName|text]]
Great!!
ViForEver 20140207
Hello, in my private wiki it does not work. How i have to create an anchor and an anchor-goal? Please give me an example!