Skip to content
This repository has been archived by the owner on Feb 5, 2020. It is now read-only.

Commit

Permalink
rST section headers can be marked with a bizarre array of characters
Browse files Browse the repository at this point in the history
There could be more tests ...
  • Loading branch information
bmcorser committed Jan 29, 2015
1 parent 21e6826 commit fcd723d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
24 changes: 18 additions & 6 deletions lib/rst.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
/* jshint node:true */
"use strict";

// http://docutils.sourceforge.net/docs/user/rst/quickref.html#section-structure
var ACCEPTED_CHARS = ['=', '-', '`', ':', "'", '"', '~', '^', '_', '*', '+', '#', '<', '>'];

function badger(content, imgUrl, linkUrl, altText) {
var lines = content.split('\n');
var idealLine = findIdealLineForInsert(lines);
Expand All @@ -13,19 +16,28 @@ function badger(content, imgUrl, linkUrl, altText) {
}

function findIdealLineForInsert(lines) {
var i = 0;
var lineIdx = 1;
var chrIdx = 0;
var seenHeader = false;

for(; i < lines.length; i++) {
// the top line is *never* the header
if(/^\s*(={3,}|-{3,})/.test(lines[i]) && i > 0) {
seenHeader = true;
for(; lineIdx < lines.length; lineIdx++) {
var line = lines[lineIdx];
var lineAbove = lines[lineIdx - 1];
var chrSeen = {};
for(; chrIdx < line.length; chrIdx++) {
chrSeen[line[chrIdx]] = null;
}
var uniqChars = Object.keys(chrSeen);
if(uniqChars.length === 1
&& ACCEPTED_CHARS.indexOf(uniqChars[0]) !== -1
&& line.length >= lineAbove.length) {
seenHeader = true;
} else {
if(seenHeader) break;
}
}

return i;
return lineIdx;
}

module.exports = badger;
4 changes: 2 additions & 2 deletions test/examples/rst-after.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
====================
####################
My Amazing Library
====================
####################

.. image:: https://badges.gitter.im/Join%20Chat.svg
:alt: Join the chat at https://gitter.im/myorg/myrepo
Expand Down
4 changes: 2 additions & 2 deletions test/examples/rst-before.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
====================
####################
My Amazing Library
====================
####################

Features
--------
Expand Down

0 comments on commit fcd723d

Please sign in to comment.