Comparing version 3.0.0 to 3.0.1
33
index.js
@@ -5,2 +5,3 @@ var fs = require('fs'); | ||
var cheerio = require('cheerio'); | ||
var normall = require('normall'); | ||
@@ -148,2 +149,32 @@ var rules = require('./rules'); | ||
// Normalize titles id based on title text | ||
function normalizeTitlesId() { | ||
var titles = ['h1', 'h2', 'h3', 'h4', 'h5', 'h6']; | ||
var ids = []; | ||
var counter; | ||
titles.forEach(function(title) { | ||
$(title).each(function() { | ||
// Compute new id | ||
var oldId = $(this).attr('id'); | ||
var textId = normall.filename($(this).text()); | ||
var newId = textId; | ||
counter = 0; | ||
while (_.includes(ids, newId)) { | ||
newId = textId+'-'+counter; | ||
} | ||
// Prevent obtaining the same id twice | ||
ids.push(newId); | ||
// Replace id in href links | ||
$('*[href="#'+oldId+'"]').each(function() { | ||
$(this).attr('href', '#'+newId); | ||
}); | ||
// Replace title id | ||
$(this).attr('id', newId); | ||
}); | ||
}); | ||
}; | ||
// For empty <a> tags with an id attribute, set id on parent | ||
@@ -273,2 +304,3 @@ function setAnchorsId() { | ||
cleanElements(); | ||
normalizeTitlesId(); | ||
// Cleanup tables | ||
@@ -289,2 +321,3 @@ removeNestedTables(); | ||
retrieveFootnotes: retrieveFootnotes, | ||
normalizeTitlesId: normalizeTitlesId, | ||
removeNestedTables: removeNestedTables, | ||
@@ -291,0 +324,0 @@ formatTables: formatTables, |
{ | ||
"name": "brightml", | ||
"version": "3.0.0", | ||
"version": "3.0.1", | ||
"description": "Smart utility rendering markdown-ready HTML", | ||
@@ -23,3 +23,4 @@ "main": "index.js", | ||
"cheerio": "git://github.com/cheeriojs/cheerio.git#70c5608113d3efaf584efd29edafe173b74e106f", | ||
"lodash": "^3.10.1" | ||
"lodash": "^3.10.1", | ||
"normall": "^0.2.2" | ||
}, | ||
@@ -26,0 +27,0 @@ "scripts": { |
@@ -106,2 +106,18 @@ # Brightml | ||
#### brightml.normalizeTitlesId() | ||
Set an `id` attribute on each `<h>` tag. The `id` is based on the title tag content. | ||
Each reference to this `id` will be modified in consequence. | ||
```HTML | ||
<h1 id="some-id">A great title</h1> | ||
<a href="#some-id">Back to a great title</a> | ||
``` | ||
will become: | ||
```HTML | ||
<h1 id="a_great_title">A great title</h1> | ||
<a href="#a_great_title">Back to a great title</a> | ||
``` | ||
#### brightml.removeNestedTables() | ||
@@ -108,0 +124,0 @@ |
@@ -217,2 +217,42 @@ var should = require('should'); | ||
describe('brightml.normalizeTitlesId()', function() { | ||
it('should change the title id', function() { | ||
var input = '<h1 id="345-is-a-weird-id">'+ | ||
'Sample title'+ | ||
'</h1>'; | ||
var correctOutput = '<h1 id="sample_title">'+ | ||
'Sample title'+ | ||
'</h1>'; | ||
brightml.parse(input); | ||
brightml.normalizeTitlesId(); | ||
var output = brightml.render(); | ||
output.should.be.equal(correctOutput); | ||
}); | ||
it('should replace references to title id', function() { | ||
var input = '<h2 id="another-strange-id">'+ | ||
'A great title'+ | ||
'</h2>'+ | ||
'<a href="#another-strange-id">'+ | ||
'Go back'+ | ||
'</a>'; | ||
var correctOutput = '<h2 id="a_great_title">'+ | ||
'A great title'+ | ||
'</h2>'+ | ||
'<a href="#a_great_title">'+ | ||
'Go back'+ | ||
'</a>'; | ||
brightml.parse(input); | ||
brightml.normalizeTitlesId(); | ||
var output = brightml.render(); | ||
output.should.be.equal(correctOutput); | ||
}); | ||
}); | ||
describe('brightml.retrieveFootnotes()', function() { | ||
@@ -414,3 +454,3 @@ it('should move the referenced <p> tag before the next <h1> tag', function() { | ||
var correctOutput = '<h1 id="first-title">Part 1</h1>'+ | ||
var correctOutput = '<h1 id="part_1">Part 1</h1>'+ | ||
'<p>'+ | ||
@@ -442,5 +482,5 @@ 'Sample footnote'+ | ||
'</p>'+ | ||
'<h1>Part 2</h1>'+ | ||
'<h1 id="part_2">Part 2</h1>'+ | ||
'<p>Some content</p>'+ | ||
'<h1>Part 3</h1>'+ | ||
'<h1 id="part_3">Part 3</h1>'+ | ||
'<caption>Data table</caption>'+ | ||
@@ -447,0 +487,0 @@ '<table>'+ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
32441
790
164
3
+ Addednormall@^0.2.2
+ Addedhan@0.0.7(transitive)
+ Addedlatenize@0.2.0(transitive)
+ Addednormall@0.2.2(transitive)