소스 검색

feat(headerLevelStart): add support for setting the header starting level

Closes #69
Estevão Soares dos Santos 10 년 전
부모
커밋
b84ac67dac

+ 7 - 5
README.md

@@ -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
@@ -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
 

+ 7 - 3
dist/showdown.js

@@ -12,6 +12,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
@@ -1456,14 +1457,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);
   });
 
@@ -1489,7 +1492,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);
   });

파일 크기가 너무 크기때문에 변경 상태를 표시하지 않습니다.
+ 0 - 0
dist/showdown.js.map


파일 크기가 너무 크기때문에 변경 상태를 표시하지 않습니다.
+ 0 - 0
dist/showdown.min.js


파일 크기가 너무 크기때문에 변경 상태를 표시하지 않습니다.
+ 0 - 0
dist/showdown.min.js.map


+ 1 - 0
src/showdown.js

@@ -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

+ 6 - 3
src/subParsers/headers.js

@@ -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);
   });
 
@@ -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);
   });

+ 9 - 0
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 - 0
test/features/#69.header_level_start.md

@@ -0,0 +1,11 @@
+#Given
+
+#When
+
+#Then
+
+foo
+===
+
+bar
+---

+ 1 - 0
test/node/showdown.js

@@ -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);

+ 2 - 0
test/node/testsuite.features.js

@@ -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();
     }

이 변경점에서 너무 많은 파일들이 변경되어 몇몇 파일들은 표시되지 않았습니다.