@richardtowers/remark-abbr #1365
Replies: 3 comments 8 replies
-
Hey @richardTowers! 👋 Some thoughts exploring the code.
|
Beta Was this translation helpful? Give feedback.
-
One question I had was whether asking remark-rehype users to do:
was the best I can achieve? It feels like it should be possible to flag to the hast compiler that some mdast nodes should be skipped when compiling to hast... Maybe something like:
? Or alternatively, some way for |
Beta Was this translation helpful? Give feedback.
-
Nice work! Trying out the example code:
There’s currently a type error. I made the error a bit more readable with this example code: /**
* @import {Options} from 'remark-rehype'
*/
import rehypeStringify from 'rehype-stringify'
import remarkAbbr from '@richardtowers/remark-abbr'
import remarkParse from 'remark-parse'
import remarkRehype from 'remark-rehype'
import {read} from 'to-vfile'
import {unified} from 'unified'
/** @type {Options} */
const options = {
handlers: {
abbrDefinition() {
return undefined
}
}
}
const file = await unified()
.use(remarkParse)
.use(remarkAbbr)
.use(remarkRehype, options)
.use(rehypeStringify)
.process(await read('example.md'))
console.log(String(file))
TS doesn’t know the Node yet, it seems. Reading through the node types:
Perhaps, like this: import type {Data, Literal} from 'mdast'
export interface AbbrDefinitionData extends Data {}
export interface AbbrDefinition extends Literal {
type: 'abbrDefinition'
identifier: string
data?: AbbrDefinitionData | undefined
}
export interface AbbrData extends Data {
hName?: 'abbr' | null | undefined
hProperties?: {
title?: string | null | undefined
}
}
export interface Abbr extends Literal {
type: 'abbr'
identifier: string
data?: AbbrData | undefined
}
declare module 'mdast' {
interface DefinitionContentMap {
abbrDefinition: AbbrDefinition
}
interface PhrasingContentMap {
abbr: Abbr
}
interface RootContentMap {
abbrDefinition: AbbrDefinition
abbr: Abbr
}
} Starting out with improved, clear types, should help you clean the types in your mdast utility. Also, some more tests for you: A&B, C&D, E&F.
G\&H, I\&J, K\&L.
M&N, O&P, Q&R.
*[A&B]: alpha
*[C\&D]: bravo
*[E&F]: charlie
*[G&H]: delta
*[I\&J]: echo
*[K&L]: foxtrot
*[M&N]: golf
*[O\&P]: hotel
*[Q&R]: india This tests whether the 2 different escape mechanisms are used or not and how. A B.
C
D.
E F.
G
H.
*[A B]: alpha
*[C D]: bravo
*[E
F]: charlie
*[G
H]: delta This tests whether whether spaces/line endings are collapsed. |
Beta Was this translation helpful? Give feedback.
-
Hello!
I previously discussed building a micromark extension to support abbreviations in the style of PHP markdown extras and kramdown.
@wooorm and I came to the conclusion that doing this in pure micromark is tricky at the moment, so instead I've used a combination of micromark and mdast extensions to build a remark-abbr plugin.
For the most part, I'm pretty happy with it. The abbr syntax it supports is a bit weird, in that abbreviation calls don't have any syntax at all - they're just "whatever the label in the definition is". This meant I needed one pretty kludgy syntax tree transform, but I think the syntax is always going to require something a bit ugly so I'm okay with it.
You can get a feel for the syntax it supports by having a look at the fixtures.
I've published it under my npm user's namespace for now (@richardtowers/remark-abbr), because zmarkdown already have the main remark-abbr package. It sounds like they're open to contributions, but I thought I'd post here first to see if there were any comments before seeing if they're interested in taking on ownership of the code (or handing me the remark-abbr package name).
Beta Was this translation helpful? Give feedback.
All reactions