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

Move transformer into mdast-util-breaks #9

Closed
4 tasks done
remcohaszing opened this issue Apr 25, 2023 · 4 comments · Fixed by #10
Closed
4 tasks done

Move transformer into mdast-util-breaks #9

remcohaszing opened this issue Apr 25, 2023 · 4 comments · Fixed by #10
Labels
💪 phase/solved Post is done

Comments

@remcohaszing
Copy link
Member

Initial checklist

Problem

The transformer can work on the AST without unified. It may be useful to use this as a plain function.

Solution

Move the transformer into a new package mdast-util-breaks. This package would be part of syntax-tree.

Alternatives

I chose the name to match the name remark-breaks, but it may make sense to use something more verbose, e.g.: mdast-util-break-newlines.

@github-actions github-actions bot added 👋 phase/new Post is being triaged automatically 🤞 phase/open Post is being triaged manually and removed 👋 phase/new Post is being triaged automatically labels Apr 25, 2023
@ChristianMurphy
Copy link
Member

Sounds good to me, thanks @remcohaszing!
the more verbose name mdast-util-break-newlines sounds better to me, but interested to hear others thoughts.

@wooorm
Copy link
Member

wooorm commented Apr 25, 2023

Yeah, sure.

Why not mdast-util-hard-break? mdast-util-automatic-break? mdast-util-auto-hard-break? mdast-util-newline-to-break? (optionally with s suffix)

I’m not sure what break-newlines means, it feels “inverted”.

I think mdast-util-newline-to-break is my preference.

@remcohaszing
Copy link
Member Author

I’m not sure what break-newlines means, it feels “inverted”.

It works if you read “break” as a verb.

Anyway, I like mdast-util-newline-to-break too. I’ll go with that. I’ll get to it later this week.

What license should I use? Since it’s going to be mostly based on this repo, copy the license as-is?

@wooorm
Copy link
Member

wooorm commented Apr 26, 2023

That could be an explanation, but then I think that’s a bit weird, wouldn’t you break the lines instead? break-lines?

What license should I use?

Yep, reuse the one seems fine.


I think you could also go with:

/**
 * @typedef {import('mdast').Content} Content
 * @typedef {import('mdast').Root} Root
 * @typedef {import('mdast-util-find-and-replace').ReplaceFunction} ReplaceFunction
 */

/**
 * @typedef {Content | Root} Node
 */

import {findAndReplace} from 'mdast-util-find-and-replace'

/**
 * Turn normal line endings into hard breaks.
 *
 * @param {Node} tree
 *   Tree to change.
 * @returns {void}
 *   Nothing.
 */
export function newlineToBreak(tree) {
  findAndReplace(tree, /\r?\n|\r/g, replace)
}

/**
 * Replace line endings.
 *
 * @type {ReplaceFunction}
 * @param {string} value
 */
function replace(value) {
  return [{type: 'break'}, {type: 'text', value}]
}

This is what I have locally now in rehype-github-*

AFAIK it is mostly equivalent: because line endings can never occur at the start or end of a paragraph, or next to each other (blank lines).

Though this findAndReplace method would split the line endings from the next text? Maybe findAndReplace should be updates to merge initial/final text node results?

Looking at the current algo, it actually drops all positional info on text nodes? 🤔

remcohaszing added a commit to syntax-tree/mdast-util-newline-to-break that referenced this issue Apr 30, 2023
The implementation is based on `remark-break`. It was copied, then
modified. The following modifications have been made:

- The implementation was moved from `index.js` to `lib/index.js`.
- The remark plugin was replaced with the function `newlineToBreak`.
- The implementation was based on
  remarkjs/remark-breaks#9 (comment).
- Tests are based on markdown output, not html output. This uncovered a
  small bug in the implementation linked above that has been fixed.
- Tests are not based on a unified pipeline.
- `tape` was replaced with `node:test`.
- Each test fixture now has its own test case. This allows test
  filtering and inspecting multiple failed tests.
- Dependencies were updated.
- Support was dropped for Node.js < 16.
- The readme was updated accordingly.
@wooorm wooorm closed this as completed in #10 May 9, 2023
wooorm pushed a commit that referenced this issue May 9, 2023
Closes GH-9.
Closes GH-10.

Reviewed-by: Christian Murphy <christian.murphy.42@gmail.com>
Reviewed-by: Titus Wormer <tituswormer@gmail.com>
@wooorm wooorm added the 💪 phase/solved Post is done label May 9, 2023
@github-actions github-actions bot removed the 🤞 phase/open Post is being triaged manually label May 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
💪 phase/solved Post is done
Development

Successfully merging a pull request may close this issue.

3 participants