Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Main features:
- [Flowchart](https://github.com/adrai/flowchart.js)
- [dot](https://github.com/mdaines/viz.js)
- [Table of contents](https://github.com/nagaozen/markdown-it-toc-done-right)
- [GitHub Flavored Markdown Alerts](https://github.com/antfu/markdown-it-github-alerts)
- Emojis
- Task lists
- Local images
Expand Down Expand Up @@ -474,6 +475,7 @@ Vim support is powered by [@chemzqm/neovim](https://github.com/neoclide/neovim)
- [markdown-it-katex](https://github.com/waylonflinn/markdown-it-katex)
- [markdown-it-plantuml](https://github.com/gmunguia/markdown-it-plantuml)
- [markdown-it-chart](https://github.com/tylingsoft/markdown-it-chart)
- [markdown-it-github-alerts](https://github.com/antfu/markdown-it-github-alerts)
- [mermaid](https://github.com/knsv/mermaid)
- [opener](https://github.com/domenic/opener)
- [sequence-diagrams](https://github.com/bramp/js-sequence-diagrams)
Expand Down
65 changes: 65 additions & 0 deletions app/_static/markdown.css
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
--color-markdown-table-border: #dfe2e5;
--color-markdown-table-tr-border: #c6cbd1;
--color-markdown-code-bg: #1b1f230d;
--color-note: #0969da;
--color-tip: #1a7f37;
--color-warning: #9a6700;
--color-caution: #d1242f;
--color-important: #8250df;
}
[data-theme="dark"] {
--color-text-primary: #c9d1d9;
Expand All @@ -34,6 +39,11 @@
--color-markdown-table-border: #3b434b;
--color-markdown-table-tr-border: #272c32;
--color-markdown-code-bg: #f0f6fc26;
--color-note: #2f81f7;
--color-tip: #3fb950;
--color-warning: #d29922;
--color-caution: #f85149;
--color-important: #a371f7;
}

.markdown-body ol ol,
Expand Down Expand Up @@ -189,6 +199,61 @@
color: var(--color-text-tertiary);
border-left: 4px solid var(--color-markdown-blockquote-border);
}
.markdown-body .markdown-alert {
padding: 0.5rem 1rem;
margin-bottom: 16px;
color: inherit;
border-left: 0.25em solid #888;
}
.markdown-body .markdown-alert > :first-child {
margin-top: 0;
}
.markdown-body .markdown-alert > :last-child {
margin-bottom: 0;
}
.markdown-body .markdown-alert .markdown-alert-title {
display: flex;
font-weight: 500;
align-items: center;
line-height: 1;
}
.markdown-body .markdown-alert .markdown-alert-title .octicon {
margin-right: 0.5rem;
display: inline-block;
overflow: visible !important;
vertical-align: text-bottom;
fill: currentColor;
}
.markdown-body .markdown-alert.markdown-alert-note {
border-left-color: var(--color-note);
}
.markdown-body .markdown-alert.markdown-alert-note .markdown-alert-title {
color: var(--color-note);
}
.markdown-body .markdown-alert.markdown-alert-important {
border-left-color: var(--color-important);
}
.markdown-body .markdown-alert.markdown-alert-important .markdown-alert-title {
color: var(--color-important);
}
.markdown-body .markdown-alert.markdown-alert-warning {
border-left-color: var(--color-warning);
}
.markdown-body .markdown-alert.markdown-alert-warning .markdown-alert-title {
color: var(--color-warning);
}
.markdown-body .markdown-alert.markdown-alert-tip {
border-left-color: var(--color-tip);
}
.markdown-body .markdown-alert.markdown-alert-tip .markdown-alert-title {
color: var(--color-tip);
}
.markdown-body .markdown-alert.markdown-alert-caution {
border-left-color: var(--color-caution);
}
.markdown-body .markdown-alert.markdown-alert-caution .markdown-alert-title {
color: var(--color-caution);
}
.markdown-body table {
display: block;
width: 100%;
Expand Down
2 changes: 2 additions & 0 deletions app/pages/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import footnote from 'markdown-it-footnote'
import markdownItAnchor from 'markdown-it-anchor'
import markdownItToc from 'markdown-it-toc-done-right'
import markdownDeflist from 'markdown-it-deflist'
import markdownItGitHubAlerts from './markdown-it-github-alerts-compat'

import mk from './katex'
import chart from './chart'
Expand Down Expand Up @@ -215,6 +216,7 @@ export default class PreviewPage extends React.Component {
.use(taskLists)
.use(markdownDeflist)
.use(footnote)
.use(markdownItGitHubAlerts)
.use(image)
.use(markdownImSize)
.use(linenumbers)
Expand Down
79 changes: 79 additions & 0 deletions app/pages/markdown-it-github-alerts-compat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Compatibility wrapper for legacy webpack parser (Next 7 / webpack 4).
// Adapted from markdown-it-github-alerts (MIT):
// https://github.com/antfu/markdown-it-github-alerts
const DEFAULT_GITHUB_ICONS = {
note: '<svg class="octicon octicon-info mr-2" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"><path d="M0 8a8 8 0 1 1 16 0A8 8 0 0 1 0 8Zm8-6.5a6.5 6.5 0 1 0 0 13 6.5 6.5 0 0 0 0-13ZM6.5 7.75A.75.75 0 0 1 7.25 7h1a.75.75 0 0 1 .75.75v2.75h.25a.75.75 0 0 1 0 1.5h-2a.75.75 0 0 1 0-1.5h.25v-2h-.25a.75.75 0 0 1-.75-.75ZM8 6a1 1 0 1 1 0-2 1 1 0 0 1 0 2Z"></path></svg>',
tip: '<svg class="octicon octicon-light-bulb mr-2" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"><path d="M8 1.5c-2.363 0-4 1.69-4 3.75 0 .984.424 1.625.984 2.304l.214.253c.223.264.47.556.673.848.284.411.537.896.621 1.49a.75.75 0 0 1-1.484.211c-.04-.282-.163-.547-.37-.847a8.456 8.456 0 0 0-.542-.68c-.084-.1-.173-.205-.268-.32C3.201 7.75 2.5 6.766 2.5 5.25 2.5 2.31 4.863 0 8 0s5.5 2.31 5.5 5.25c0 1.516-.701 2.5-1.328 3.259-.095.115-.184.22-.268.319-.207.245-.383.453-.541.681-.208.3-.33.565-.37.847a.751.751 0 0 1-1.485-.212c.084-.593.337-1.078.621-1.489.203-.292.45-.584.673-.848.075-.088.147-.173.213-.253.561-.679.985-1.32.985-2.304 0-2.06-1.637-3.75-4-3.75ZM5.75 12h4.5a.75.75 0 0 1 0 1.5h-4.5a.75.75 0 0 1 0-1.5ZM6 15.25a.75.75 0 0 1 .75-.75h2.5a.75.75 0 0 1 0 1.5h-2.5a.75.75 0 0 1-.75-.75Z"></path></svg>',
important: '<svg class="octicon octicon-report mr-2" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"><path d="M0 1.75C0 .784.784 0 1.75 0h12.5C15.216 0 16 .784 16 1.75v9.5A1.75 1.75 0 0 1 14.25 13H8.06l-2.573 2.573A1.458 1.458 0 0 1 3 14.543V13H1.75A1.75 1.75 0 0 1 0 11.25Zm1.75-.25a.25.25 0 0 0-.25.25v9.5c0 .138.112.25.25.25h2a.75.75 0 0 1 .75.75v2.19l2.72-2.72a.749.749 0 0 1 .53-.22h6.5a.25.25 0 0 0 .25-.25v-9.5a.25.25 0 0 0-.25-.25Zm7 2.25v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 9a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path></svg>',
warning: '<svg class="octicon octicon-alert mr-2" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"><path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path></svg>',
caution: '<svg class="octicon octicon-stop mr-2" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"><path d="M4.47.22A.749.749 0 0 1 5 0h6c.199 0 .389.079.53.22l4.25 4.25c.141.14.22.331.22.53v6a.749.749 0 0 1-.22.53l-4.25 4.25A.749.749 0 0 1 11 16H5a.749.749 0 0 1-.53-.22L.22 11.53A.749.749 0 0 1 0 11V5c0-.199.079-.389.22-.53Zm.84 1.28L1.5 5.31v5.38l3.81 3.81h5.38l3.81-3.81V5.31L10.69 1.5ZM8 4a.75.75 0 0 1 .75.75v3.5a.75.75 0 0 1-1.5 0v-3.5A.75.75 0 0 1 8 4Zm0 8a1 1 0 1 1 0-2 1 1 0 0 1 0 2Z"></path></svg>'
}

function capitalize(str) {
return str.charAt(0).toUpperCase() + str.slice(1)
}

export default function markdownItGitHubAlerts(md, options = {}) {
const markers = options.markers || ['TIP', 'NOTE', 'IMPORTANT', 'WARNING', 'CAUTION']
const icons = options.icons || DEFAULT_GITHUB_ICONS
const matchCaseSensitive = options.matchCaseSensitive || false
const titles = options.titles || {}
const classPrefix = options.classPrefix || 'markdown-alert'

const markerNameRE = markers === '*' ? '\\w+' : markers.join('|')
const RE = new RegExp(
`^\\\\?\\[\\!(${markerNameRE})\\]([^\\n\\r]*)`,
matchCaseSensitive ? '' : 'i'
)

md.core.ruler.after('block', 'github-alerts', (state) => {
const tokens = state.tokens
for (let i = 0; i < tokens.length; i++) {
if (tokens[i].type !== 'blockquote_open') {
continue
}

const open = tokens[i]
const startIndex = i
while (i <= tokens.length && (!tokens[i] || tokens[i].type !== 'blockquote_close')) {
i += 1
}

const close = tokens[i]
const endIndex = i
const firstContent = tokens
.slice(startIndex, endIndex + 1)
.find((token) => token.type === 'inline')
if (!firstContent) {
continue
}

const match = firstContent.content.match(RE)
if (!match) {
continue
}

const type = match[1].toLowerCase()
const title = match[2].trim() || (
Object.prototype.hasOwnProperty.call(titles, type)
? titles[type]
: capitalize(type)
)
const icon = Object.prototype.hasOwnProperty.call(icons, type) ? icons[type] : ''

firstContent.content = firstContent.content.slice(match[0].length).trimStart()

open.type = 'alert_open'
open.tag = 'div'
open.meta = { title, type, icon }

close.type = 'alert_close'
close.tag = 'div'
}
})

md.renderer.rules.alert_open = function(tokens, idx) {
const { title, type, icon } = tokens[idx].meta
return `<div class="${classPrefix} ${classPrefix}-${type}"><p class="${classPrefix}-title">${icon}${title}</p>`
}
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"markdown-it-deflist": "^2.0.3",
"markdown-it-emoji": "^1.4.0",
"markdown-it-footnote": "^3.0.1",
"markdown-it-github-alerts": "0.1.2",
"markdown-it-task-lists": "^2.1.1",
"markdown-it-toc-done-right": "^4.0.1",
"md-it-meta": "^0.0.2",
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3589,6 +3589,11 @@ markdown-it-footnote@^3.0.1:
resolved "https://registry.yarnpkg.com/markdown-it-footnote/-/markdown-it-footnote-3.0.3.tgz#e0e4c0d67390a4c5f0c75f73be605c7c190ca4d8"
integrity sha512-YZMSuCGVZAjzKMn+xqIco9d1cLGxbELHZ9do/TSYVzraooV8ypsppKNmUJ0fVH5ljkCInQAtFpm8Rb3eXSrt5w==

markdown-it-github-alerts@0.1.2:
version "0.1.2"
resolved "https://registry.yarnpkg.com/markdown-it-github-alerts/-/markdown-it-github-alerts-0.1.2.tgz#b2f858756535b78bdce9128ffe0114b1875d469f"
integrity sha512-jtCrfFxXR6c/bNRBvWkoqUquEDdIF9q7/gNZjP47W96kaIiBMiYHbpf468IPeZJB5BVRls6+IXGchhQSTxy0DQ==

markdown-it-task-lists@^2.1.1:
version "2.1.1"
resolved "https://registry.yarnpkg.com/markdown-it-task-lists/-/markdown-it-task-lists-2.1.1.tgz#f68f4d2ac2bad5a2c373ba93081a1a6848417088"
Expand Down