Skip to content

Commit

Permalink
Fix types, add type coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
richardTowers committed Sep 13, 2024
1 parent 5d72e9e commit f18b487
Show file tree
Hide file tree
Showing 7 changed files with 171 additions and 10 deletions.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
*.d.ts
*.log
/node_modules
/.vscode
/coverage
/lib
/index.js
/index.js
!/dev/index.d.ts
/tsconfig.tsbuildinfo
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@

## TODO

- typescript coverage
- See if there's a nice way to remove the empty `<div>` tags output by remark-rehype, without needing the user to pass a custom handler
- proper README
7 changes: 7 additions & 0 deletions dev/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/** @this {any} */
export default function remarkAbbr(this: any): void
export {abbrFromMarkdown as mdastUtilAbbrFromMarkdown} from './lib/mdast-util-abbr/index.js'
export {
abbr as micromarkAbbr,
abbrTypes as micromarkAbbrTypes,
} from './lib/micromark-extension-abbr/syntax.js'
4 changes: 1 addition & 3 deletions dev/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ export {
} from './lib/micromark-extension-abbr/syntax.js'
export {abbrFromMarkdown as mdastUtilAbbrFromMarkdown} from './lib/mdast-util-abbr/index.js'

/**
* @this {any}
*/
/** @this {any} */
export default function remarkAbbr() {
const self = this
const data = self.data()
Expand Down
18 changes: 14 additions & 4 deletions dev/lib/mdast-util-abbr/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
* Handle as FromMarkdownHandle
* } from 'mdast-util-from-markdown'
* @import {
* Literal,
* Node
* Node,
* } from 'unist'
*/

Expand All @@ -16,7 +15,7 @@ import {abbrTypes} from '../micromark-extension-abbr/syntax.js'

/**
*
* @param {Literal} textNode
* @param {any} textNode
* @param {{label: string, title: string}[]} abbreviations
* @returns {Node[]}
*/
Expand Down Expand Up @@ -160,7 +159,10 @@ export function abbrFromMarkdown() {
},
transforms: [
(tree) => {
// Find the abbrDefinitions - they'll be at the top level
/**
* Find the abbrDefinitions - they'll be at the top level
* @type {any[]}
*/
const abbrDefinitions = tree.children.filter(
(x) => x.type === abbrTypes.abbrDefinition,
)
Expand All @@ -174,7 +176,13 @@ export function abbrFromMarkdown() {
return SKIP
}

if (index === undefined || parent === undefined) {
return CONTINUE
}

// @ts-ignore parent.type is overly restrictive
if (node.type === 'text' && parent.type !== 'abbr') {
/** @type {any[]} */
const newNodes = splitTextByAbbr(node, abbrDefinitions)
parent.children.splice(index, 1, ...newNodes)
return SKIP
Expand Down Expand Up @@ -208,6 +216,7 @@ export function abbrFromMarkdown() {
*/
function exitAbbrDefinitionLabel() {
const label = this.resume()
/** @type {any} */
const node = this.stack[this.stack.length - 1]
assert(node.type === abbrTypes.abbrDefinition)
node.label = label
Expand All @@ -226,6 +235,7 @@ export function abbrFromMarkdown() {
* @type {FromMarkdownHandle}
*/
function exitAbbrDefinitionValueString() {
/** @type {any} */
const node = this.stack.find(
(node) => node.type === abbrTypes.abbrDefinition,
)
Expand Down
142 changes: 142 additions & 0 deletions package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "1.0.0",
"main": "dev/index.js",
"scripts": {
"build": "tsc --build --clean && micromark-build",
"build": "tsc --build --clean && tsc --build && type-coverage && micromark-build",
"format": "prettier . -w --log-level warn && xo --fix",
"test-dev": "node --conditions development --test",
"test-prod": "node --conditions production --test",
Expand Down Expand Up @@ -86,6 +86,7 @@
"remark-parse": "^11.0.0",
"remark-rehype": "^11.1.0",
"remark-stringify": "^11.0.0",
"type-coverage": "^2.29.1",
"typescript": "^5.6.2",
"unified": "^11.0.5",
"xo": "^0.59.3"
Expand Down

0 comments on commit f18b487

Please sign in to comment.