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
13 changes: 10 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type MarkdownIt from 'markdown-it'
import type { MdcInlineSpanOptions } from './syntax/inline-span'
import { MarkdownItMdcBlock } from './syntax/block'
import { MarkdownItInlineComponent } from './syntax/inline-component'
import { MarkdownItInlineProps } from './syntax/inline-props'
Expand Down Expand Up @@ -26,10 +27,12 @@ export interface MarkdownItMdcOptions {
/**
* Enable inline span syntax.
*
* Pass `true` to enable with defaults, or pass an object to configure.
*
* @see https://content.nuxtjs.org/guide/writing/mdc#inline-span
* @default true
*/
inlineSpan?: boolean
inlineSpan?: boolean | MdcInlineSpanOptions
/**
* Enable inline component syntax.
*
Expand All @@ -54,8 +57,12 @@ const MarkdownItMdc: MarkdownIt.PluginWithOptions<MarkdownItMdcOptions> = (md, o
if (inlineProps)
md.use(MarkdownItInlineProps)

if (inlineSpan)
md.use(MarkdownItInlineSpan)
if (inlineSpan) {
const spanOptions = typeof inlineSpan === 'object'
? inlineSpan
: {}
md.use(MarkdownItInlineSpan, spanOptions)
}

if (inlineComponent)
md.use(MarkdownItInlineComponent)
Expand Down
14 changes: 12 additions & 2 deletions src/syntax/inline-span.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
import type MarkdownIt from 'markdown-it'

export interface MdcInlineSpanOptions {

/**
* Skip footnote-like syntax `[^...]` to avoid conflicts with footnote plugins.
*
* @default true
*/
skipFootnoteLike?: boolean
}

export const MarkdownItInlineSpan: MarkdownIt.PluginWithOptions<MdcInlineSpanOptions> = (md) => {
export const MarkdownItInlineSpan: MarkdownIt.PluginWithOptions<MdcInlineSpanOptions> = (md, options = {}) => {
const { skipFootnoteLike = true } = options

md.inline.ruler.before('link', 'mdc_inline_span', (state, silent) => {
const start = state.pos
const char = state.src[start]

if (char !== '[')
return false

if (skipFootnoteLike && state.src[start + 1] === '^')
return false

let index = start + 1
let depth = 0
while (index < state.src.length) {
Expand Down
2 changes: 2 additions & 0 deletions test/input/8.inline-span.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ Hello [World]{.bg-blue-500}!
Hello [World\] Yes]!

Hello [World]{style=""}!

Test[^note]
1 change: 1 addition & 0 deletions test/output/8.inline-span.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
<p>Hello <span class="bg-blue-500">World</span>!</p>
<p>Hello <span>World] Yes</span>!</p>
<p>Hello <span style="">World</span>!</p>
<p>Test[^note]</p>
Loading