Browse Source

doc: continue restructuring & theme fixing

pull/1197/head
Vitaly Puzrin 2 months ago
parent
commit
f5083f4d1c
  1. 4
      CHANGELOG.md
  2. 189
      README.md
  3. 10
      docs/README.md
  4. 0
      docs/migration/4.0.md
  5. 0
      docs/migration/5.0.md
  6. 10
      docs/safety.md
  7. 53
      docs/syntax_plugins.md
  8. 137
      docs/usage.md
  9. 76
      src/markdownit.ts
  10. 42
      src/types.ts
  11. 49
      support/typedoc-custom.css
  12. 15
      support/typedoc-oxide-fixes.mjs
  13. 7
      typedoc.config.mjs

4
CHANGELOG.md

@ -438,7 +438,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Internal API change. Due to new CM spec requirements, we had to update
internals. That should not touch ordinary users, but can affect some external
plugins. If you are plugin developper - see migration guide:
https://github.com/markdown-it/markdown-it/blob/master/docs/5.0_migration.md.
https://github.com/markdown-it/markdown-it/blob/master/docs/migration/5.0.md.
- Updated CM spec compatibility to 0.22 (see list below).
- Keep tabs (don't replace with spaces).
- Don't wrap iframes with paragraphs.
@ -539,7 +539,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [4.0.0] - 2015-03-11
### Changed
- Breaking internal API changes. See [v4 migration notes](https://github.com/markdown-it/markdown-it/blob/master/docs/4.0_migration.md). In usual case you will need to update plugins.
- Breaking internal API changes. See [v4 migration notes](https://github.com/markdown-it/markdown-it/blob/master/docs/migration/4.0.md). In usual case you will need to update plugins.
- Token internals changed
- Unified the most of renderer methods.
- Changed tokens creation - use `state.push(...)` (see sources)

189
README.md

@ -1,4 +1,4 @@
# markdown-it <!-- omit in toc -->
# markdown-it
[![CI](https://github.com/markdown-it/markdown-it/actions/workflows/ci.yml/badge.svg)](https://github.com/markdown-it/markdown-it/actions/workflows/ci.yml)
[![NPM version](https://img.shields.io/npm/v/markdown-it.svg?style=flat)](https://www.npmjs.org/package/markdown-it)
@ -8,18 +8,13 @@
__[Live demo](https://markdown-it.github.io)__
- Follows the __[CommonMark spec](http://spec.commonmark.org/)__ + adds syntax extensions & sugar (URL autolinking, typographer).
- Follows the [CommonMark spec](http://spec.commonmark.org/) + adds syntax extensions & sugar (URL autolinking, typographer).
- Configurable syntax! You can add new rules and even replace existing ones.
- High speed.
- [Safe](https://github.com/markdown-it/markdown-it/tree/master/docs/safety.md) by default.
- Community-written __[plugins](https://www.npmjs.org/browse/keyword/markdown-it-plugin)__ and [other packages](https://www.npmjs.org/browse/keyword/markdown-it) on npm.
- Safe by default.
- Community-written __[plugins](https://www.npmjs.org/browse/keyword/markdown-it-plugin)__
and [other packages](https://www.npmjs.org/browse/keyword/markdown-it) on npm.
__Table of content__
- [Install](#install)
- [Usage examples](#usage-examples)
- [API](#api)
- [Syntax extensions](#syntax-extensions)
## Install
@ -34,187 +29,21 @@ npm install markdown-it
> For a quick look at `dist/` folder contents, see
> <https://unpkg.com/markdown-it/>.
>
> For browser you can use unpkg.com, esm.sh or any other CDN, wich mirror npm
> For browser you can use unpkg.com, esm.sh or any other CDN, which mirror npm
> registry
## Usage examples
See also:
- __[API documentation](https://markdown-it.github.io/markdown-it/)__ - for more
info and examples.
- [Development info](https://github.com/markdown-it/markdown-it/tree/master/docs) -
for plugins writers.
**Simple** <!-- omit in toc -->
## Usage
```js
// node.js
import MarkdownIt from 'markdown-it'
const md = new MarkdownIt()
const result = md.render('# markdown-it rulezz!');
// browser with UMD build, added to "window" on script load
// Note, there is no dash in "markdownit".
const md = new window.markdownit();
const result = md.render('# markdown-it rulezz!');
const result = md.render('# markdown-it rulezz!')
```
Single line rendering, without paragraph wrap:
```js
import MarkdownIt from 'markdown-it'
const md = new MarkdownIt()
const result = md.renderInline('__markdown-it__ rulezz!');
```
**Init with presets and options** <!-- omit in toc -->
(*) presets define combinations of active rules and options. Can be
`"commonmark"`, `"zero"` or `"default"` (if skipped). See
[API docs](https://markdown-it.github.io/markdown-it/classes/MarkdownIt.html#constructor.constructor) for more details.
```js
import MarkdownIt from 'markdown-it'
// commonmark mode
const md = new MarkdownIt('commonmark')
// default mode
const md = new MarkdownIt()
// enable everything
const md = new MarkdownIt({
html: true,
linkify: true,
typographer: true
})
```
See [MarkdownItOptions](https://markdown-it.github.io/markdown-it/interfaces/MarkdownItOptions.html)
for the full options list.
**Plugins load** <!-- omit in toc -->
```js
import MarkdownIt from 'markdown-it'
const md = new MarkdownIt()
.use(plugin1)
.use(plugin2, opts, ...)
.use(plugin3);
```
**Syntax highlighting** <!-- omit in toc -->
Apply syntax highlighting to fenced code blocks with the `highlight` option:
```js
import MarkdownIt from 'markdown-it'
import hljs from 'highlight.js' // https://highlightjs.org
// Actual default values
const md = new MarkdownIt({
highlight: function (str, lang) {
if (lang && hljs.getLanguage(lang)) {
try {
return hljs.highlight(str, { language: lang }).value;
} catch (__) {}
}
return ''; // use external default escaping
}
});
```
Or with full wrapper override (if you need assign class to `<pre>` or `<code>`):
```js
import MarkdownIt from 'markdown-it'
import hljs from 'highlight.js' // https://highlightjs.org
// Actual default values
const md = new MarkdownIt({
highlight: function (str, lang) {
if (lang && hljs.getLanguage(lang)) {
try {
return `<pre><code class="hljs">${hljs.highlight(str,
{ language: lang, ignoreIllegals: true }).value}</code></pre>`;
} catch (__) {}
}
return `<pre><code class="hljs">${md.utils.escapeHtml(str)}</code></pre>`;
}
});
```
**Linkify** <!-- omit in toc -->
`linkify: true` uses [linkify-it](https://github.com/markdown-it/linkify-it). To
configure linkify-it, access the linkify instance through `md.linkify`:
```js
md.linkify.set({ fuzzyEmail: false }); // disables converting email to link
```
[More usage examples](docs/usage.md).
## API
__[API documentation](https://markdown-it.github.io/markdown-it/)__
If you are going to write plugins, please take a look at
[Development info](https://github.com/markdown-it/markdown-it/tree/master/docs).
## Syntax extensions
Embedded (enabled by default):
- [Tables](https://help.github.com/articles/organizing-information-with-tables/) (GFM)
- [Strikethrough](https://help.github.com/articles/basic-writing-and-formatting-syntax/#styling-text) (GFM)
Via plugins:
- [subscript](https://github.com/markdown-it/markdown-it-sub)
- [superscript](https://github.com/markdown-it/markdown-it-sup)
- [footnote](https://github.com/markdown-it/markdown-it-footnote)
- [definition list](https://github.com/markdown-it/markdown-it-deflist)
- [abbreviation](https://github.com/markdown-it/markdown-it-abbr)
- [emoji](https://github.com/markdown-it/markdown-it-emoji)
- [custom container](https://github.com/markdown-it/markdown-it-container)
- [insert](https://github.com/markdown-it/markdown-it-ins)
- [mark](https://github.com/markdown-it/markdown-it-mark)
- ... and [others](https://www.npmjs.org/browse/keyword/markdown-it-plugin)
**Manage rules** <!-- omit in toc -->
By default all rules are enabled, but can be restricted by options. On plugin
load all its rules are enabled automatically.
```js
import MarkdownIt from 'markdown-it'
// Activate/deactivate rules, with currying
const md = new MarkdownIt()
.disable(['link', 'image'])
.enable(['link'])
.enable('image');
// Enable everything
const md = new MarkdownIt({
html: true,
linkify: true,
typographer: true,
});
```
You can find all rules in sources:
- [`ParserCore`](https://github.com/markdown-it/markdown-it/blob/master/src/parser_core.ts)
- [`ParserBlock`](https://github.com/markdown-it/markdown-it/blob/master/src/parser_block.ts)
- [`ParserInline`](https://github.com/markdown-it/markdown-it/blob/master/src/parser_inline.ts)

10
docs/README.md

@ -1,10 +0,0 @@
This folder contains useful info for plugin developers.
If you just use `markdown-it` in your app, see
[README](https://github.com/markdown-it/markdown-it#markdown-it) and
[API docs](https://markdown-it.github.io/markdown-it/).
__Content__:
- [Parser architecture & design principles](architecture.md)
- [Some guidelines for plugin developers](development.md)

0
docs/4.0_migration.md → docs/migration/4.0.md

0
docs/5.0_migration.md → docs/migration/5.0.md

10
docs/safety.md

@ -1,3 +1,8 @@
---
title: Safety
category: Main
---
# Safety
Many people don't understand that markdown format does not care much about
@ -18,8 +23,9 @@ for XSS:
So, by default `markdown-it` should be safe. We care about it.
If you find a security problem - contact us via tracker or email. Such reports
are fixed with top priority.
If you find a security problem, please
[report it privately](https://github.com/markdown-it/markdown-it/security/advisories/new).
Such reports are fixed with top priority.
## Plugins

53
docs/syntax_plugins.md

@ -0,0 +1,53 @@
---
title: Syntax plugins
category: Main
---
# Syntax plugins
Embedded (enabled by default):
- [Tables](https://help.github.com/articles/organizing-information-with-tables/) (GFM)
- [Strikethrough](https://help.github.com/articles/basic-writing-and-formatting-syntax/#styling-text) (GFM)
Via plugins:
- [subscript](https://github.com/markdown-it/markdown-it-sub)
- [superscript](https://github.com/markdown-it/markdown-it-sup)
- [footnote](https://github.com/markdown-it/markdown-it-footnote)
- [definition list](https://github.com/markdown-it/markdown-it-deflist)
- [abbreviation](https://github.com/markdown-it/markdown-it-abbr)
- [emoji](https://github.com/markdown-it/markdown-it-emoji)
- [custom container](https://github.com/markdown-it/markdown-it-container)
- [insert](https://github.com/markdown-it/markdown-it-ins)
- [mark](https://github.com/markdown-it/markdown-it-mark)
- ... and [others](https://www.npmjs.org/browse/keyword/markdown-it-plugin)
## Manage rules
By default all rules are enabled, but can be restricted by options. On plugin
load all its rules are enabled automatically.
```js
import MarkdownIt from 'markdown-it'
// Activate/deactivate rules, with currying
const md = new MarkdownIt()
.disable(['link', 'image'])
.enable(['link'])
.enable('image');
// Enable everything
const md = new MarkdownIt({
html: true,
linkify: true,
typographer: true,
});
```
You can find all rules in sources:
- [`ParserCore`](https://github.com/markdown-it/markdown-it/blob/master/src/parser_core.ts)
- [`ParserBlock`](https://github.com/markdown-it/markdown-it/blob/master/src/parser_block.ts)
- [`ParserInline`](https://github.com/markdown-it/markdown-it/blob/master/src/parser_inline.ts)

137
docs/usage.md

@ -0,0 +1,137 @@
---
title: Usage examples
category: Main
---
# Usage examples
## Simple
Node.js:
```js
import MarkdownIt from 'markdown-it'
const md = new MarkdownIt()
const result = md.render('# markdown-it rulezz!');
```
Browser with UMD build, added to `window` on script load:
```js
// Note, there is no dash in "markdownit".
const md = new window.markdownit();
const result = md.render('# markdown-it rulezz!');
```
Single line rendering, without paragraph wrap:
```js
import MarkdownIt from 'markdown-it'
const md = new MarkdownIt()
const result = md.renderInline('__markdown-it__ rulezz!');
```
## Init with presets and options
(*) presets define combinations of active rules and options. Can be
`"commonmark"`, `"zero"` or `"default"` (if skipped). See
[API docs](https://markdown-it.github.io/markdown-it/classes/MarkdownIt.html#constructor.constructor) for more details.
CommonMark mode:
```js
import MarkdownIt from 'markdown-it'
const md = new MarkdownIt('commonmark')
```
Default mode:
```js
import MarkdownIt from 'markdown-it'
const md = new MarkdownIt()
```
Enable everything:
```js
import MarkdownIt from 'markdown-it'
const md = new MarkdownIt({
html: true,
linkify: true,
typographer: true
})
```
See [MarkdownItOptions](https://markdown-it.github.io/markdown-it/interfaces/MarkdownItOptions.html)
for the full options list.
## Plugins load
```js
import MarkdownIt from 'markdown-it'
import markdownItAbbr from 'markdown-it-abbr'
import markdownItContainer from 'markdown-it-container'
import markdownItFootnote from 'markdown-it-footnote'
const md = new MarkdownIt()
.use(markdownItAbbr)
.use(markdownItContainer, 'warning')
.use(markdownItFootnote)
```
## Syntax highlighting
Apply syntax highlighting to fenced code blocks with the `highlight` option:
```js
import MarkdownIt from 'markdown-it'
import hljs from 'highlight.js' // https://highlightjs.org
const md = new MarkdownIt({
highlight: function (str, lang) {
if (lang && hljs.getLanguage(lang)) {
try {
return hljs.highlight(str, { language: lang }).value
} catch (__) {}
}
return '' // use external default escaping
}
});
```
Or with full wrapper override (if you need assign class to `<pre>` or `<code>`):
```js
import MarkdownIt from 'markdown-it'
import hljs from 'highlight.js' // https://highlightjs.org
const md = new MarkdownIt({
highlight: function (str, lang) {
if (lang && hljs.getLanguage(lang)) {
try {
return `<pre><code class="hljs">${hljs.highlight(str,
{ language: lang, ignoreIllegals: true }).value}</code></pre>`
} catch (__) {}
}
return `<pre><code class="hljs">${md.utils.escapeHtml(str)}</code></pre>`
}
});
```
## Linkify
`linkify: true` uses [linkify-it](https://github.com/markdown-it/linkify-it). To
configure linkify-it, access the linkify instance through `md.linkify`:
```js
import MarkdownIt from 'markdown-it'
const md = new MarkdownIt({ linkify: true })
md.linkify.set({ fuzzyEmail: false }) // disables converting email to link
```

76
src/markdownit.ts

@ -65,82 +65,6 @@ const RECODE_HOSTNAME_FOR = ['http:', 'https:', 'mailto:']
/**
* Parses Markdown into tokens and renders them to HTML.
*
* @example Basic usage
* ```javascript
* // node.js
* import MarkdownIt from 'markdown-it'
* const md = new MarkdownIt()
* const result = md.render('# markdown-it rulezz!')
*
* // browser without AMD, added to "window" on script load
* // Note, there are no dash.
* const browserMd = new window.markdownit()
* const browserResult = browserMd.render('# markdown-it rulezz!')
* ```
*
* @example Single line rendering, without paragraph wrap
* ```javascript
* import MarkdownIt from 'markdown-it'
* const md = new MarkdownIt()
* const result = md.renderInline('__markdown-it__ rulezz!')
* ```
*
* @example Presets and options
* ```javascript
* import MarkdownIt from 'markdown-it'
*
* // commonmark mode
* const commonmark = new MarkdownIt('commonmark')
*
* // default mode
* const md = new MarkdownIt()
*
* // enable everything
* const full = new MarkdownIt({
* html: true,
* linkify: true,
* typographer: true
* })
* ```
*
* @example Syntax highlighting
* ```js
* import MarkdownIt from 'markdown-it'
* import hljs from 'highlight.js' // https://highlightjs.org/
*
* const md = new MarkdownIt({
* highlight: function (str, lang) {
* if (lang && hljs.getLanguage(lang)) {
* try {
* return hljs.highlight(str, { language: lang, ignoreIllegals: true }).value
* } catch (__) {}
* }
*
* return '' // use external default escaping
* }
* })
* ```
*
* @example Full wrapper override
* If you need assign class to `<pre>` or `<code>`:
* ```javascript
* import MarkdownIt from 'markdown-it'
* import hljs from 'highlight.js' // https://highlightjs.org/
*
* // Actual default values
* const md = new MarkdownIt({
* highlight: function (str, lang) {
* if (lang && hljs.getLanguage(lang)) {
* try {
* return `<pre><code class="hljs">${hljs.highlight(str, { language: lang, ignoreIllegals: true }).value}</code></pre>`
* } catch (__) {}
* }
*
* return `<pre><code class="hljs">${md.utils.escapeHtml(str)}</code></pre>`
* }
* })
* ```
*
* @category Main
*/
class MarkdownIt {

42
src/types.ts

@ -83,6 +83,48 @@ export interface MarkdownItOptions {
* Highlighter function. Should return escaped HTML, or an empty string if
* the source string was not changed and should be escaped externally.
* If the result starts with `<pre`, the internal wrapper is skipped.
*
* The highlighter is called by the default `fence` renderer rule. If needed,
* you can replace that renderer rule completely; see the
* [renderer source](https://github.com/markdown-it/markdown-it/blob/master/src/renderer.ts).
*
* @example
* ```js
* import MarkdownIt from 'markdown-it'
* import hljs from 'highlight.js' // https://highlightjs.org
*
* const md = new MarkdownIt({
* highlight: function (str, lang) {
* if (lang && hljs.getLanguage(lang)) {
* try {
* return hljs.highlight(str, { language: lang }).value
* } catch (__) {}
* }
*
* return '' // use external default escaping
* }
* });
* ```
*
* @example
* Or with full wrapper override (if you need assign class to `<pre>` or `<code>`):
* ```js
* import MarkdownIt from 'markdown-it'
* import hljs from 'highlight.js' // https://highlightjs.org
*
* const md = new MarkdownIt({
* highlight: function (str, lang) {
* if (lang && hljs.getLanguage(lang)) {
* try {
* return `<pre><code class="hljs">${hljs.highlight(str,
* { language: lang, ignoreIllegals: true }).value}</code></pre>`
* } catch (__) {}
* }
*
* return `<pre><code class="hljs">${md.utils.escapeHtml(str)}</code></pre>`
* }
* });
* ```
*/
highlight?: ((str: string, lang: string, attrs: string) => string) | null

49
support/typedoc-custom.css

@ -0,0 +1,49 @@
/*
* The oxide theme ships no styles for TypeDoc's markdown alerts
* (`> [!NOTE]` & co), so they render as plain text. Rules below are taken
* from the default TypeDoc theme, with GitHub's alert colors.
*/
:root {
--color-alert-note: #0969da;
--color-alert-tip: #1a7f37;
--color-alert-important: #8250df;
--color-alert-warning: #9a6700;
--color-alert-caution: #cf222e;
}
:root[data-theme="dark"],
:root[data-theme="ayu"] {
--color-alert-note: #4493f8;
--color-alert-tip: #3fb950;
--color-alert-important: #ab7df8;
--color-alert-warning: #d29922;
--color-alert-caution: #f85149;
}
.tsd-alert {
padding: 8px 16px;
margin-bottom: 16px;
border-left: 0.25em solid var(--alert-color);
}
.tsd-alert > :last-child {
margin-bottom: 0;
}
.tsd-alert-title {
color: var(--alert-color);
display: inline-flex;
align-items: center;
font-weight: 500;
}
.tsd-alert-title span {
margin-left: 4px;
}
.tsd-alert-note { --alert-color: var(--color-alert-note); }
.tsd-alert-tip { --alert-color: var(--color-alert-tip); }
.tsd-alert-important { --alert-color: var(--color-alert-important); }
.tsd-alert-warning { --alert-color: var(--color-alert-warning); }
.tsd-alert-caution { --alert-color: var(--color-alert-caution); }

15
support/typedoc-oxide-fixes.mjs

@ -1,7 +1,7 @@
import * as fs from 'fs/promises'
import * as path from 'path'
import { deflateSync as deflate, inflateSync as inflate } from 'zlib'
import { RendererEvent, ReflectionKind } from 'typedoc'
import { RendererEvent, PageEvent, ReflectionKind } from 'typedoc'
import { itemSlug } from 'typedoc-theme-oxide/dist/plugin/context/utils.js'
// Workarounds for https://github.com/balthild/typedoc-theme-oxide - drop them
@ -23,6 +23,19 @@ export function load (app) {
}
})
// The theme builds its own "Source" and navigation links, ignoring
// `sourceLinkExternal`. Markdown links are already handled by TypeDoc.
const baseUrl = app.options.getValue('hostedBaseUrl')
app.renderer.on(PageEvent.END, (page) => {
page.contents = page.contents.replace(/<a\s[^>]*>/g, tag => {
if (/\btarget=/.test(tag)) return tag
const href = tag.match(/\shref="(https?:\/\/[^"]*)"/)?.[1]
if (!href || (href + '/').startsWith(baseUrl)) return tag
return tag.replace(/^<a\s/, '<a target="_blank" ')
})
})
// The theme emits its own member anchors ("property.core") but leaves the
// router on TypeDoc's scheme ("core"), so every {@link} into a member lands
// nowhere. Teach the router the anchors the theme actually prints.

7
typedoc.config.mjs

@ -2,6 +2,9 @@ export default {
entryPoints: ['src/index.ts'],
projectDocuments: [
'docs/usage.md',
'docs/syntax_plugins.md',
'docs/safety.md',
'docs/architecture.md',
'docs/development.md',
'docs/benchmark.md',
@ -17,11 +20,15 @@ export default {
'./support/typedoc-oxide-fixes.mjs'
],
theme: 'oxide',
customCss: './support/typedoc-custom.css',
excludeExternals: true,
placeInternalsInOwningModule: true,
out: 'apidoc',
includeVersion: true,
// `markdownLinkExternal` compares links against `hostedBaseUrl`,
// and does nothing until that one is set.
hostedBaseUrl: 'https://markdown-it.github.io/markdown-it/',
markdownLinkExternal: true,
sourceLinkExternal: true,
sourceLinkTemplate: 'https://github.com/markdown-it/markdown-it/blob/{gitRevision:short}/{path}#L{line}',

Loading…
Cancel
Save