Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve type annotations #8

Merged
merged 1 commit into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,32 @@
import type {Root} from 'mdast'
import type {Extension as FromMarkdownExtension} from 'mdast-util-from-markdown'
import type {Processor, Transformer} from 'unified'
import {micromarkAttributes} from './packages/micromark-attributes/index.js'
import {mdastAttributes} from './packages/mdast-attributes/index.js'
import {attributesTransformer} from './packages/attributes-transformer/index.js'
import {AttributesExtension} from './util/types.js'

interface AttributesData {
micromarkExtensions?: AttributesExtension[]
fromMarkdownExtensions?: FromMarkdownExtension[]
}

/**
* Plugin to support attributes like markdown-it-attrs
* [text](https://test.com){target=_blank}
*/
export default function remarkAttributes(
this: Processor,
this: Processor<Root, Root, Root, string>,
options = {mdx: false}
): Transformer<Root> {
const data: Record<string, unknown> = this.data()
const data = this.data() as AttributesData

/**
* @param {string} key
* @param {unknown} value
*/
function add(key: string, value: unknown) {
const list = (data[key] || (data[key] = [])) as unknown[]
list.unshift(value)
function add<K extends keyof AttributesData>(
key: K,
value: AttributesData[K][0]
) {
data[key] ||= []
data[key].unshift(value)
}

add('micromarkExtensions', micromarkAttributes({escaped: options.mdx}))
Expand Down
5 changes: 3 additions & 2 deletions packages/attributes-transformer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {Node, Parent} from 'unist'
import parseAttrs from 'md-attr-parser'

type AttrsNode = {
type: 'attrs'
value?: string
} & Node

Expand All @@ -22,11 +23,11 @@ export function attributesTransformer(node: any): void {
node,
(node, index, parent) =>
['paragraph'].includes(node.type) && parent?.type === 'listItem',
(node: Parent<AttrsNode>, index, parent) => {
(node: Parent<Node | AttrsNode>, index, parent) => {
const children = node.children

const ids = children.filter(
(child: {type: string}) => child.type === 'attrs'
(child: {type: string}): child is AttrsNode => child.type === 'attrs'
)

if (!ids || ids.length === 0) return
Expand Down
9 changes: 4 additions & 5 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@
"moduleResolution": "nodenext",
"noImplicitAny": false,
"declaration": true,
"skipLibCheck": true,
"typeRoots": [
"./typings",
"node_modules/@types"
],
"paths": {
"md-attr-parser": ["./typings/md-attr-parser.d.ts"]
},
"skipLibCheck": true
},
"ts-node": {
"esm": true,
Expand Down
1 change: 0 additions & 1 deletion typings/index.d.ts

This file was deleted.

22 changes: 22 additions & 0 deletions typings/md-attr-parser.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// md-attr-parser 1.3.0

export interface Properties {
id: string
class: string[]
[K: string]: string | string[]
}

export interface ParseResult {
prop: Properties
eaten: string
}

export interface Config {
defaultValue?: () => string
}

export default function parse(
value: string,
indexNext?: number,
config?: Config
): ParseResult