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

feat(commonmark-editor): detect and format pasted code #147

Merged
merged 18 commits into from Jun 29, 2022

Conversation

dancormier
Copy link
Contributor

@dancormier dancormier commented Jun 23, 2022

closes #135

Describe your changes

Detect pasted code and wrap it in code fences.

PR Checklist

  • All new/changed functionality includes unit and (optionally) e2e tests as appropriate
  • All new/changed functions have /** ... */ docs
  • I've added the bug/enhancement and other labels as appropriate

Pasting within an existing commonmark code block

The functionality introduced in this PR does not currently account for pasting into an existing code block in markdown mode (it always wraps pasted code in fences). Although this behavior is somewhat odd from a UX standpoint, it lines up with the existing behavior of the code block command. I'd suggest we create a new issue for this behavior and consider addressing it out of scope for this PR. What do you think @b-kelly?

A note on the approach taken in this PR

From the related issue #135:

For every paste, we'd need to spin up a copy of the rich-text schema[…]

I took a very light touch approach with this PR by purely extending the functionality of codePasteHandler just to wrap pasted code in code fences. For the sake of time/scope, I suggest this "light touch" approach for now and revisiting spinning up the rich-text schema/editor as a refactor down the road, since that approach might be applicable in for a few different features (markdown preview pane, rich-text paste node parsing for CommonMark editor, etc.).

@dancormier dancormier added enhancement New feature or request mode - markdown Affects the editor's markdown input mode labels Jun 23, 2022
@dancormier dancormier requested a review from b-kelly June 23, 2022 22:31
@netlify
Copy link

netlify bot commented Jun 23, 2022

Deploy Preview for nifty-lalande-39c157 ready!

Name Link
🔨 Latest commit 9b53107
🔍 Latest deploy log https://app.netlify.com/sites/nifty-lalande-39c157/deploys/62bca50abf2418000a96b191
😎 Deploy Preview https://deploy-preview-147--nifty-lalande-39c157.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@b-kelly b-kelly force-pushed the main branch 4 times, most recently from b84f781 to db0a798 Compare June 24, 2022 15:03
Comment on lines 79 to 83
if (isMarkdown) {
codeData = `${
$from.pos === 1 ? "" : "\n"
}\`\`\`\n${codeData}\n\`\`\`\n`;
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the main modification to codePasteHandler to operate within the CommonMark editor.

@dancormier dancormier marked this pull request as ready for review June 28, 2022 16:43
@b-kelly b-kelly force-pushed the main branch 2 times, most recently from 1e96950 to 3e4c847 Compare June 28, 2022 21:06
@b-kelly b-kelly merged commit 9d9841a into main Jun 29, 2022
@b-kelly b-kelly deleted the dcormier/md-code-paste-too-simple-135 branch June 29, 2022 19:18
@b-kelly
Copy link
Collaborator

b-kelly commented Jun 29, 2022

Made some tweaks to split the plugin into two (one for rich-text, one for commonmark), moving the shared functionality out into a common, testable function. Additionally, I set up the commonmark extension to detect pasted code in the same manner the rich-text editor's schema does.

@double-beep
Copy link
Contributor

double-beep commented Jun 30, 2022

OK, does this even work? I tried to paste any text into the commonmark editor and it is always formatted as code. Keep in mind that:

  1. Since the Commonmark editor is wrapped in a <code> block, text copied from and pasted to that editor should not considered code.
  2. Inline code should not be treated as a code fence.

@b-kelly
Copy link
Collaborator

b-kelly commented Jul 1, 2022

@double-beep Can you be more specific in what issue you're running into? Can you provide repro steps for me?

I've contemplated in the past whether to consider text copied from the commonmark editor as code or not. I'm going to revisit that this morning and make a decision before cutting the next release.

@b-kelly
Copy link
Collaborator

b-kelly commented Jul 1, 2022

@double-beep I think I'm reproing the issue you're describing. The fix is simple - I'll get it in place with some updated tests asap.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request mode - markdown Affects the editor's markdown input mode
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Auto-escape detected code in Commonmark mode
3 participants