-
Notifications
You must be signed in to change notification settings - Fork 28
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
Keith Robertson
committed
Jun 9, 2017
1 parent
e28f241
commit af47cfe
Showing
22 changed files
with
435 additions
and
1 deletion.
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,2 @@ | ||
/staging/ | ||
/web-ext-artifacts/ |
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,6 @@ | ||
[submodule "lib/highlightjs"] | ||
path = lib/highlightjs | ||
url = https://github.com/components/highlightjs | ||
[submodule "lib/markdown-it"] | ||
path = lib/markdown-it | ||
url = https://github.com/markdown-it/markdown-it |
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 |
---|---|---|
@@ -1,2 +1,12 @@ | ||
# markdown-viewer | ||
Markdown (.md) file viewer WebExtension for your browser. | ||
Markdown (.md) file viewer [WebExtension](https://developer.mozilla.org/en-US/Add-ons/WebExtensions) for your browser. | ||
|
||
## Build | ||
* Run `build.bat` (Windows) or `build.sh` (Linux) | ||
* Uses [web-ext](https://github.com/mozilla/web-ext/) which requires [nodejs](https://nodejs.org/) with npm. | ||
|
||
Firefox won't install the generated .zip unless it's signed by Mozilla; you have to test from the staging folder. | ||
|
||
## Testing | ||
* Navigate to the staging folder and run `web-ext run`. | ||
* Or install `staging/manifest.json` [temporarily in Firefox](https://developer.mozilla.org/en-US/Add-ons/WebExtensions/Temporary_Installation_in_Firefox). |
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,24 @@ | ||
|
||
@rmdir /s /q staging 2>nul | ||
@rmdir /s /q web-ext-artifacts 2>nul | ||
@for %%f in ( | ||
LICENSE | ||
manifest.json | ||
ext\*.* | ||
lib\highlightjs\LICENSE | ||
lib\highlightjs\highlight.pack.min.js | ||
lib\highlightjs\styles\default.css | ||
lib\markdown-it\LICENSE | ||
lib\markdown-it\dist\markdown-it.min.js | ||
lib\sss\LICENSE.md | ||
lib\sss\sss.css | ||
lib\sss\sss.print.css | ||
) do @call :copyfile %%f staging\%%f | ||
|
||
@web-ext build -s staging | ||
@goto :EOF | ||
|
||
:copyfile | ||
@mkdir %~dp2 2>nul | ||
@copy %1 %2 >nul | ||
@exit /b |
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,20 @@ | ||
#!/bin/sh | ||
rm -r staging 2>/dev/null | ||
rm -r web-ext-artifacts 2>/dev/null | ||
for f in LICENSE \ | ||
manifest.json \ | ||
ext/* \ | ||
lib/highlightjs/LICENSE \ | ||
lib/highlightjs/highlight.pack.min.js \ | ||
lib/highlightjs/styles/default.css \ | ||
lib/markdown-it/LICENSE \ | ||
lib/markdown-it/dist/markdown-it.min.js \ | ||
lib/sss/LICENSE.md \ | ||
lib/sss/sss.css \ | ||
lib/sss/sss.print.css | ||
do | ||
mkdir -p `dirname staging/$f` | ||
cp $f staging/$f | ||
done | ||
|
||
web-ext build -s staging |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,9 @@ | ||
=== Original code: i.e. some of ext/content.js | ||
|
||
Copyright (c) 2012-2013 Rousseau Thibaut | ||
|
||
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,20 @@ | ||
browser.tabs.onUpdated.addListener((id, changeInfo, tab) => { | ||
|
||
const markdownFileExtension = /\.m(arkdown|kdn?|d(o?wn)?)(\?.*)?(#.*)?$/i; | ||
|
||
if (changeInfo.status == 'complete' && markdownFileExtension.test(tab.url)) { | ||
|
||
// Support loading additional scripts on demand by content script. | ||
browser.runtime.onMessage.addListener((message, sender, sendResponse) => { | ||
if (message.scriptToInject) { | ||
browser.tabs.executeScript(sender.tab.id, { file: message.scriptToInject }, () => { | ||
sendResponse({ success: true }); | ||
}); | ||
return true; | ||
} | ||
return false; | ||
}); | ||
|
||
browser.tabs.executeScript(id, { file: "ext/content.js" }); | ||
} | ||
}); |
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,106 @@ | ||
function addStylesheet(path, media) { | ||
var style = document.createElement('link'); | ||
style.rel = 'stylesheet'; | ||
if (media) { style.setAttribute('media', media); } | ||
style.type = 'text/css'; | ||
style.href = browser.extension.getURL(path); | ||
document.head.appendChild(style); | ||
} | ||
|
||
function processMarkdown(textContent) { | ||
// Parse the content Markdown => HTML | ||
var md = markdownit({ | ||
html: true, | ||
linkify: true, | ||
// Shameless copypasta https://github.com/markdown-it/markdown-it#syntax-highlighting | ||
highlight: function (str, lang) { | ||
if (lang && hljs.getLanguage(lang)) { | ||
try { | ||
return hljs.highlight(lang, str).value; | ||
} catch (__) {} | ||
} | ||
|
||
try { | ||
return hljs.highlightAuto(str).value; | ||
} catch (__) {} | ||
|
||
return ''; // use external default escaping | ||
} | ||
}); | ||
|
||
var html = md.render(textContent); | ||
|
||
// Style the page and code highlights. | ||
addStylesheet('lib/sss/sss.css'); | ||
addStylesheet('lib/sss/sss.print.css', 'print'); | ||
addStylesheet('lib/highlightjs/styles/default.css'); | ||
|
||
// This is considered a good practice for mobiles. | ||
var viewport = document.createElement('meta'); | ||
viewport.name = 'viewport'; | ||
viewport.content = 'width=device-width, initial-scale=1'; | ||
document.head.appendChild(viewport); | ||
|
||
// Insert html for the markdown into an element for processing. | ||
var markdownRoot = document.createElement('div'); | ||
markdownRoot.className = "markdownRoot"; | ||
markdownRoot.innerHTML = html; | ||
|
||
// Trample out script elements. | ||
markdownRoot.querySelectorAll('script').forEach(each => { | ||
each.innerText = ''; | ||
each.src = ''; | ||
}); | ||
// Remove hrefs that don't look like URLs. | ||
const likeUrl = /^[-a-z]*:\/\//i; | ||
markdownRoot.querySelectorAll('[href]').forEach(each => { | ||
if (!likeUrl.test(each.href)) { | ||
each.href = ''; | ||
} | ||
}); | ||
// Remove event handlers. (Others?) | ||
var events = ['onclick', 'onload', 'onmouseover', 'onmouseout']; | ||
var eventsJoined = '[' + events.join('],[') + ']'; | ||
markdownRoot.querySelectorAll(eventsJoined).forEach(each => { | ||
events.forEach(attr => { | ||
if (each.getAttribute(attr)) { each.setAttribute(attr, null); } | ||
}); | ||
}); | ||
|
||
// Set the page title. | ||
var title = markdownRoot.querySelector('h1, h2, h3, h4, h5, h6'); // First header | ||
if (title) { | ||
title = title.textContent.trim(); | ||
} else { | ||
title = markdownRoot.textContent.trim().split("\n", 1)[0].trim(); // First line | ||
} | ||
if (title.length > 50) { | ||
title = title.substr(0, 50) + "..."; | ||
} | ||
document.title = title; | ||
|
||
// Finally insert the markdown. | ||
document.body.appendChild(markdownRoot); | ||
} | ||
|
||
function loadScriptThen(path, nextStep) { | ||
browser.runtime.sendMessage({ scriptToInject: path }, (response) => { | ||
if (response.success) { nextStep(); } | ||
}); | ||
} | ||
|
||
// Execute only if .md is unprocessed text. | ||
var body = document.body; | ||
if (body.childNodes.length == 1 && | ||
body.children.length == 1 && | ||
body.children[0].nodeName.toUpperCase() == 'PRE') | ||
{ | ||
var textContent = body.textContent; | ||
body.textContent = ''; | ||
|
||
loadScriptThen('lib/highlightjs/highlight.pack.min.js', () => { | ||
loadScriptThen('lib/markdown-it/dist/markdown-it.min.js', () => { | ||
processMarkdown(textContent); | ||
}) | ||
}); | ||
} |
Submodule highlightjs
added at
c81db4
Submodule markdown-it
added at
ff643c
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 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2016 | ||
|
||
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,89 @@ | ||
body { | ||
color: #333; | ||
font-family: 'Segoe UI', 'Lucida Grande', Helvetica, sans-serif; | ||
line-height: 1.5; | ||
margin: 2em; | ||
//max-width: 800px; // Ick | ||
} | ||
|
||
h1, h2, h3, h4, h5, h6 { | ||
font-weight: normal; | ||
line-height: 1em; | ||
margin: 20px 0; | ||
} | ||
h1 { | ||
font-size: 2.25em; | ||
} | ||
h2 { | ||
font-size: 1.75em; | ||
} | ||
h3 { | ||
font-size: 1.5em; | ||
} | ||
h4, h5, h6 { | ||
font-size: 1.25em; | ||
} | ||
|
||
a { | ||
color: #08C; | ||
text-decoration: none; | ||
} | ||
a:hover, a:focus { | ||
text-decoration: underline; | ||
} | ||
a:visited { | ||
color: #058; | ||
} | ||
|
||
img { | ||
max-width: 100%; | ||
} | ||
|
||
li + li { | ||
margin-top: 3px; | ||
} | ||
dt { | ||
font-weight: bold; | ||
} | ||
|
||
code { | ||
background: #EEE; | ||
font-family: "Consolas", "Lucida Console", monospace; | ||
padding: 1px 5px; | ||
} | ||
pre { | ||
background: #EEE; | ||
padding: 5px 10px; | ||
white-space: pre-wrap; | ||
} | ||
pre code { | ||
padding: 0; | ||
} | ||
|
||
blockquote { | ||
border-left: 5px solid #EEE; | ||
margin: 0; | ||
padding: 0 10px; | ||
} | ||
|
||
table { | ||
border-collapse: collapse; | ||
width: 100%; | ||
} | ||
table + table { | ||
margin-top: 1em; | ||
} | ||
thead { | ||
background: #EEE; | ||
text-align: left; | ||
} | ||
th, td { | ||
border: 1px solid #EEE; | ||
padding: 5px 10px; | ||
} | ||
|
||
hr { | ||
background: #EEE; | ||
border: 0; | ||
height: 1px; | ||
} |
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,18 @@ | ||
body { | ||
color: #000; | ||
font: 12pt serif; | ||
margin: 0; | ||
max-width: 100%; | ||
} | ||
|
||
a { | ||
color: #666; | ||
text-decoration: underline; | ||
} | ||
a[href]:after { | ||
content: ' (' attr(href) ')'; | ||
} | ||
|
||
blockquote, ul, dl { | ||
page-break-inside: avoid; | ||
} |
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,28 @@ | ||
{ | ||
"manifest_version": 2, | ||
"name": "markdown-viewer", | ||
"version": "1.0", | ||
"description": "Displays markdown documents beautified.", | ||
"homepage_url": "https://github.com/KeithLRobertson/markdown-viewer", | ||
|
||
"permissions": [ | ||
"activeTab", "tabs", "<all_urls>" | ||
], | ||
|
||
"icons": { | ||
"32": "ext/32x20.png", | ||
"48": "ext/48x30.png", | ||
"66": "ext/66x40.png", | ||
"208": "ext/208x128.png" | ||
}, | ||
|
||
"background": { | ||
"scripts": ["ext/background.js"] | ||
}, | ||
|
||
"web_accessible_resources": [ | ||
"lib/sss/sss.css", | ||
"lib/sss/sss.print.css", | ||
"lib/highlightjs/styles/default.css" | ||
] | ||
} |
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,7 @@ | ||
Copyright (c) 2012-2013 Rousseau Thibaut | ||
|
||
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 @@ | ||
# Hello World! |
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 @@ | ||
# Hello Sub-World! |
Oops, something went wrong.