Skip to content
This repository has been archived by the owner on Apr 4, 2019. It is now read-only.

modify relative link #94

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 8 additions & 22 deletions chrome/content/markdown-viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,37 +130,23 @@ if (!MarkdownViewer) {

// Restore the links
var links = document.getElementsByTagName('a');

// Files in the same folder: "fileNameRegardlessOfExtension",
// "./fileNameRegardlessOfExtension", "." or "./". Allow any #hash-part, too.
// Also, allow for relative (#hash-part-only) URLs within the same document.
var linkInTheSameFolderWithHash = /^(\.|(\.\/)?[^#/\\]*)(#.*)?$/;

// Like linkInTheSameFolderWithHash, but without extension and exluding any #hash-part.
var linkWithNoExtension = /^(\.|(\.\/)?[^#/\\.]*)$/;
var relativeLinkWithHash = /^(\/?(?:[^#/]+\/)*(?:[^#/.]+(\.[^#/.]*)*))(#.*)?$/;

for (var i=0; i < links.length; i++) {
var link = links[i];
if (!link.getAttribute('href')) {
var originalHref = link.getAttribute('data-original-href');

// linkInTheSameFolderWithHash also matches an empty string, so check that URL is non-empty
if (originalHref === './'){
originalHref = './index.md';
}
if (originalHref !== '') {
// Sanitize: Only allow links in the same folder.
var match = linkInTheSameFolderWithHash.exec(originalHref);
var match = relativeLinkWithHash.exec(originalHref);
if (match) {
var url = match[1];

// Handling links with no extension, as used on Github pages:
// replacing PageName with PageName.md. Also, replacing ./ with ./index.md.
// If url is empty, then it's a #hash-part-only relative link.
if (url !== '' && linkWithNoExtension.test(url)) {
if (url === '.' || url === './') {
url = 'index';
}
url += '.md';
if(!match[2]){
url += ".md";
}
if (match[3]) {
if(match[3]){
url += match[3];
}
link.setAttribute('href', url);
Expand Down
2 changes: 1 addition & 1 deletion install.rdf
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<RDF xmlns="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:em="http://www.mozilla.org/2004/em-rdf#">
<Description about="urn:mozilla:install-manifest">
<em:id>[email protected]</em:id>
<em:version>1.12</em:version>
<em:version>1.13</em:version>
<em:type>2</em:type>

<em:name>Markdown Viewer</em:name>
Expand Down