All Projects → dillonkearns → Elm Markdown

dillonkearns / Elm Markdown

Licence: bsd-3-clause
Extensible markdown parser with custom rendering, in pure Elm.

Programming Languages

elm
856 projects

Projects that are alternatives of or similar to Elm Markdown

Fakeiteasy
The easy mocking library for .NET
Stars: ✭ 1,092 (+1750.85%)
Mutual labels:  hacktoberfest
Esp8266 deauther
Affordable WiFi hacking platform for testing and learning
Stars: ✭ 9,312 (+15683.05%)
Mutual labels:  hacktoberfest
Box Shadows
Box Shadows - Handpicked Box-Shadows for Developers and Designers
Stars: ✭ 58 (-1.69%)
Mutual labels:  hacktoberfest
Tarpaulin
A code coverage tool for Rust projects
Stars: ✭ 1,097 (+1759.32%)
Mutual labels:  hacktoberfest
Containerd
An open and reliable container runtime
Stars: ✭ 9,956 (+16774.58%)
Mutual labels:  hacktoberfest
Openapi Generator
OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
Stars: ✭ 10,634 (+17923.73%)
Mutual labels:  hacktoberfest
Vscode Bcdn
A Plugin for easy bootstrap markup and template
Stars: ✭ 58 (-1.69%)
Mutual labels:  hacktoberfest
Laminas Migration
Migrate Zend Framework MVC applications, Expressive applications, Apigility applications, or third-party libraries to target Laminas.
Stars: ✭ 58 (-1.69%)
Mutual labels:  hacktoberfest
Thanos
Highly available Prometheus setup with long term storage capabilities. A CNCF Incubating project.
Stars: ✭ 9,820 (+16544.07%)
Mutual labels:  hacktoberfest
Webtorrent Desktop
❤️ Streaming torrent app for Mac, Windows, and Linux
Stars: ✭ 8,587 (+14454.24%)
Mutual labels:  hacktoberfest
Godo
DigitalOcean Go API client
Stars: ✭ 1,097 (+1759.32%)
Mutual labels:  hacktoberfest
Drf Nested Routers
Nested Routers for Django Rest Framework
Stars: ✭ 1,098 (+1761.02%)
Mutual labels:  hacktoberfest
Post Mortems
A collection of postmortems. Sorry for the delay in merging PRs!
Stars: ✭ 8,772 (+14767.8%)
Mutual labels:  hacktoberfest
Web
Grow Open Source
Stars: ✭ 1,097 (+1759.32%)
Mutual labels:  hacktoberfest
Hooking Template With Mod Menu
A small template for Android Hooking with Substrate. (Includes a mod menu written in Java)
Stars: ✭ 59 (+0%)
Mutual labels:  hacktoberfest
Mattermost Plugin Jira
JIRA plugin for Mattermost 🔌
Stars: ✭ 58 (-1.69%)
Mutual labels:  hacktoberfest
Vulcan
🌋 A toolkit to quickly build apps with React, GraphQL & Meteor
Stars: ✭ 8,027 (+13505.08%)
Mutual labels:  hacktoberfest
Awesome Material Ui
A curated list of Material-UI resources and related projects. The main idea is that everyone can contribute here, so we can have a central repository of informations about Material-UI that we keep up-to-date
Stars: ✭ 57 (-3.39%)
Mutual labels:  hacktoberfest
Holysheet
A program to store arbitrary files in Google Sheets
Stars: ✭ 59 (+0%)
Mutual labels:  hacktoberfest
Django Cms
The easy-to-use and developer-friendly enterprise CMS powered by Django
Stars: ✭ 8,522 (+14344.07%)
Mutual labels:  hacktoberfest

elm-markdown

All Contributors Build Status Elm package

Extensible markdown parsing in pure elm.

This library extends the basic markdown blocks without actually adding features to the syntax. It simply provides a declarative way to map certain HTML tags to your Elm view functions to render them. For example,

<bio
  name="Dillon Kearns"
  photo="https://avatars2.githubusercontent.com/u/1384166"
  twitter="dillontkearns"
  github="dillonkearns"
>
  Dillon really likes building things with Elm! Here are some links -
  [Articles](https://incrementalelm.com/articles)
</bio>

And you wire up your Elm rendering function like this

Markdown.Html.oneOf
  [ Markdown.Html.tag "bio"
    (\name photoUrl twitter github dribbble renderedChildren ->
      bioView renderedChildren name photoUrl twitter github dribbble
    )
    |> Markdown.Html.withAttribute "name"
    |> Markdown.Html.withAttribute "photo"
    |> Markdown.Html.withOptionalAttribute "twitter"
    |> Markdown.Html.withOptionalAttribute "github"
    |> Markdown.Html.withOptionalAttribute "dribbble"
  ]

Note that it gets the rendered children as an argument. This is rendering the inner contents of the HTML tag using your HTML renderer, so you get all of your rendered lists, code blocks, links, etc. within your tag. You can try a live Ellie demo of this code snippet.

Live Code Demos

Core features

Custom Renderers

You define your own custom renderer, turning your markdown content into any data type with totally customizable logic. You can even pass back an Err to get custom failures (for example, broken links or validations like headings that are too long)!

Here's a snippet from the default HTML renderer that comes built in to give you a sense of how you define a Renderer:

import Html exposing (Html)
import Html.Attributes as Attr
import Markdown.Block as Block exposing (Block)
import Markdown.Html

defaultHtmlRenderer : Renderer (Html msg)
defaultHtmlRenderer =
    { heading =
        \{ level, children } ->
            case level of
                Block.H1 ->
                    Html.h1 [] children

                Block.H2 ->
                    Html.h2 [] children

                Block.H3 ->
                    Html.h3 [] children

                Block.H4 ->
                    Html.h4 [] children

                Block.H5 ->
                    Html.h5 [] children

                Block.H6 ->
                    Html.h6 [] children
    , paragraph = Html.p []
    , hardLineBreak = Html.br [] []
    , blockQuote = Html.blockquote []
    , strong =
        \children -> Html.strong [] children
    , emphasis =
        \children -> Html.em [] children
    , codeSpan =
        \content -> Html.code [] [ Html.text content ]
    , link =
        \link content ->
            case link.title of
                Just title ->
                    Html.a
                        [ Attr.href link.destination
                        , Attr.title title
                        ]
                        content

                Nothing ->
                    Html.a [ Attr.href link.destination ] content
    , image =
        \imageInfo ->
            case imageInfo.title of
                Just title ->
                    Html.img
                        [ Attr.src imageInfo.src
                        , Attr.alt imageInfo.alt
                        , Attr.title title
                        ]
                        []

                Nothing ->
                    Html.img
                        [ Attr.src imageInfo.src
                        , Attr.alt imageInfo.alt
                        ]
                        []
    , text =
        Html.text
    , unorderedList =
        \items ->
            Html.ul []
                (items
                    |> List.map
                        (\item ->
                            case item of
                                Block.ListItem task children ->
                                    let
                                        checkbox =
                                            case task of
                                                Block.NoTask ->
                                                    Html.text ""

                                                Block.IncompleteTask ->
                                                    Html.input
                                                        [ Attr.disabled True
                                                        , Attr.checked False
                                                        , Attr.type_ "checkbox"
                                                        ]
                                                        []

                                                Block.CompletedTask ->
                                                    Html.input
                                                        [ Attr.disabled True
                                                        , Attr.checked True
                                                        , Attr.type_ "checkbox"
                                                        ]
                                                        []
                                    in
                                    Html.li [] (checkbox :: children)
                        )
                )
    , orderedList =
        \startingIndex items ->
            Html.ol
                (case startingIndex of
                    1 ->
                        [ Attr.start startingIndex ]

                    _ ->
                        []
                )
                (items
                    |> List.map
                        (\itemBlocks ->
                            Html.li []
                                itemBlocks
                        )
                )
    , html = Markdown.Html.oneOf []
    , codeBlock =
        \{ body, language } ->
            Html.pre []
                [ Html.code []
                    [ Html.text body
                    ]
                ]
    , thematicBreak = Html.hr [] []
    , table = Html.table []
    , tableHeader = Html.thead []
    , tableBody = Html.tbody []
    , tableRow = Html.tr []
    , tableHeaderCell =
        \maybeAlignment ->
            let
                attrs =
                    maybeAlignment
                        |> Maybe.map
                            (\alignment ->
                                case alignment of
                                    Block.AlignLeft ->
                                        "left"

                                    Block.AlignCenter ->
                                        "center"

                                    Block.AlignRight ->
                                        "right"
                            )
                        |> Maybe.map Attr.align
                        |> Maybe.map List.singleton
                        |> Maybe.withDefault []
            in
            Html.th attrs
    , tableCell = Html.td []
    }

Markdown Block Transformation

You get full access to the parsed markdown blocks before passing it to a renderer. That means that you can inspect it, do custom logic on it, perform validations, or even go in and transform it! It's totally customizable, and of course it's all just nice Elm custom types!

Here's a live Ellie example that transforms the AST into a table of contents and renders a TOC data type along with the rendered markdown.

Philosophy & Goals

  • Render markdown to any type (Html, elm-ui Elements, Strings representing ANSI color codes for terminal output... or even a function, allowing you to inject dynamic values into your markdown view)
  • Extend markdown without adding to the syntax using custom HTML renderers, and fail explicitly for unexpected HTML tags, or missing attributes within those tags
  • Allow users to give custom parsing failures with nice error messages (for example, broken links, or custom validation like titles that are too long)

Parsing Goals

This is evolving and I would like input on the direction of parsing. My current thinking is that this library should:

  • Do not add any new syntax, this library has a subset of the features of Github flavored markdown.
  • Only parse the Github-flavored markdown style (not CommonMark or other variants)
  • (This breaks GFM compliance in favor of explicit errors) All markdown is valid in github-flavored markdown and other variants. This library aims to give explicit errors instead of falling back and silently continuing, see example below
  • Only deviate from Github-flavored markdown rules when it helps give better error feedback for "things you probably didn't mean to do." In all other cases, follow the Github-flavored markdown spec.

Current Github-flavored markdown compliance

The test suite for this library runs through all the expected outputs outlined in the GFM spec. It uses the same test suite to test these cases as highlight.js (the library that elm-explorations/elm-markdown uses under the hood).

You can see the latest passing and failing tests from this test suite in the test-results folder (in particular, take a look at the Github-Flavored Markdown failures in in failing/GFM.

Examples of fallback behavior

Github flavored markdown behavior: Links with missing closing parens are are rendered as raw text instead of links

[My link](/home/ wait I forgot to close the link

Renders the raw string instead of a link, like so:

<p>
  [My link](/home/ wait I forgot to close the link
</p>

This library gives an error message here, and aims to do so in similar situations.

Contributors

A huge thanks to Pablo Hirafuji, who was kind enough to allow me to use his InlineParser in this project. It turns out that Markdown inline parsing is a very specialized algorithm, and the elm/parser library isn't suited to solve that particular problem.


Stephen Reddekopp

⚠️ 💻

thomasin

⚠️ 💻

Brian Ginsburg

⚠️ 💻

Philipp Krüger

💻

Folkert de Vries

💻

Thank you @jinjor for your elm-xml-parser package!

I needed to tweak it so I copied it into the project, but it is one of the dependencies and it worked without a hitch!

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].