Giter Club home page Giter Club logo

kramdown-prismic's Introduction

Kramdown Prismic build gem

A Kramdown parser and converter to convert documents into prismic rich text format and the other way around.

A useful usage is to convert markdown documents to prismic for import purpose. Then you can even convert prismic format back to markdown.

Learn more how to import markdown document to Prismic.

Status

The converter part (kramdown to prismic) is working and fairly complete. See Difference between markdown and rich text below to know more about limitations.

The parser part is quite new and not feature complete.

Install

gem 'kramdown-prismic', '~> 0.1'

Usage

With executables

markdown2prismic:

markdown2prismic $'# My Title\n\nHello world'

html2prismic:

html2prismic '<h1>My Title</h1><p>Hello world</p>'

prismic2markdown:

prismic2markdown '[{"type":"heading1","content":{"text":"My Title","spans":[]}},{"type":"paragraph","content":{"text":"Test","spans":[]}}]'

As a library

Convert kramdown documents to Prismic

require 'kramdown-prismic'

kramdown = '# Hello world'
Kramdown::Document.new(kramdown).to_prismic

Convert markdown documents to Prismic

require 'kramdown-prismic'

markdown = '# Hello world'
Kramdown::Document.new(markdown, input: :markdown).to_prismic

Convert HTML documents to Prismic

require 'kramdown-prismic'

html = '<h1>Hello world</h1>'
Kramdown::Document.new(html, input: :html).to_prismic

Convert Prismic to markdown

require 'kramdown-prismic'
prismic = [
  {
    type: "heading1",
    content: {
      text: "This is the document title",
      spans: []
    }
  }
]
Kramdown::Document.new(prismic, input: :prismic).to_kramdown

You can also convert to or from others formats supported by Kramdown. See kramdown documentation.

Lookup for warnings

If there is some elements that cannot be converted (see the status table), a warning will be emitted.

For instance, html elements in the markdown is not supported:

require 'kramdown-prismic'

markdown = '<h1>Hello world</h1>'
result = Kramdown::Document.new(markdown, input: :markdown)
result.to_prismic
p result.warnings

Difference between markdown and rich text

Some elements cannot be converted, due to some Prismic limitations. The table below explain the difference and limitations of the current converter:

Markdown Prismic
blockquote converted to preformatted
hr nothing
img moved to the top level
nested list moved to the top level
entity converted to unicode
typographic_sym converted to unicode
smart_quote converted to unicode
dl not supported
dt not supported
dd not supported
table not supported
thead not supported
tobdy not supported
tfoot not supported
tr not supported
td not supported
math not supported
footnote not supported
abbreviation not supported
html_element not supported
xml_comment not supported
xml_pi not supported
comment not supported
raw not supported

Develop

Install dependencies:

bundle install

Run tests:

bundle exec rake test

License

MIT

kramdown-prismic's People

Contributors

dependabot[bot] avatar francois2metz avatar tjkohli avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

kramdown-prismic's Issues

<a> tag inside <span> tag are rendered as plain text

Hi guys,
I have an issue when parsing from html to prismic format.
For example my html text:

<p>
    <span style="font-weight: 400;">this is some text1 <a href="https://link1.com/">link text 1</a> with link inside</span>
    <a href="https://link2.com/"><span style="font-weight: 400;">this is some text2 </span></a><span style="font-weight: 
     400;">with link inside.</span>
</p>

The output is like this:

[
  {
    type: 'paragraph',
    content: { 
      text: 'this is some text1 link text 1 with link inside.this is some text2 link text 2 with link inside'
    }, 
    spans: [
            {
              "type": "hyperlink",
              "data": {
                "url": "https://link2.com/"
              },
              "start": x,
              "end": x
            }
          ]
    }
]

Basically, the href tag for link1.com will be missing and only shows for link2.com. Is this because the link1.com is wrapped inside a span tag and nested inside p tag?
Any help would be greatly appreciated, thanks!

Support for <br /> html tag

Hello,

I am encountering the following error : convert_element': undefined method convert_br' while exporting some wordpress content.

Would it be possible for you to add support for this br tag please ?

Thank you very much,
Simon

Targets for hyperlink tags

Hi again,
Thank you for your work and such a fast response last time. I am still working on my migration project and noticed one more thing. I don't think this is an error, but rather a missing feature maybe ๐Ÿ˜„ when converting hyperlink tags the target is ignored. Would it be possible to add this info as well?
What I get after parsing

"spans": [
  {
    "start": 11,
    "end": 26,
    "type": "hyperlink",
    "data": {
      "url": "https://link.com"
    }
  }
]

What I would like to get

"spans": [
  {
    "start": 11,
    "end": 26,
    "type": "hyperlink",
    "data": {
      "url": "https://link.com",
      "target": "_blank"
    }
  }
]

