Skip to content

Commit

Permalink
Refactor to move implementation to lib/
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Jan 25, 2023
1 parent daf6040 commit 0a50017
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 53 deletions.
55 changes: 2 additions & 53 deletions index.js
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'
56 changes: 56 additions & 0 deletions lib/index.js
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
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"main": "index.js",
"types": "index.d.ts",
"files": [
"lib/",
"index.d.ts",
"index.js"
],
Expand Down

0 comments on commit 0a50017

Please sign in to comment.