mirror of
https://github.com/openSUSE/osem.git
synced 2026-08-13 19:54:02 +00:00
Fix markdown editors bug #811 using asset gem.
This commit is contained in:
parent
b9469f6a17
commit
64b825cebe
7 changed files with 14 additions and 3095 deletions
4
Gemfile
4
Gemfile
|
|
@ -82,6 +82,10 @@ source 'https://rails-assets.org' do
|
|||
gem 'rails-assets-waypoints'
|
||||
# for displaying maps
|
||||
gem 'rails-assets-leaflet'
|
||||
# for markdown editors
|
||||
gem 'rails-assets-bootstrap-markdown'
|
||||
gem 'rails-assets-to-markdown'
|
||||
gem 'rails-assets-markdown'
|
||||
end
|
||||
|
||||
# as date picker
|
||||
|
|
|
|||
|
|
@ -331,16 +331,22 @@ GEM
|
|||
bundler (>= 1.3.0, < 2.0)
|
||||
railties (= 4.2.5.2)
|
||||
sprockets-rails
|
||||
rails-assets-bootstrap (3.3.6)
|
||||
rails-assets-jquery (>= 1.9.1, < 3)
|
||||
rails-assets-bootstrap-markdown (2.10.0)
|
||||
rails-assets-bootstrap (~> 3)
|
||||
rails-assets-date.format (1.2.3)
|
||||
rails-assets-holderjs (2.9.1)
|
||||
rails-assets-jquery (2.2.0)
|
||||
rails-assets-jquery-smooth-scroll (1.7.2)
|
||||
rails-assets-jquery (>= 1.4.2)
|
||||
rails-assets-leaflet (0.7.7)
|
||||
rails-assets-markdown (0.5.0)
|
||||
rails-assets-momentjs (2.11.2)
|
||||
rails-assets-spectrum (1.8.0)
|
||||
rails-assets-jquery (>= 1.7.2)
|
||||
rails-assets-tinycolor (1.3.0)
|
||||
rails-assets-to-markdown (1.3.0)
|
||||
rails-assets-trianglify (0.4.0)
|
||||
rails-assets-waypoints (4.0.0)
|
||||
rails-deprecated_sanitizer (1.0.3)
|
||||
|
|
@ -540,13 +546,16 @@ DEPENDENCIES
|
|||
prawn_rails
|
||||
quiet_assets
|
||||
rails (~> 4.2)
|
||||
rails-assets-bootstrap-markdown!
|
||||
rails-assets-date.format!
|
||||
rails-assets-holderjs!
|
||||
rails-assets-jquery-smooth-scroll!
|
||||
rails-assets-leaflet!
|
||||
rails-assets-markdown!
|
||||
rails-assets-momentjs!
|
||||
rails-assets-spectrum!
|
||||
rails-assets-tinycolor!
|
||||
rails-assets-to-markdown!
|
||||
rails-assets-trianglify!
|
||||
rails-assets-waypoints!
|
||||
rails-observers
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@
|
|||
*= require osem-splash
|
||||
*= require font-awesome
|
||||
*= require osem-fonts
|
||||
*= require bootstrap-markdown.min
|
||||
*= require bootstrap-markdown
|
||||
*= require bootstrap-datetimepicker
|
||||
*= require leaflet
|
||||
*= require bootstrap3-switch
|
||||
|
|
|
|||
|
|
@ -1 +0,0 @@
|
|||
.md-editor{display:block;border:1px solid #ddd}.md-editor>.md-header,.md-editor .md-footer{display:block;padding:6px 4px;background:#fff}.md-editor>.md-header{margin: 0;}.md-editor>.md-preview{background:#fff;border-top:1px dashed #ddd;border-bottom:1px dashed #ddd;min-height:10px;overflow:auto}.md-editor>textarea{font-family:Monaco,Menlo,Consolas,"Courier New",monospace;font-size:14px;outline:0;outline:thin dotted \9;margin:0;display:block;padding:0;width:100%;border:0;border-top:1px dashed #ddd;border-bottom:1px dashed #ddd;border-radius:0;box-shadow:none;background:#eee}.md-editor>textarea:focus{box-shadow:none;background:#fff}.md-editor.active{border-color:#66afe9;outline:0;-webkit-box-shadow:inset 0 1px 1px rgba(0,0,0,.075),0 0 8px rgba(102,175,233,0.6);box-shadow:inset 0 1px 1px rgba(0,0,0,.075),0 0 8px rgba(102,175,233,0.6)}
|
||||
1293
vendor/assets/javascripts/bootstrap-markdown.js
vendored
1293
vendor/assets/javascripts/bootstrap-markdown.js
vendored
File diff suppressed because it is too large
Load diff
1616
vendor/assets/javascripts/markdown.js
vendored
1616
vendor/assets/javascripts/markdown.js
vendored
File diff suppressed because it is too large
Load diff
184
vendor/assets/javascripts/to-markdown.js
vendored
184
vendor/assets/javascripts/to-markdown.js
vendored
|
|
@ -1,184 +0,0 @@
|
|||
/*
|
||||
* to-markdown - an HTML to Markdown converter
|
||||
*
|
||||
* Copyright 2011, Dom Christie
|
||||
* Licenced under the MIT licence
|
||||
*
|
||||
*/
|
||||
|
||||
var toMarkdown = function(string) {
|
||||
|
||||
var ELEMENTS = [
|
||||
{
|
||||
patterns: 'p',
|
||||
replacement: function(str, attrs, innerHTML) {
|
||||
return innerHTML ? '\n\n' + innerHTML + '\n' : '';
|
||||
}
|
||||
},
|
||||
{
|
||||
patterns: 'br',
|
||||
type: 'void',
|
||||
replacement: '\n'
|
||||
},
|
||||
{
|
||||
patterns: 'h([1-6])',
|
||||
replacement: function(str, hLevel, attrs, innerHTML) {
|
||||
var hPrefix = '';
|
||||
for(var i = 0; i < hLevel; i++) {
|
||||
hPrefix += '#';
|
||||
}
|
||||
return '\n\n' + hPrefix + ' ' + innerHTML + '\n';
|
||||
}
|
||||
},
|
||||
{
|
||||
patterns: 'hr',
|
||||
type: 'void',
|
||||
replacement: '\n\n* * *\n'
|
||||
},
|
||||
{
|
||||
patterns: 'a',
|
||||
replacement: function(str, attrs, innerHTML) {
|
||||
var href = attrs.match(attrRegExp('href')),
|
||||
title = attrs.match(attrRegExp('title'));
|
||||
return href ? '[' + innerHTML + ']' + '(' + href[1] + (title && title[1] ? ' "' + title[1] + '"' : '') + ')' : str;
|
||||
}
|
||||
},
|
||||
{
|
||||
patterns: ['b', 'strong'],
|
||||
replacement: function(str, attrs, innerHTML) {
|
||||
return innerHTML ? '**' + innerHTML + '**' : '';
|
||||
}
|
||||
},
|
||||
{
|
||||
patterns: ['i', 'em'],
|
||||
replacement: function(str, attrs, innerHTML) {
|
||||
return innerHTML ? '_' + innerHTML + '_' : '';
|
||||
}
|
||||
},
|
||||
{
|
||||
patterns: 'code',
|
||||
replacement: function(str, attrs, innerHTML) {
|
||||
return innerHTML ? '`' + innerHTML + '`' : '';
|
||||
}
|
||||
},
|
||||
{
|
||||
patterns: 'img',
|
||||
type: 'void',
|
||||
replacement: function(str, attrs, innerHTML) {
|
||||
var src = attrs.match(attrRegExp('src')),
|
||||
alt = attrs.match(attrRegExp('alt')),
|
||||
title = attrs.match(attrRegExp('title'));
|
||||
return '![' + (alt && alt[1] ? alt[1] : '') + ']' + '(' + src[1] + (title && title[1] ? ' "' + title[1] + '"' : '') + ')';
|
||||
}
|
||||
}
|
||||
];
|
||||
|
||||
for(var i = 0, len = ELEMENTS.length; i < len; i++) {
|
||||
if(typeof ELEMENTS[i].patterns === 'string') {
|
||||
string = replaceEls(string, { tag: ELEMENTS[i].patterns, replacement: ELEMENTS[i].replacement, type: ELEMENTS[i].type });
|
||||
}
|
||||
else {
|
||||
for(var j = 0, pLen = ELEMENTS[i].patterns.length; j < pLen; j++) {
|
||||
string = replaceEls(string, { tag: ELEMENTS[i].patterns[j], replacement: ELEMENTS[i].replacement, type: ELEMENTS[i].type });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function replaceEls(html, elProperties) {
|
||||
var pattern = elProperties.type === 'void' ? '<' + elProperties.tag + '\\b([^>]*)\\/?>' : '<' + elProperties.tag + '\\b([^>]*)>([\\s\\S]*?)<\\/' + elProperties.tag + '>',
|
||||
regex = new RegExp(pattern, 'gi'),
|
||||
markdown = '';
|
||||
if(typeof elProperties.replacement === 'string') {
|
||||
markdown = html.replace(regex, elProperties.replacement);
|
||||
}
|
||||
else {
|
||||
markdown = html.replace(regex, function(str, p1, p2, p3) {
|
||||
return elProperties.replacement.call(this, str, p1, p2, p3);
|
||||
});
|
||||
}
|
||||
return markdown;
|
||||
}
|
||||
|
||||
function attrRegExp(attr) {
|
||||
return new RegExp(attr + '\\s*=\\s*["\']?([^"\']*)["\']?', 'i');
|
||||
}
|
||||
|
||||
// Pre code blocks
|
||||
|
||||
string = string.replace(/<pre\b[^>]*>`([\s\S]*)`<\/pre>/gi, function(str, innerHTML) {
|
||||
innerHTML = innerHTML.replace(/^\t+/g, ' '); // convert tabs to spaces (you know it makes sense)
|
||||
innerHTML = innerHTML.replace(/\n/g, '\n ');
|
||||
return '\n\n ' + innerHTML + '\n';
|
||||
});
|
||||
|
||||
// Lists
|
||||
|
||||
// Escape numbers that could trigger an ol
|
||||
// If there are more than three spaces before the code, it would be in a pre tag
|
||||
// Make sure we are escaping the period not matching any character
|
||||
string = string.replace(/^(\s{0,3}\d+)\. /g, '$1\\. ');
|
||||
|
||||
// Converts lists that have no child lists (of same type) first, then works it's way up
|
||||
var noChildrenRegex = /<(ul|ol)\b[^>]*>(?:(?!<ul|<ol)[\s\S])*?<\/\1>/gi;
|
||||
while(string.match(noChildrenRegex)) {
|
||||
string = string.replace(noChildrenRegex, function(str) {
|
||||
return replaceLists(str);
|
||||
});
|
||||
}
|
||||
|
||||
function replaceLists(html) {
|
||||
|
||||
html = html.replace(/<(ul|ol)\b[^>]*>([\s\S]*?)<\/\1>/gi, function(str, listType, innerHTML) {
|
||||
var lis = innerHTML.split('</li>');
|
||||
lis.splice(lis.length - 1, 1);
|
||||
|
||||
for(i = 0, len = lis.length; i < len; i++) {
|
||||
if(lis[i]) {
|
||||
var prefix = (listType === 'ol') ? (i + 1) + ". " : "* ";
|
||||
lis[i] = lis[i].replace(/\s*<li[^>]*>([\s\S]*)/i, function(str, innerHTML) {
|
||||
|
||||
innerHTML = innerHTML.replace(/^\s+/, '');
|
||||
innerHTML = innerHTML.replace(/\n\n/g, '\n\n ');
|
||||
// indent nested lists
|
||||
innerHTML = innerHTML.replace(/\n([ ]*)+(\*|\d+\.) /g, '\n$1 $2 ');
|
||||
return prefix + innerHTML;
|
||||
});
|
||||
}
|
||||
}
|
||||
return lis.join('\n');
|
||||
});
|
||||
return '\n\n' + html.replace(/[ \t]+\n|\s+$/g, '');
|
||||
}
|
||||
|
||||
// Blockquotes
|
||||
var deepest = /<blockquote\b[^>]*>((?:(?!<blockquote)[\s\S])*?)<\/blockquote>/gi;
|
||||
while(string.match(deepest)) {
|
||||
string = string.replace(deepest, function(str) {
|
||||
return replaceBlockquotes(str);
|
||||
});
|
||||
}
|
||||
|
||||
function replaceBlockquotes(html) {
|
||||
html = html.replace(/<blockquote\b[^>]*>([\s\S]*?)<\/blockquote>/gi, function(str, inner) {
|
||||
inner = inner.replace(/^\s+|\s+$/g, '');
|
||||
inner = cleanUp(inner);
|
||||
inner = inner.replace(/^/gm, '> ');
|
||||
inner = inner.replace(/^(>([ \t]{2,}>)+)/gm, '> >');
|
||||
return inner;
|
||||
});
|
||||
return html;
|
||||
}
|
||||
|
||||
function cleanUp(string) {
|
||||
string = string.replace(/^[\t\r\n]+|[\t\r\n]+$/g, ''); // trim leading/trailing whitespace
|
||||
string = string.replace(/\n\s+\n/g, '\n\n');
|
||||
string = string.replace(/\n{3,}/g, '\n\n'); // limit consecutive linebreaks to 2
|
||||
return string;
|
||||
}
|
||||
|
||||
return cleanUp(string);
|
||||
};
|
||||
|
||||
if (typeof exports === 'object') {
|
||||
exports.toMarkdown = toMarkdown;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue