-
-
Notifications
You must be signed in to change notification settings - Fork 90
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
feat: add rehype-semantic-blockquotes
plugin
#178
Conversation
Signed-off-by: Nikita Revenco <[email protected]>
…lugin Signed-off-by: Nikita Revenco <[email protected]>
Thanks for sharing @nikitarevenco! 🙇 General feedback on what gets included in the plugins list:
source: syntax-tree/hast#24 (comment) For your plugin I'm seeing:
More specific comments.
|
thank you for the review! changes:
|
Very nice! I do recommend, in the readme, after your “For instance, by turning the following syntax:” line and corresponding markdown code block, to also show some HTML code. Such as: …which is equivalent to the following HTML code:
```html
<blockquote>
<p>We cannot solve our problems with the same thinking we used when we created them.</p>
<p>@ Albert Einstein</p>
</blockquote>
``` People can write HTML in markdown. Which will work with this plugin and rehype. I’d recommend not using words such as “easy” ( I see some things that TS should normally catch in your code. Perhaps turn on With rehype, you do not need to generate MDX custom nodes. rehype is used inside MDX. You can generate I recommend some tests for different HTML structures. You are now heavily basing things on what Something like: /**
* @import {Root} from 'hast'
*/
import {whitespace} from 'hast-util-whitespace'
import {visit} from 'unist-util-visit'
// Get `tree` somehow.
const tree = /** @type {Root} */ (/** @type {unknown} */ (undefined))
visit(tree, 'element', (blockquote, index, parent) => {
if (!parent || typeof index !== 'number') return
if (blockquote.tagName !== 'blockquote') return
let tailIndex = blockquote.children.length - 1
while (tailIndex > -1 && whitespace(blockquote.children[tailIndex])) {
tailIndex--
}
const tail = blockquote.children[tailIndex]
// Tail must be a `p`.
if (!tail || tail.type !== 'element' || tail.tagName !== 'p') return
// Depends what you want with `> *@* this`, `> @ *this*`, `> *@ this*`.
// Tail must be a `p`.
const tailText = tail.children[0]
if (tail.children.length !== 1 || tailText.type !== 'text') return
// Tail text must start with prefix.
if (!tailText.value.startsWith(opts.syntax)) return
parent.children[index] = {
type: 'element',
tagName: 'figure',
properties: {[opts.figure]: ''},
children: [
{
type: 'element',
tagName: 'blockquote',
properties: {[opts.blockquote]: ''},
children: blockquote.children.slice(0, tailIndex)
},
{
type: 'element',
tagName: 'figcaption',
properties: {[opts.figcaption]: ''},
children: [
{
type: 'element',
tagName: 'p',
properties: {},
children: [
{type: 'text', value: tailText.value.slice(opts.syntax.length)}
]
}
]
}
]
}
}) In the hast, camelcased properties are used:
|
Okay, so I have:
I don't really want to let syntax like this: what do you think? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code looks great new!! ✨
One nit about CI failing
doc/plugins.md
Outdated
* [`rehype-semantic-blockquotes`](https://github.com/nikitarevenco/rehype-semantic-blockquotes) | ||
— extend blockquote syntax to make it simple to mention/cite sources in a semantically correct way |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* [`rehype-semantic-blockquotes`](https://github.com/nikitarevenco/rehype-semantic-blockquotes) | |
— extend blockquote syntax to make it simple to mention/cite sources in a semantically correct way | |
* [`rehype-semantic-blockquotes`](https://github.com/nikitarevenco/rehype-semantic-blockquotes) | |
— new blockquote syntax to more simply mention/cite sources |
How about this? A bit shorter.
If you think the semantically correct part is important, you could add it. Just make sure it line wraps correctly (CI tells you about problems: npm install && npm test
!)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, thank you! I have slightly reworded it to make sure it's under 80 chars with inspiration from your suggestion
Codecov ReportAll modified and coverable lines are covered by tests ✅
❗ Your organization needs to install the Codecov GitHub app to enable full functionality. Additional details and impacted files@@ Coverage Diff @@
## main #178 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 6 6
Lines 154 154
=========================================
Hits 154 154 ☔ View full report in Codecov by Sentry. |
Initial checklist
Description of changes
Initially I submitted this to remark, but I was redirected to submit it here, so I converted it to rehype
With this config it will transform the markdown
Into this
Repo: https://github.com/nikitarevenco/rehype-semantic-blockquotes