Skip to content

Commit

Permalink
feat(headerLevelStart): add support for setting the header starting l…
Browse files Browse the repository at this point in the history
…evel

Closes #69
  • Loading branch information
tivie committed Jun 17, 2015
1 parent b49d934 commit b84ac67
Show file tree
Hide file tree
Showing 11 changed files with 47 additions and 14 deletions.
12 changes: 7 additions & 5 deletions README.md
Expand Up @@ -139,7 +139,7 @@ var thisConverterSpecificOptions = conveter.getOptions();
### Valid Options
* **omitExtraWLInCodeBlocks**: (boolean) [default false] Omits the trailing newline in a code block. Ex:
* **omitExtraWLInCodeBlocks**: (boolean) [default false] Omit the trailing newline in a code block. Ex:
This:
```html
Expand All @@ -151,16 +151,18 @@ var thisConverterSpecificOptions = conveter.getOptions();
<code><pre>var foo = 'bar';</pre></code>
```
* **noHeaderId**: (boolean) [default false] Disables the automatic generation of header ids. Setting to true overrides **prefixHeaderId**
* **noHeaderId**: (boolean) [default false] Disable the automatic generation of header ids. Setting to true overrides **prefixHeaderId**
* **prefixHeaderId**: (string/boolean) [default false] Adds a prefix to the generated header ids. Passing a string will prefix that string to the header id. Setting to `true` will add a generic 'section' prefix.
* **prefixHeaderId**: (string/boolean) [default false] Add a prefix to the generated header ids. Passing a string will prefix that string to the header id. Setting to `true` will add a generic 'section' prefix.
* **parseImgDimensions**: (boolean) [default false] Enables support for setting image dimensions from within markdown syntax.
* **parseImgDimensions**: (boolean) [default false] Enable support for setting image dimensions from within markdown syntax.
Example:
```
![my image](foo.jpg =100x80)
```
* **headerLevelStart**: (integer) [default 1] Set the header starting level. For instance, setting this to 3 means that
`# foo` will be parsed as `<h3>foo</h3>`
## Integration with AngularJS
Expand Down
10 changes: 7 additions & 3 deletions dist/showdown.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/showdown.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/showdown.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/showdown.min.js.map

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/showdown.js
Expand Up @@ -10,6 +10,7 @@ var showdown = {},
omitExtraWLInCodeBlocks: false,
prefixHeaderId: false,
noHeaderId: false,
headerLevelStart: 1,
parseImgDimensions: false
},
globalOptions = JSON.parse(JSON.stringify(defaultOptions)); //clone default options out of laziness =P
Expand Down
9 changes: 6 additions & 3 deletions src/subParsers/headers.js
Expand Up @@ -14,14 +14,16 @@ showdown.subParser('headers', function (text, options, globals) {

var spanGamut = showdown.subParser('spanGamut')(m1, options, globals),
hID = (options.noHeaderId) ? '' : ' id="' + headerId(m1) + '"',
hashBlock = '<h1' + hID + '>' + spanGamut + '</h1>';
hLevel = parseInt(options.headerLevelStart),
hashBlock = '<h' + hLevel + hID + '>' + spanGamut + '</h' + hLevel + '>';
return showdown.subParser('hashBlock')(hashBlock, options, globals);
});

text = text.replace(/^(.+)[ \t]*\n-+[ \t]*\n+/gm, function (matchFound, m1) {
var spanGamut = showdown.subParser('spanGamut')(m1, options, globals),
hID = (options.noHeaderId) ? '' : ' id="' + headerId(m1) + '"',
hashBlock = '<h2' + hID + '>' + spanGamut + '</h2>';
hLevel = parseInt(options.headerLevelStart) + 1,
hashBlock = '<h' + hLevel + hID + '>' + spanGamut + '</h' + hLevel + '>';
return showdown.subParser('hashBlock')(hashBlock, options, globals);
});

Expand All @@ -47,7 +49,8 @@ showdown.subParser('headers', function (text, options, globals) {
text = text.replace(/^(#{1,6})[ \t]*(.+?)[ \t]*#*\n+/gm, function (wholeMatch, m1, m2) {
var span = showdown.subParser('spanGamut')(m2, options, globals),
hID = (options.noHeaderId) ? '' : ' id="' + headerId(m2) + '"',
header = '<h' + m1.length + hID + '>' + span + '</h' + m1.length + '>';
hLevel = parseInt(options.headerLevelStart) - 1 + m1.length,
header = '<h' + hLevel + hID + '>' + span + '</h' + hLevel + '>';

return showdown.subParser('hashBlock')(header, options, globals);
});
Expand Down
9 changes: 9 additions & 0 deletions test/features/#69.header_level_start.html
@@ -0,0 +1,9 @@
<h3 id="given">Given</h3>

<h3 id="when">When</h3>

<h3 id="then">Then</h3>

<h3 id="foo">foo</h3>

<h4 id="bar">bar</h4>
11 changes: 11 additions & 0 deletions test/features/#69.header_level_start.md
@@ -0,0 +1,11 @@
#Given

#When

#Then

foo
===

bar
---
1 change: 1 addition & 0 deletions test/node/showdown.js
Expand Up @@ -21,6 +21,7 @@ describe('showdown.options', function () {
omitExtraWLInCodeBlocks: false,
prefixHeaderId: false,
noHeaderId: false,
headerLevelStart: 1,
parseImgDimensions: false
};
expect(showdown.getDefaultOptions()).to.be.eql(opts);
Expand Down
2 changes: 2 additions & 0 deletions test/node/testsuite.features.js
Expand Up @@ -12,6 +12,8 @@ describe('makeHtml() features testsuite', function () {
var converter;
if (testsuite[i].name === '#143.support_image_dimensions') {
converter = new showdown.Converter({parseImgDimensions: true});
} else if (testsuite[i].name === '#69.header_level_start') {
converter = new showdown.Converter({headerLevelStart: 3});
} else {
converter = new showdown.Converter();
}
Expand Down

0 comments on commit b84ac67

Please sign in to comment.