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

Add Remarkable standalone blocks configuration to enable customization #147

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Ynote
Copy link

@Ynote Ynote commented Nov 12, 2020

Description

I noticed that Remarkable standalone blocks like fence or hr have a special case handling in src/markdown-to-draft.js#L257.

When I tried to add a custom Remarkable plugin that renders a token that looks like the fence one, it is not included correctly for Draft.js block creation except if the token type finishes with "_open" (cf. src/markdown-to-draft.js#L257). As my custom Remarkable block is a standalone block, I think it would be more readable and consistent if its name doesn't end with "_open" and can still be handled correctly by markdown-draft-js (eg. yaml_frontmatter and not yaml_frontmatter_open).

For this purpose, I added another configuration array to handle these standalone blocks. This array clarifies their specificity and enables customization.

Use case

I want Remarkable to parses this kind of block at the top of a file:

---
someMetadata: content
someOtherMetadata: content
---

and create a token that looks like this (with a plugin like this one), :

{
  type: 'yaml_frontmatter',
  content: 'someMetadata: content\nsomeOtherMetadata: content',
  level: ...,
  lines: ...,
}

Then, using markdownToDraft, I would get the following raw Draft state:

{
  blocks: {
    {
      type: 'atomic',
      content: 'someMetadata: content\nsomeOtherMetadata: content',
      entityRanges: [],
      inlineStyleRanges: [],
    }
  },
  entityMap: ...
}

The PR would enable it with the following configuration for markdownToDraft:

let rawContent = markdownToDraft(content, {
  remarkablePlugins: [remarkableYamlFrontMatterPlugin],
  blockTypes: {
    frontmatter_yaml: (item) => { // Custom method to tell markdownToDraft how to handle custom Markdown
      return {
        type: 'atomic',
        text: item.content,
        entityRanges: [],
        inlineStyleRanges: [],
      }
    },
    remarkableStandaloneBlocks: ['frontmatter_yaml'], // Here, we tell markdowntoDraft to add this case to the raw Draft state
  },
})

@Ynote Ynote marked this pull request as draft November 12, 2020 22:17
@Ynote Ynote force-pushed the enable-singleton-custom-block branch from 5d3dbd6 to 844fe55 Compare November 12, 2020 23:37
@Ynote Ynote changed the title Add Remarkable singletons configuration to enable customization Add Remarkable standalone blocks configuration to enable customization Nov 13, 2020
@Ynote Ynote marked this pull request as ready for review November 13, 2020 09:31
@Ynote Ynote force-pushed the enable-singleton-custom-block branch from be85cb8 to 0d0f6b9 Compare November 13, 2020 09:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant