Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
coopwire-hypothesis
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
孙灵跃 Leon Sun
coopwire-hypothesis
Commits
ce766975
Commit
ce766975
authored
Nov 13, 2012
by
Randall Leeds
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #208 from csillag/develop
Markdown support Fix #91
parents
713c6a27
ae13c34b
Changes
3
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
1447 additions
and
8 deletions
+1447
-8
Markdown.Converter.js
h/js/lib/Markdown.Converter.js
+1332
-0
Markdown.Sanitizer.js
h/js/lib/Markdown.Sanitizer.js
+108
-0
hypothesis.coffee
h/js/src/hypothesis.coffee
+7
-8
No files found.
h/js/lib/Markdown.Converter.js
0 → 100644
View file @
ce766975
This diff is collapsed.
Click to expand it.
h/js/lib/Markdown.Sanitizer.js
0 → 100644
View file @
ce766975
(
function
()
{
var
output
,
Converter
;
if
(
typeof
exports
===
"object"
&&
typeof
require
===
"function"
)
{
// we're in a CommonJS (e.g. Node.js) module
output
=
exports
;
Converter
=
require
(
"./Markdown.Converter"
).
Converter
;
}
else
{
output
=
window
.
Markdown
;
Converter
=
output
.
Converter
;
}
output
.
getSanitizingConverter
=
function
()
{
var
converter
=
new
Converter
();
converter
.
hooks
.
chain
(
"postConversion"
,
sanitizeHtml
);
converter
.
hooks
.
chain
(
"postConversion"
,
balanceTags
);
return
converter
;
}
function
sanitizeHtml
(
html
)
{
return
html
.
replace
(
/<
[^
>
]
*>
?
/gi
,
sanitizeTag
);
}
// (tags that can be opened/closed) | (tags that stand alone)
var
basic_tag_whitelist
=
/^
(
<
\/?(
b|blockquote|code|del|dd|dl|dt|em|h1|h2|h3|i|kbd|li|ol|p|pre|s|sup|sub|strong|strike|ul
)
>|<
(
br|hr
)\s?\/?
>
)
$/i
;
// <a href="url..." optional title>|</a>
var
a_white
=
/^
(
<a
\s
href="
((
https
?
|ftp
)
:
\/\/
|
\/)[
-A-Za-z0-9+&@#
\/
%?=~_|!:,.;
\(\)]
+"
(\s
title="
[^
"<>
]
+"
)?\s?
>|<
\/
a>
)
$/i
;
// <img src="url..." optional width optional height optional alt optional title
var
img_white
=
/^
(
<img
\s
src="
(
https
?
:
\/\/
|
\/)[
-A-Za-z0-9+&@#
\/
%?=~_|!:,.;
\(\)]
+"
(\s
width="
\d{1,3}
"
)?(\s
height="
\d{1,3}
"
)?(\s
alt="
[^
"<>
]
*"
)?(\s
title="
[^
"<>
]
*"
)?\s?\/?
>
)
$/i
;
function
sanitizeTag
(
tag
)
{
if
(
tag
.
match
(
basic_tag_whitelist
)
||
tag
.
match
(
a_white
)
||
tag
.
match
(
img_white
))
return
tag
;
else
return
""
;
}
/// <summary>
/// attempt to balance HTML tags in the html string
/// by removing any unmatched opening or closing tags
/// IMPORTANT: we *assume* HTML has *already* been
/// sanitized and is safe/sane before balancing!
///
/// adapted from CODESNIPPET: A8591DBA-D1D3-11DE-947C-BA5556D89593
/// </summary>
function
balanceTags
(
html
)
{
if
(
html
==
""
)
return
""
;
var
re
=
/<
\/?\w
+
[^
>
]
*
(\s
|$|>
)
/g
;
// convert everything to lower case; this makes
// our case insensitive comparisons easier
var
tags
=
html
.
toLowerCase
().
match
(
re
);
// no HTML tags present? nothing to do; exit now
var
tagcount
=
(
tags
||
[]).
length
;
if
(
tagcount
==
0
)
return
html
;
var
tagname
,
tag
;
var
ignoredtags
=
"<p><img><br><li><hr>"
;
var
match
;
var
tagpaired
=
[];
var
tagremove
=
[];
var
needsRemoval
=
false
;
// loop through matched tags in forward order
for
(
var
ctag
=
0
;
ctag
<
tagcount
;
ctag
++
)
{
tagname
=
tags
[
ctag
].
replace
(
/<
\/?(\w
+
)
.*/
,
"$1"
);
// skip any already paired tags
// and skip tags in our ignore list; assume they're self-closed
if
(
tagpaired
[
ctag
]
||
ignoredtags
.
search
(
"<"
+
tagname
+
">"
)
>
-
1
)
continue
;
tag
=
tags
[
ctag
];
match
=
-
1
;
if
(
!
/^<
\/
/
.
test
(
tag
))
{
// this is an opening tag
// search forwards (next tags), look for closing tags
for
(
var
ntag
=
ctag
+
1
;
ntag
<
tagcount
;
ntag
++
)
{
if
(
!
tagpaired
[
ntag
]
&&
tags
[
ntag
]
==
"</"
+
tagname
+
">"
)
{
match
=
ntag
;
break
;
}
}
}
if
(
match
==
-
1
)
needsRemoval
=
tagremove
[
ctag
]
=
true
;
// mark for removal
else
tagpaired
[
match
]
=
true
;
// mark paired
}
if
(
!
needsRemoval
)
return
html
;
// delete all orphaned tags from the string
var
ctag
=
0
;
html
=
html
.
replace
(
re
,
function
(
match
)
{
var
res
=
tagremove
[
ctag
]
?
""
:
match
;
ctag
++
;
return
res
;
});
return
html
;
}
})();
h/js/src/hypothesis.coffee
View file @
ce766975
...
...
@@ -66,6 +66,12 @@ class Hypothesis extends Annotator
getMaxBottom
:
{}
scrollTop
:
{}
# Prepare a MarkDown renderer, and add some post-processing
# so that all created links have their target set to _blank
@
renderer
=
Markdown
.
getSanitizingConverter
()
@
renderer
.
hooks
.
chain
"postConversion"
,
(
text
)
->
text
.
replace
/<a href=/
,
"<a target=
\"
_blank
\"
href="
super
# Load plugins
...
...
@@ -557,13 +563,6 @@ class Hypothesis extends Annotator
annotation
.
id
renderAnnotation
:
(
annotation
)
->
text
=
annotation
.
text
#Must do escaping manually, since we will need to disable Handlebar's autoamtic escaping,
# so that it leaves the inserted links intact
safe_text
=
Handlebars
.
Utils
.
escapeExpression
(
text
)
rendered_text
=
safe_text
.
replace
/(https?:\/\/[^\s]+)/g
,
(
match
)
->
"<a target=
\"
_blank
\"
href=
\"
#{
match
}
\"
>
#{
match
}
</a>"
annotation
.
rendered_text
=
rendered_text
annotation
.
rendered_text
=
@
renderer
.
makeHtml
(
annotation
.
text
)
window
.
Hypothesis
=
Hypothesis
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment