• Stars
    star
    691
  • Rank 65,435 (Top 2 %)
  • Language
    Kotlin
  • License
    Apache License 2.0
  • Created almost 10 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Markdown parser written in kotlin

intellij-markdown official JetBrains project Maven Central IR

A multiplatform Markdown processor written in Kotlin.

Introduction

intellij-markdown is an extensible Markdown processor written in Kotlin. It aims to suit the following needs:

  • Use one code base for both client and server-side processing
  • Produce consistent output on different platforms
  • Support different Markdown flavours
  • Be easily extensible

The processor is written in pure Kotlin (with a little flex), so it can be compiled not only for the JVM target, but for JS and Native. This allows for the processor to be used everywhere.

Usage

Adding intellij-markdown as a dependency

The library is hosted in the Maven Central Repository, so to be able to use it, you need to configure the central repository:

repositories {
  mavenCentral()
}

If you have Gradle >= 5.4, you can just add the main artifact as a dependency:

dependencies {
  implementation("org.jetbrains:markdown:<version>")
}

Gradle should resolve your target platform and decide which artifact (JVM or JS) to download.

For the multiplatform projects you can the single dependency to the commonMain class path:

commonMain {
  dependencies {
    implementation("org.jetbrains:markdown:<version>")
  }
}

If you are using Maven or older Gradle, you need to specify the correct artifact for your platform, e.g.:

  • org.jetbrains:markdown-jvm:<version> for the JVM version
  • org.jetbrains:markdown-js:<version> for the JS version

Using intellij-markdown for parsing and generating HTML

One of the goals of this project is to provide flexibility in terms of the tasks being solved. Markdown Plugin for JetBrains IDEs is an example of a usage when Markdown processing is done in several stages:

  1. Parse block structure without parsing inlines to provide lazy parsable blocks for IDE
  2. Quickly parse inlines of a given block to provide faster syntax highlighting update
  3. Generate HTML for preview

These tasks may be completed independently according to the current needs.

Simple html generation (Kotlin)

val src = "Some *Markdown*"
val flavour = CommonMarkFlavourDescriptor()
val parsedTree = MarkdownParser(flavour).buildMarkdownTreeFromString(src)
val html = HtmlGenerator(src, parsedTree, flavour).generateHtml()

Simple html generation (Java)

final String src = "Some *Markdown*";
final MarkdownFlavourDescriptor flavour = new GFMFlavourDescriptor();
final ASTNode parsedTree = new MarkdownParser(flavour).buildMarkdownTreeFromString(text);
final String html = new HtmlGenerator(src, parsedTree, flavour, false).generateHtml();

Development gotchas

The only non-Kotlin files are .flex lexer definitions. They are used for generating lexers, which are the first stage of inline elements parsing. Unfortunately, due to bugs, native java->kt conversion crashes for these files. Because of that, conversion from .flex to respective Kotlin files requires some manual steps:

  1. Install Grammar Kit plugin. It should be suggested on the opening of any .flex file.
  2. Install jflexToKotlin plugin (you will need to build it and then install it manually, via settings).
  3. Run Run JFlex Generator action while having .flex file opened.
    • On the first run, a dialog will open, suggesting to place to download JFlex - select the project root, then delete excessively downloaded .skeleton file.
  4. A respective _<SomeName>Lexer.java will be generated somewhere. Move it near the existing _<SomeName>Lexer.kt.
  5. Delete the .kt lexer.
  6. Run Convert JFlex Lexer to Kotlin action while having the new .java file opened.
  7. Fix the small problems such as imports in the generated .kt file. There should be no major issues. Please try to minimize the number of changes to the generated files. This is needed for keeping a clean Git history.

Parsing algorithm

The parsing process is held in two logical parts:

  1. Splitting the document into blocks of logical structure (lists, blockquotes, paragraphs, etc.)
  2. Parsing the inline structure of the resulting blocks

This is the same way as the one being proposed by the Commonmark spec.

Building the logical structure

Each (future) node (list, list item, blockquote, etc.) is associated with the so-called MarkerBlock. The rollback-free parsing algorithm is processing every token in the file, one by one. Tokens are passed to the opened marker blocks, and each block chooses whether to either:

  • do nothing
  • drop itself
  • complete itself

The MarkerProcessor stores the blocks, executes the actions chosen by the blocks, and, possibly, adds some new ones.

Parsing inlines

For the sake of speed and parsing convenience, the text is passed to the MarkdownLexer first. Then the resulting set of tokens is processed in a special way.

Some inline constructs in Markdown have priorities, i.e., if two different ones overlap, the parsing result depends on their types, not their positions - e.g. *code, `not* emph` and `code, *not` emph* are both code spans + literal asterisks. This means that normal recursive parsing is inapplicable.

Still, the parsing of inline elements is quite straightforward. For each inline construct, there is a particular SequentialParser which accepts some input text and returns:

  1. The parsed ranges found in this text;
  2. The sub-text(s), which are to be passed to the subsequent inline parsers.

Building AST

After building the logical structure and parsing inline elements, a set of ranges corresponding to some markdown entities (i.e. nodes) is given. In order to work with the results effectively, it ought to be converted to the AST.

As a result, a root ASTNode corresponding to the parsed Markdown document is returned. Each AST node has its own type which is called IElementType as in the IntelliJ Platform.

Generating HTML

For a given AST root, a special visitor to generate the resulting HTML is created. Using a given mapping from IElementType to the GeneratingProvider it processes the parsed tree in Depth-First order, generating HTML pieces for on each node visit.

Extending the parser

Many routines in the above process can be extended or redefined by creating a different Markdown flavour. The minimal default flavour is CommonMark which is implemented in this project.

GitHub Flavoured Markdown is an example of extending CommonMark flavour implementation. It can be used as a reference for implementing your own Markdown features.

API

  • MarkdownFlavourDescriptor is a base class for extending the Markdown parser.
    • markerProcessorFactory is responsible for block structure customization.

      • stateInfo value allows to use a state during document parsing procedure.

        updateStateInfo(pos: LookaheadText.Position) is called at the beginning of each position processing

      • populateConstraintsTokens is called to create nodes for block structure markers at the beginning of the lines (for example, > characters constituting blockquotes)

      • getMarkerBlockProviders is a place to (re)define types of block structures

    • sequentialParserManager

      getParserSequence defines inlines parsing procedure. The method must return a list of SequentialParsers where the earliest parsers have the biggest operation precedence. For example, to parse code spans and emphasis elements with the correct priority, the list should be [CodeSpanParser, EmphParser] but not the opposite.

      SequentialParser has only one method:

      parse(tokens: TokensCache, rangesToGlue: List<IntRange>): ParsingResult

      • tokens is a special holder for the tokens returned by lexer

      • rangesToGlue is a list of ranges in the document which are to be searched for the structures in question.

        Considering the input: A * emph `code * span` b * c for the emph parser ranges [A * emph , b * c] mean that emph must be searched in the input A * emph | b * c.

        The method must essentially return the parsing result (nodes for the found structures) and the parts of the text to be given to the next parsers.

        Considering the same input for the code span parser the result would be `code * span` of the type "code span" and the delegate pieces would be [A * emph , b * c].

    • createInlinesLexer should return the lexer to split the text to the tokens before inline parsing procedure run.

    • createHtmlGeneratingProviders(linkMap: LinkMap, baseURI: URI?) is the place where generated HTML is customized. This method should return a map which defines how to handle the particular kinds of the nodes in the resulting tree.

      linkMap here is precalculated information about the links defined in the document with the means of link definition. baseURI is the URI to be considered the base path for the relative links resolving. For example, given baseUri='/user/repo-name/blob/master' the link foo/bar.png should be transformed to the /user/repo-name/blob/master/foo/bar.png.

      Each returned provider must implement processNode(visitor: HtmlGenerator.HtmlGeneratingVisitor, text: String, node: ASTNode) where

      • text is the whole document being processed,
      • node is the node being given to the provider,
      • visitor is a special object responsible for the HTML generation. See GeneratingProviders.kt for the samples.

More Repositories

1

kotlin

The Kotlin Programming Language.
Kotlin
48,745
star
2

intellij-community

IntelliJ IDEA Community Edition & IntelliJ Platform
Java
17,135
star
3

compose-multiplatform

Compose Multiplatform, a modern UI framework for Kotlin that makes building performant and beautiful user interfaces easy and enjoyable.
Kotlin
16,179
star
4

JetBrainsMono

JetBrains Mono โ€“ the free and open-source typeface for developers
Shell
10,765
star
5

Exposed

Kotlin SQL Framework
Kotlin
8,296
star
6

ideavim

IdeaVim โ€“ A Vim engine for JetBrains IDEs
Kotlin
7,980
star
7

kotlin-native

Kotlin/Native infrastructure
Kotlin
7,026
star
8

ring-ui

A collection of JetBrains Web UI components
TypeScript
3,595
star
9

intellij-platform-plugin-template

Template repository for creating plugins for IntelliJ Platform
Kotlin
3,048
star
10

kotlinconf-app

KotlinConf Schedule Application
Kotlin
2,967
star
11

skija

Java bindings for Skia
Java
2,628
star
12

create-react-kotlin-app

Create React apps using Kotlin with no build configuration
JavaScript
2,480
star
13

projector-docker

Run JetBrains IDEs remotely with Docker
Shell
2,235
star
14

intellij-plugins

Open-source plugins included in the distribution of IntelliJ IDEA Ultimate and other IDEs based on the IntelliJ Platform
Java
2,002
star
15

svg-sprite-loader

Webpack loader for creating SVG sprites.
JavaScript
1,998
star
16

skiko

Kotlin Multiplatform bindings to Skia
C++
1,825
star
17

compose-multiplatform-ios-android-template

Compose Multiplatform iOS+Android Application project template
Kotlin
1,600
star
18

lets-plot

Multiplatform plotting library based on the Grammar of Graphics
Kotlin
1,559
star
19

MPS

JetBrains Meta programming System
JetBrains MPS
1,527
star
20

kotlin-web-site

The Kotlin programming language website
JavaScript
1,474
star
21

intellij-platform-gradle-plugin

Gradle plugin for building plugins for IntelliJ-based IDEs
Kotlin
1,426
star
22

kotlin-wrappers

Kotlin wrappers for popular JavaScript libraries
Kotlin
1,339
star
23

phpstorm-stubs

PHP runtime & extensions header files for PhpStorm
PHP
1,317
star
24

JetBrainsRuntime

Runtime environment based on OpenJDK for running IntelliJ Platform-based products on Windows, macOS, and Linux
Java
1,312
star
25

idea-gitignore

.ignore support plugin for IntelliJ IDEA
Kotlin
1,290
star
26

projector-server

Server-side library for running Swing applications remotely
Kotlin
1,246
star
27

intellij-sdk-docs

IntelliJ SDK Platform Documentation
Markdown
1,212
star
28

resharper-unity

Unity support for both ReSharper and Rider
C#
1,209
star
29

intellij-scala

Scala plugin for IntelliJ IDEA
Scala
1,192
star
30

xodus

Transactional schema-less embedded database used by JetBrains YouTrack and JetBrains Hub.
Java
1,180
star
31

kotless

Kotlin Serverless Framework
Kotlin
1,147
star
32

intellij-sdk-code-samples

Mirror of the IntelliJ SDK Docs Code Samples
Java
1,074
star
33

amper

Amper - a project configuration and build tool with a focus on the user experience and the IDE support
Kotlin
995
star
34

js-graphql-intellij-plugin

GraphQL language support for WebStorm, IntelliJ IDEA and other IDEs based on the IntelliJ Platform.
Java
879
star
35

android

Android Plugin for IntelliJ IDEA. This repository is a subset of https://git.jetbrains.org/?p=idea/android.git cut according to GitHub file size limitations.
Kotlin
863
star
36

projector-installer

Install, configure and run JetBrains IDEs with Projector Server on Linux or in WSL
Python
808
star
37

projector-client

Common and client-related code for running Swing applications remotely
Kotlin
806
star
38

Grammar-Kit

Grammar files support & parser/PSI generation for IntelliJ IDEA
Java
715
star
39

jewel

An implementation of the IntelliJ look and feels in Compose for Desktop
Kotlin
709
star
40

Arend

The Arend Proof Assistant
Java
691
star
41

jediterm

Pure Java Terminal Emulator. Works with SSH and PTY.
Java
675
star
42

compose-multiplatform-template

Compose Multiplatform Application project template
Kotlin
623
star
43

lincheck

Framework for testing concurrent data structures
Kotlin
576
star
44

Nitra

Public Nitra repository
Nemerle
549
star
45

intellij-micropython

Plugin for MicroPython devices in PyCharm and IntelliJ
Python
506
star
46

compose-multiplatform-desktop-template

Compose Multiplatform Desktop Application project template
Kotlin
447
star
47

kotlin-playground

Self-contained component to embed in websites for running Kotlin code
JavaScript
440
star
48

lets-plot-kotlin

Grammar of Graphics for Kotlin
Kotlin
434
star
49

java-annotations

Annotations for JVM-based languages.
Kotlin
405
star
50

colorSchemeTool

Python
396
star
51

Qodana

๐Ÿ“ Source repository of Qodana Help
389
star
52

phpstorm-attributes

PhpStorm specific attributes
PHP
388
star
53

godot-support

C#
385
star
54

rd

Reactive Distributed communication framework for .NET, Kotlin, C++. Inspired by Rider IDE.
C#
385
star
55

pty4j

Pty for Java
Java
354
star
56

Unity3dRider

Unity JetBrains Rider integration
348
star
57

resharper-fsharp

F# support in JetBrains Rider
F#
328
star
58

phpstorm-workshop

Code for the PhpStorm workshop
PHP
287
star
59

web-types

JSON standard for documenting web component libraries for IDEs, documentation generators and other tools
TypeScript
280
star
60

awesome-pycharm

A curated list of resources for learning and using PyCharm, a Python IDE from JetBrains
280
star
61

gradle-changelog-plugin

Plugin for parsing and managing the Changelog in a "keep a changelog" style.
Kotlin
266
star
62

youtrack-mobile

A iOS and Android client for YouTrack
TypeScript
265
star
63

meta-runner-power-pack

A set of Meta-runners for TeamCity
PowerShell
256
star
64

inspection-plugin

Gradle plugin to launch IDEA inspections
Kotlin
256
star
65

ideolog

Interactive viewer for '.log' files.
Kotlin
254
star
66

qodana-action

โš™๏ธ Scan your Go, Java, Kotlin, PHP, Python, JavaScript, TypeScript, .NET projects at GitHub with Qodana. This repository contains Qodana for Azure, GitHub, CircleCI and Gradle
JavaScript
248
star
67

jcef

A simple framework for embedding Chromium-based browsers into Java-based applications.
Java
245
star
68

kotlin-compiler-server

Server for executing kotlin code
Kotlin
241
star
69

gradle-idea-ext-plugin

Plugin to store IJ settings in gradle script
Groovy
233
star
70

resharper-rider-samples

Simple interactive exercises to help learn ReSharper and Rider
C#
225
star
71

php-timeline

Notable events of PHP history
222
star
72

la-clojure

Clojure plugin for IntelliJ IDEA
Java
220
star
73

jdk8u_jdk

Java
212
star
74

pest-intellij

The official Pest Plugin for PhpStorm / IntelliJ IDEA
Kotlin
197
star
75

youtrack-workflows

YouTrack Custom Workflow Repository
JavaScript
194
star
76

psiviewer

PSI Viewer for IntelliJ IDEA plugin development
Java
184
star
77

intellij-plugin-verifier

Compatibility verification tool for IntelliJ Platform plugins
Kotlin
178
star
78

resharper-rider-plugin

https://www.jetbrains.com/help/resharper/sdk/
PowerShell
175
star
79

rider-efcore

Entity Framework Core UI plugin for JetBrains Rider
Kotlin
174
star
80

svg-mixer

Node.js toolset for generating & transforming SVG images and sprites in modern way
JavaScript
173
star
81

qodana-cli

๐Ÿ”ง JetBrains Qodanaโ€™s official command line tool
Go
171
star
82

phpstorm-docker-images

Pre-configured Docker images used by PhpStorm team
Dockerfile
171
star
83

compose-for-web-demos

Historical repository of early Compose for Web effort.
170
star
84

kotlin-web-demo

Online mini-IDE for Kotlin
Kotlin
167
star
85

intellij-samples

Code that demonstrates various IntelliJ IDEA features
Java
167
star
86

jetbrains_guide

JetBrains Guides where Developer Advocacy and the community share ideas.
CSS
164
star
87

jdk8u_hotspot

C++
159
star
88

educational-plugin

Educational plugin to learn and teach programming languages such as Kotlin, Java, Python, JavaScript, and others right inside of JetBrains IntelliJ Platform based IDEs.
Kotlin
152
star
89

teamcity-messages

Python Unit Test Reporting to TeamCity
Python
139
star
90

lets-plot-skia

Skia frontend for Lets-Plot multiplatform plotting library.
Kotlin
137
star
91

ruby-type-inference

Dynamic definitions and types provider for ruby static analysis
Kotlin
137
star
92

YouTrackSharp

.NET Standard 2.0 Library to access YouTrack API.
C#
134
star
93

clion-remote

134
star
94

resharper-angularjs

ReSharper plugin for AngularJS support
JavaScript
134
star
95

UnrealLink

C++
132
star
96

golandtipsandtricks

This is an ever evolving repository for GoLand Tips&Tricks
Go
132
star
97

python-skeletons

The python-skeltons repo is deprecated: use PEP 484 and Typeshed instead
Python
132
star
98

phpstorm-phpstan-plugin

PHPStan plugin for PhpStorm
Java
131
star
99

clion-wsl

Shell
130
star
100

teamcity-docker-samples

TeamCity docker compose samples
Shell
128
star