-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor to move implementation to
lib/
- Loading branch information
Showing
3 changed files
with
59 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,5 @@ | ||
/** | ||
* @typedef {import('mdast').Heading} Heading | ||
* @typedef {'atx'|'atx-closed'|'setext'} Style | ||
* @typedef {import('./lib/index.js').Style} Style | ||
*/ | ||
|
||
/** | ||
* @param {Heading} node | ||
* @param {Style} [relative] | ||
* @returns {Style|null} | ||
*/ | ||
export function headingStyle(node, relative) { | ||
const last = node.children[node.children.length - 1] | ||
const depth = node.depth | ||
const pos = node && node.position && node.position.end | ||
const final = last && last.position && last.position.end | ||
|
||
if (!pos) { | ||
return null | ||
} | ||
|
||
// This can only occur for `'atx'` and `'atx-closed'` headings. | ||
// This might incorrectly match `'atx'` headings with lots of trailing white | ||
// space as an `'atx-closed'` heading. | ||
if (!last) { | ||
if (pos.column - 1 <= depth * 2) { | ||
return consolidate(depth, relative) | ||
} | ||
|
||
return 'atx-closed' | ||
} | ||
|
||
if (final && final.line + 1 === pos.line) { | ||
return 'setext' | ||
} | ||
|
||
if (final && final.column + depth < pos.column) { | ||
return 'atx-closed' | ||
} | ||
|
||
return consolidate(depth, relative) | ||
} | ||
|
||
/** | ||
* Get the probable style of an atx-heading, depending on preferred style. | ||
* | ||
* @param {number} depth | ||
* @param {Style|undefined} relative | ||
* @returns {Style|null} | ||
*/ | ||
function consolidate(depth, relative) { | ||
return depth < 3 | ||
? 'atx' | ||
: relative === 'atx' || relative === 'setext' | ||
? relative | ||
: null | ||
} | ||
export {headingStyle} from './lib/index.js' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/** | ||
* @typedef {import('mdast').Heading} Heading | ||
* @typedef {'atx'|'atx-closed'|'setext'} Style | ||
*/ | ||
|
||
/** | ||
* @param {Heading} node | ||
* @param {Style} [relative] | ||
* @returns {Style|null} | ||
*/ | ||
export function headingStyle(node, relative) { | ||
const last = node.children[node.children.length - 1] | ||
const depth = node.depth | ||
const pos = node && node.position && node.position.end | ||
const final = last && last.position && last.position.end | ||
|
||
if (!pos) { | ||
return null | ||
} | ||
|
||
// This can only occur for `'atx'` and `'atx-closed'` headings. | ||
// This might incorrectly match `'atx'` headings with lots of trailing white | ||
// space as an `'atx-closed'` heading. | ||
if (!last) { | ||
if (pos.column - 1 <= depth * 2) { | ||
return consolidate(depth, relative) | ||
} | ||
|
||
return 'atx-closed' | ||
} | ||
|
||
if (final && final.line + 1 === pos.line) { | ||
return 'setext' | ||
} | ||
|
||
if (final && final.column + depth < pos.column) { | ||
return 'atx-closed' | ||
} | ||
|
||
return consolidate(depth, relative) | ||
} | ||
|
||
/** | ||
* Get the probable style of an atx-heading, depending on preferred style. | ||
* | ||
* @param {number} depth | ||
* @param {Style|undefined} relative | ||
* @returns {Style|null} | ||
*/ | ||
function consolidate(depth, relative) { | ||
return depth < 3 | ||
? 'atx' | ||
: relative === 'atx' || relative === 'setext' | ||
? relative | ||
: null | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,7 @@ | |
"main": "index.js", | ||
"types": "index.d.ts", | ||
"files": [ | ||
"lib/", | ||
"index.d.ts", | ||
"index.js" | ||
], | ||
|