Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: inline text attributes with []{} syntax #471

Merged
merged 4 commits into from
Jun 21, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
16 changes: 16 additions & 0 deletions docs/content/2.writing/2.syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -308,3 +308,19 @@ Inline components are used to put components in the middle of any Markdown block
:button-link[A button link]{bold}
::
::


## Text Attributes

Text attributes are useful for hightightin and modifying part of paragraph. The syntax ins nearly similar to inline components and markdown links syntax.

::code-group

```md [Code]
Hello [World]{.text-primary-500}!
```

::code-block{label="Preview" preview}
Hello [World]{.text-primary-500}!
::
::
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ export const Codes = {
* '{'
*/
openningCurlyBracket: 123,
/**
* '('
*/
openningParentheses: 40,
/**
* ')'
*/
closingParentheses: 41,
/**
* '_'
*/
Expand Down
13 changes: 10 additions & 3 deletions src/core/parser/markdown/directive/micromark-directive/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
// https://github.com/micromark/micromark-extension-directive/blob/main/lib/syntax.js

import directiveSpan from './tokenize-directive-span'
import directiveInline from './tokenize-directive-inline'
import directiveContainer from './tokenize-directive-container'
import directiveContainerIndented from './tokenize-directive-container-indented'
import { Codes } from './constants'

export default function directive() {
return {
text: { 58: directiveInline },
flow: { 58: [directiveContainer, directiveInline] },
text: {
[Codes.colon]: directiveInline,
[Codes.openningSquareBracket]: [directiveSpan]
},
flow: {
[Codes.colon]: [directiveContainer, directiveInline]
},
flowInitial: {
'-2': directiveContainerIndented,
'-1': directiveContainerIndented,
32: directiveContainerIndented
[Codes.space]: directiveContainerIndented
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { Effects, Okay, NotOkay } from 'micromark/dist/shared-types'
import { Codes } from './constants'
import createAttributes from './factory-attributes'
import createLabel from './factory-label'

const label: any = { tokenize: tokenizeLabel, partial: true }
const attributes: any = { tokenize: tokenizeAttributes, partial: true }

function tokenize(effects: Effects, ok: Okay, nok: NotOkay) {
return start

function start(code: number) {
if (code !== Codes.openningSquareBracket) {
throw new Error('expected `[`')
}

effects.enter('directiveTextSpan')
return effects.attempt(label, afterLabel, nok)(code)
}

function afterLabel(code: number) {
if (code === Codes.closingParentheses) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shoudn't be closingSquareBracket? 🤔

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Damn, This should be removed, it remains from parentheses syntax

effects.consume(code)
return afterLabel
}

// Check for attributes after label
if (code === Codes.openningCurlyBracket) {
return effects.attempt(attributes, exit, nok)(code)
}
return nok(code)
}

function exit(code: number) {
effects.exit('directiveTextSpan')
return ok(code)
}
}

/**
* Labels starts with `[` and ends with `]`
*/
function tokenizeLabel(effects: Effects, ok: Okay, nok: NotOkay) {
return createLabel(effects, ok, nok, 'directiveTextLabel', 'directiveTextLabelMarker', 'directiveTextLabelString')
}

/**
* Attributes starts with `{` and ends with `}`
*/
function tokenizeAttributes(effects: Effects, ok: Okay, nok: NotOkay) {
return createAttributes(
effects,
ok,
nok,
'directiveTextAttributes',
'directiveTextAttributesMarker',
'directiveTextAttribute',
'directiveTextAttributeId',
'directiveTextAttributeClass',
'directiveTextAttributeName',
'directiveTextAttributeInitializerMarker',
'directiveTextAttributeValueLiteral',
'directiveTextAttributeValue',
'directiveTextAttributeValueMarker',
'directiveTextAttributeValueData'
)
}

export default {
tokenize
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const enter = {
directiveLeafAttributes: enterAttributes,

directiveText: enterText,
directiveTextSpan: enterTextSpan,
directiveTextAttributes: enterAttributes
}
const exit = {
Expand Down Expand Up @@ -44,6 +45,7 @@ const exit = {
directiveLeafName: exitName,

directiveText: exitToken,
directiveTextSpan: exitToken,
directiveTextAttributeClassValue: exitAttributeClassValue,
directiveTextAttributeIdValue: exitAttributeIdValue,
directiveTextAttributeName: exitAttributeName,
Expand Down Expand Up @@ -109,6 +111,10 @@ function enterLeaf(token: Token) {
enterToken.call(this, 'leafDirective', token)
}

function enterTextSpan(token: Token) {
this.enter({ type: 'textDirective', name: 'span', attributes: {}, children: [] }, token)
}

function enterText(token: Token) {
enterToken.call(this, 'textDirective', token)
}
Expand Down