I noticed this with inline hyperlink, that are in paragraph, so not sure how it acts with stand alone hyperlink.

Support for header in list

Hello,

First thank you for this awesome library and for your reactivity to fix issues !

I am encountering the following error : converter/prismic.rb:230:in block in extract_content': undefined method extract_span_header' while exporting some wordpress content.

code failing : <ul><li><h4>Title</h4></li></ul>
version : 0.3.6

Would it be possible for you to add support header in list please ?

Thank you very much,
Jonathan

<strong> tag with extra whitespace is parsed incorrectly

I have an issue when parsing from html to rich text.
If I pass such text <p>Some text in <strong>strong </strong> style</p>, rich text json is created correctly. But if there is a whitespace inside the tag <p>Some text in <strong> strong</strong> style</p>, then this part of the text gets lost.

[
  {
    type: 'paragraph',
    content: { text: 'Some text in  style', spans: [] }
  }
]

Works the same if the space is in the beginning or end.
Am I missing something or is this a bug?

Prismic V2: prismic2markdown Type errors

RE: #17 - this issue is about V2 Prismic file

I'm inexperienced with Ruby so this may be a complete newbie problem but I'm not getting it to work with even a small JSON file from Prismic's API.

Sample file: trust.json

 **prismic2markdown --format v2 โ€“ "$(cat /Downloads/trust.json)"**
/Users/viljarsepp/.rubies/ruby-3.3.0/lib/ruby/gems/3.3.0/gems/kramdown-prismic-0.3.10/lib/kramdown-prismic/parser/migration_api.rb:22:in `parse_element': no implicit conversion of Symbol into Integer (TypeError)

type = block[:type].gsub('-', '_')
              ^^^^^
from ../ruby-3.3.0/lib/ruby/gems/3.3.0/gems/kramdown-prismic-0.3.10/lib/kramdown-prismic/parser/**migration_api.rb**:15:in `block in parse'
from ../ruby-3.3.0/lib/ruby/gems/3.3.0/gems/kramdown-prismic-0.3.10/lib/kramdown-prismic/parser/migration_api.rb:14:in `each'
from ../ruby-3.3.0/lib/ruby/gems/3.3.0/gems/kramdown-prismic-0.3.10/lib/kramdown-prismic/parser/migration_api.rb:14:in `reduce'
from ../ruby-3.3.0/lib/ruby/gems/3.3.0/gems/kramdown-prismic-0.3.10/lib/kramdown-prismic/parser/migration_api.rb:14:in `parse'
from ../ruby-3.3.0/lib/ruby/gems/3.3.0/gems/kramdown-2.4.0/lib/kramdown/parser/base.rb:69:in `parse'
from ../ruby-3.3.0/lib/ruby/gems/3.3.0/gems/kramdown-2.4.0/lib/kramdown/document.rb:102:in `initialize'
from ../ruby-3.3.0/lib/ruby/gems/3.3.0/gems/kramdown-prismic-0.3.10/bin/prismic2markdown:28:in `new'
from ../ruby-3.3.0/lib/ruby/gems/3.3.0/gems/kramdown-prismic-0.3.10/bin/prismic2markdown:28:in `<top (required)>'
from ../ruby-3.3.0/bin/prismic2markdown:25:in `load'
from ../ruby-3.3.0/bin/prismic2markdown:25:in `<main>'

Support for nested list (ex: <ol> in <li>)

Hello,

I am encountering the following error : block in extract_content': undefined method extract_span_ol' while exporting some wordpress content.

Would it be possible for you to add support for nested list please ?

Thank you very much,
Yohann

Add support for HTML comments

This is a great package! Thanks for writing it.

I've encountered some HTML with comments, such as <!-- Main -->, and always get the following error: undefined method 'extract_span_xml_comment'. It would be helpful if kramdown-prismic could support this in some way instead of failing. I don't actually need the comments converted โ€” stripping them would be all right.

Add support for <strong> tags

I found an error similar to #5, but this time when importing HTML with a <strong> tag. Kramdown-prismic throws the error: undefined method 'convert_strong'. Sorry for not catching this one when reporting the previous!

Support image with link

Hello - thanks for the amazing work!
Do you think you will support images with a link in the future?

<a href="https://www.google.com"><img src="https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_92x30dp.png" alt="Google Logo"></a>

to

{
  "type": "image",
  "content": { "text": "", "spans": [] },
  "data": {
    "origin": {
      "url": "https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_92x30dp.png"
    },
    "alt": "Google Logo",
    "linkTo": {
      "url": "https://www.google.com",
      "preview": {
        "title": "https://www.google.com"
      }
    }
  }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.