Skip to content

Commit

Permalink
Fail on false + preserving HTML blocks as single sections (#49)
Browse files Browse the repository at this point in the history
* new test to fail on false

* should fail on false

* added divideIntoSections

* fixes

* fixes

* some progress

* fixes!

* bump to 0.4.0

* fixed test

* @rexagod adjustments

* fixes

* added fail()

* fixed mostly, need to deal with edge cases

* working for all but need to adjust other tests to ignore empty sections

* all fixed

* version bump
  • Loading branch information
jywarren committed Feb 27, 2019
1 parent 0056cab commit e2b0c19
Show file tree
Hide file tree
Showing 10 changed files with 133 additions and 32 deletions.
49 changes: 38 additions & 11 deletions dist/inlineMarkdownEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -10227,6 +10227,28 @@ module.exports = function defaultMarkdown(element) {
}

},{"megamark":91}],96:[function(require,module,exports){
// split by double-newline, but preserve HTML blocks as single sections:
module.exports = function divideIntoSections(content) {
var sections = [];
content = content.replace(/[\n]{2,}/g, "\n\n"); // manage double newlines (not sure exactly?)
chunkArray = content.replace(/(<(.+).*>.+<\/\2>)/g,"@@@@@@$1").split(/@@@@@@|\n\n/);

chunkArray = chunkArray.filter(function (x) {
return (x !== undefined && x !== '' && x.match(/\S/) !== null); // probably overzealous filtering of "empty" sections
});

chunkArray.forEach(function(chunk) {
if (chunk.match(/<\w>/)) {
sections.push(chunk); // it's an HTML chunk
} else {
sections = sections.concat(chunk.split("\n\n")); // split by double newline and add
}
});

return sections;
}

},{}],97:[function(require,module,exports){
inlineMarkdownEditor = function inlineMarkdownEditor(o) {
o.defaultMarkdown = o.defaultMarkdown || require('./defaultMarkdown.js');
o.buildSectionForm = o.buildSectionForm || require('./buildSectionForm.js');
Expand All @@ -10239,7 +10261,8 @@ inlineMarkdownEditor = function inlineMarkdownEditor(o) {
o.originalMarkdown = el.html();
o.preProcessor = o.preProcessor || function(m) { return m; }
// split by double-newline:
var sections = o.originalMarkdown.replace(/[\n]{2,}/g, "\n\n").split("\n\n");
o.divideIntoSections = o.divideIntoSections || require('./divideIntoSections.js');
var sections = o.divideIntoSections(o.originalMarkdown);
var editableSections = [];
// we also do this inside processSection, but independently track here:
sections.forEach(function forEachSection(section, index) {
Expand All @@ -10258,7 +10281,7 @@ inlineMarkdownEditor = function inlineMarkdownEditor(o) {
}
module.exports = inlineMarkdownEditor;

},{"./buildSectionForm.js":94,"./defaultMarkdown.js":95,"./insertEditLink.js":97,"./isEditable.js":98,"./onComplete.js":99,"./onFail.js":100,"./processSections.js":102}],97:[function(require,module,exports){
},{"./buildSectionForm.js":94,"./defaultMarkdown.js":95,"./divideIntoSections.js":96,"./insertEditLink.js":98,"./isEditable.js":99,"./onComplete.js":100,"./onFail.js":101,"./processSections.js":103}],98:[function(require,module,exports){
module.exports = function insertEditLink(uniqueId, el, form, onEdit, editor, o) {
var editBtns = "";
editBtns += "<span class='inline-edit-btns inline-edit-btns-" + uniqueId + "'>";
Expand All @@ -10283,11 +10306,11 @@ module.exports = function insertEditLink(uniqueId, el, form, onEdit, editor, o)
});
}

},{}],98:[function(require,module,exports){
},{}],99:[function(require,module,exports){
module.exports = function isEditable(markdown, originalMarkdown) {
originalMarkdown = originalMarkdown || markdown; // optional parameter for checking against original complete text
// filter? Only p,h1-5,ul?
var editable = markdown.match(/</) === null; // has tags; exclueds HTML
var editable = markdown.match(/</) === null; // has tags; excludes HTML
editable = editable && markdown.match(/\*\*\*\*/) === null; // no horizontal rules: ****
editable = editable && markdown.match(/\-\-\-\-/) === null; // no horizontal rules: ----
editable = editable && markdown !== ''; // no blanks
Expand All @@ -10296,7 +10319,7 @@ module.exports = function isEditable(markdown, originalMarkdown) {
return editable;
}

},{}],99:[function(require,module,exports){
},{}],100:[function(require,module,exports){
module.exports = function onComplete(response, markdown, html, el, uniqueId, form, o) {
var message = form.find('.section-message');
if (response === 200) {
Expand All @@ -10314,13 +10337,13 @@ module.exports = function onComplete(response, markdown, html, el, uniqueId, for
}
}

},{}],100:[function(require,module,exports){
},{}],101:[function(require,module,exports){
module.exports = function onFail(response, uniqueId) {
var message = $('#' + uniqueId + ' .section-message');
message.html('There was an error -- the wiki page may have changed while you were editing; save your content in the clipboard and try refreshing the page.');
}

},{}],101:[function(require,module,exports){
},{}],102:[function(require,module,exports){
module.exports = function processSection(markdown, o) {
var html,
randomNum = parseInt(Math.random() * 10000),
Expand Down Expand Up @@ -10381,8 +10404,12 @@ module.exports = function processSection(markdown, o) {
after: after // encodeURI(after)
})
.done(function onComplete(result, success, xhr) {
// we should need fewer things here:
o.onComplete(xhr.status, after, html, _el, uniqueId, __form, o);
if (result == "false") {
o.onFail(result, uniqueId);
} else {
// we should need fewer things here:
o.onComplete(xhr.status, after, html, _el, uniqueId, __form, o);
}
}).fail(function onFail(response) {
o.onFail(response, uniqueId);
}); // these don't work?
Expand All @@ -10392,12 +10419,12 @@ module.exports = function processSection(markdown, o) {
}
}

},{}],102:[function(require,module,exports){
},{}],103:[function(require,module,exports){
module.exports = function processSections(sections, o) {
sections.forEach(function(markdown) {
processSection = require('./processSection.js');
processSection(markdown, o);
});
}

},{"./processSection.js":101}]},{},[96]);
},{"./processSection.js":102}]},{},[97]);
10 changes: 9 additions & 1 deletion examples/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,15 @@ <h3>
* this
* is
* a
* list</div>
* list

<p>
<input type="text" />
</p>

(above, raw HTML blocks should be undisturbed)

</div>

<script>

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "inline-markdown-editor",
"version": "0.3.4",
"version": "0.5.0",
"description": "An inline wysiwyg markdown document editor based on replacing string subsections. WYSIWYG possible via woofmark.",
"main": "dist/inlineMarkdownEditor.js",
"scripts": {
Expand Down
20 changes: 9 additions & 11 deletions spec/javascripts/editor_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,21 @@ describe("Editor", function() {
});

it("generates the right number of sections", function() {
expect(editor.sections.length).toBe(8);
expect($('.inline-section').length).toBe(8);
expect(editor.sections[0]).toBe('');
expect(editor.sections.length).toBe(6);
expect($('.inline-section').length).toBe(6);
expect(editor.sections[0]).toBe('### Hello, World');
expect(editor.editableSections.length).toBe(5);
expect($('.inline-edit-form').length).toBe(5);
expect($('.inline-edit-form textarea').length).toBe(5);
});

it("correctly splits up sections", function() {
expect(editor.sections[0]).toBe("");
expect(editor.sections[1]).toBe("### Hello, World");
expect(editor.sections[2]).toBe("This is a paragraph.");
expect(editor.sections[3]).toBe("* this\n* is\n* a\n* list");
expect(editor.sections[4]).toBe("****");
expect(editor.sections[5]).toBe("### Hello again, World");
expect(editor.sections[6]).toBe("### Hello finally");
expect(editor.sections[7]).toBe("");
expect(editor.sections[0]).toBe("### Hello, World");
expect(editor.sections[1]).toBe("This is a paragraph.");
expect(editor.sections[2]).toBe("* this\n* is\n* a\n* list");
expect(editor.sections[3]).toBe("****");
expect(editor.sections[4]).toBe("### Hello again, World");
expect(editor.sections[5]).toBe("### Hello finally");
});

});
31 changes: 27 additions & 4 deletions spec/javascripts/error_handling_spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
describe('error handling by onFail', function () {

var editor, html = "A simple text line";

beforeEach(function() {
Expand All @@ -9,11 +9,14 @@ describe('error handling by onFail', function () {
replaceUrl: '/wiki/replace/',
selector: '.markdown'
});
jasmine.Ajax.install();
});

it("should call onFail", function(){
jasmine.Ajax.install();
afterEach(function() {
jasmine.Ajax.uninstall();
});

it("should call onFail", function(){
spyOn(editor.options, "onComplete");
spyOn(editor.options, "onFail");

Expand All @@ -30,5 +33,25 @@ describe('error handling by onFail', function () {
expect(editor.options.onComplete).not.toHaveBeenCalled();
expect(editor.options.onFail).toHaveBeenCalled();
});

// we are seeing response of "false" when plots2 fails to perform a replacement; adjusting here to read that as a failure
it("should correctly report 'false' string response as a failure, even with 200 response code", function(){
spyOn(editor.options, "onComplete");
spyOn(editor.options, "onFail");

spyOn($, "post").and.callFake(function(options) {
//here options is /wiki/replace/
var d = $.Deferred();
d.resolve("false");
return d.promise();
});

$('.inline-edit-btn').click(); // generate editor by clicking the pencil icon
$('.inline-edit-form button.submit').click(); //click the save button in that form to send the post request

// here, because of src/processSection.js#61, onFail should be called from within onComplete when it detects a response of "false"
expect(editor.options.onComplete).not.toHaveBeenCalled();
expect(editor.options.onFail).toHaveBeenCalled();
});

})
})
20 changes: 20 additions & 0 deletions spec/javascripts/replacement_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,26 @@ describe("Replacement functions", function() {
expect($('.inline-edit-form textarea').length).toBe(1);
});

it("it correctly splits up mixed HTML and markdown into sections", function(done) {
fixture = loadFixtures('index.html');
var html = "<div>lala</div>\n\nhey<table class='hey'><p><table></table></p></table>\n\n## Markdown\n\n<p>Hi there</p>\n\n* One\n* Two\n\n<table class='hey'><p><table></table></p></table>\n\nSo <p></p> shouldn't match\n\nAnd `<p></p>` shouldn't match either";
// note that <table>s here are improperly nested but we still want to treat them as a section

$('.markdown').html(html);

var editor = inlineMarkdownEditor({
replaceUrl: '/wiki/replace/',
selector: '.markdown'
});

// ok, but now so 'hey' is split from the html.

expect(editor.sections.length).toBe(9);
expect($('.inline-section').toArray().length).toBe(9);
expect($('.inline-edit-btn').toArray().length).toBe(2); // two should be editable
done();
});

it("sends exactly matching original text and 'before' parameters", function(done) {
fixture = loadFixtures('index.html');
var html = "## Headings [with](/links)";
Expand Down
20 changes: 20 additions & 0 deletions src/divideIntoSections.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// split by double-newline, but preserve HTML blocks as single sections:
module.exports = function divideIntoSections(content) {
var sections = [];
content = content.replace(/[\n]{2,}/g, "\n\n"); // manage double newlines (not sure exactly?)
chunkArray = content.replace(/(<(.+).*>.+<\/\2>)/g,"@@@@@@$1").split(/@@@@@@|\n\n/);

chunkArray = chunkArray.filter(function (x) {
return (x !== undefined && x !== '' && x.match(/\S/) !== null); // probably overzealous filtering of "empty" sections
});

chunkArray.forEach(function(chunk) {
if (chunk.match(/<\w>/)) {
sections.push(chunk); // it's an HTML chunk
} else {
sections = sections.concat(chunk.split("\n\n")); // split by double newline and add
}
});

return sections;
}
3 changes: 2 additions & 1 deletion src/inlineMarkdownEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ inlineMarkdownEditor = function inlineMarkdownEditor(o) {
o.originalMarkdown = el.html();
o.preProcessor = o.preProcessor || function(m) { return m; }
// split by double-newline:
var sections = o.originalMarkdown.replace(/[\n]{2,}/g, "\n\n").split("\n\n");
o.divideIntoSections = o.divideIntoSections || require('./divideIntoSections.js');
var sections = o.divideIntoSections(o.originalMarkdown);
var editableSections = [];
// we also do this inside processSection, but independently track here:
sections.forEach(function forEachSection(section, index) {
Expand Down
2 changes: 1 addition & 1 deletion src/isEditable.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module.exports = function isEditable(markdown, originalMarkdown) {
originalMarkdown = originalMarkdown || markdown; // optional parameter for checking against original complete text
// filter? Only p,h1-5,ul?
var editable = markdown.match(/</) === null; // has tags; exclueds HTML
var editable = markdown.match(/</) === null; // has tags; excludes HTML
editable = editable && markdown.match(/\*\*\*\*/) === null; // no horizontal rules: ****
editable = editable && markdown.match(/\-\-\-\-/) === null; // no horizontal rules: ----
editable = editable && markdown !== ''; // no blanks
Expand Down
8 changes: 6 additions & 2 deletions src/processSection.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,12 @@ module.exports = function processSection(markdown, o) {
after: after // encodeURI(after)
})
.done(function onComplete(result, success, xhr) {
// we should need fewer things here:
o.onComplete(xhr.status, after, html, _el, uniqueId, __form, o);
if (result == "false") {
o.onFail(result, uniqueId);
} else {
// we should need fewer things here:
o.onComplete(xhr.status, after, html, _el, uniqueId, __form, o);
}
}).fail(function onFail(response) {
o.onFail(response, uniqueId);
}); // these don't work?
Expand Down

0 comments on commit e2b0c19

Please sign in to comment.