Skip to content

Commit

Permalink
feat: sugar syntax for blocks (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
farnabaz committed Oct 26, 2022
1 parent a6f9f69 commit 3d41472
Show file tree
Hide file tree
Showing 10 changed files with 376 additions and 86 deletions.
3 changes: 2 additions & 1 deletion src/micromark-extension/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import tokenizeInline from './tokenize-inline'
import tokenizeContainer from './tokenize-container'
import tokenizeContainerIndented from './tokenize-container-indented'
import { Codes } from './constants'
import tokenizeContainerSuger from './tokenize-container-suger'

export default function micromarkComponentsExtension () {
return {
Expand All @@ -20,7 +21,7 @@ export default function micromarkComponentsExtension () {
[Codes.openingCurlyBracket]: [tokenizeBinding, tokenizeAttribute]
},
flow: {
[Codes.colon]: [tokenizeContainer]
[Codes.colon]: [tokenizeContainer, tokenizeContainerSuger]
},
flowInitial: {
'-2': tokenizeContainerIndented,
Expand Down
45 changes: 45 additions & 0 deletions src/micromark-extension/tokenize-container-suger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import type { Effects, State, TokenizeContext, Code } from 'micromark-util-types'
import { markdownLineEnding } from 'micromark-util-character'
import { factorySpace } from 'micromark-factory-space'
import componentContainer from './tokenize-inline'
import { Codes } from './constants'

/**
text
:component
text
*/
function tokenize (this: TokenizeContext, effects: Effects, ok: State, nok: State) {
const self = this

const tokenizeSugerSyntax = componentContainer.tokenize.call(
self,
effects,
factorySpace(effects, exit as State, 'linePrefix'),
nok
)

return factorySpace(effects, lineStart as State, 'linePrefix')

function lineStart (code: Code): void | State {
if (code === Codes.colon) {
return tokenizeSugerSyntax(code)
}
return nok(code)
}

function exit (code: Code): void | State {
if (markdownLineEnding(code) || code === Codes.EOF) {
return ok(code)
}
return nok(code)
}
}

export default {
tokenize
}
158 changes: 127 additions & 31 deletions test/__snapshots__/attributes.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,28 @@ exports[`Attributes > boolean 1`] = `
},
"type": "textComponent",
},
{
"position": {
"end": {
"column": 17,
"line": 1,
"offset": 16,
},
"start": {
"column": 12,
"line": 1,
"offset": 11,
},
},
"type": "text",
"value": " text",
},
],
"position": {
"end": {
"column": 12,
"column": 17,
"line": 1,
"offset": 11,
"offset": 16,
},
"start": {
"column": 1,
Expand All @@ -50,9 +66,9 @@ exports[`Attributes > boolean 1`] = `
],
"position": {
"end": {
"column": 12,
"column": 17,
"line": 1,
"offset": 11,
"offset": 16,
},
"start": {
"column": 1,
Expand Down Expand Up @@ -98,12 +114,28 @@ exports[`Attributes > class-suger 1`] = `
},
"type": "textComponent",
},
{
"position": {
"end": {
"column": 46,
"line": 1,
"offset": 45,
},
"start": {
"column": 41,
"line": 1,
"offset": 40,
},
},
"type": "text",
"value": " text",
},
],
"position": {
"end": {
"column": 41,
"column": 46,
"line": 1,
"offset": 40,
"offset": 45,
},
"start": {
"column": 1,
Expand All @@ -116,9 +148,9 @@ exports[`Attributes > class-suger 1`] = `
],
"position": {
"end": {
"column": 41,
"column": 46,
"line": 1,
"offset": 40,
"offset": 45,
},
"start": {
"column": 1,
Expand Down Expand Up @@ -373,12 +405,28 @@ exports[`Attributes > html-characters 1`] = `
},
"type": "textComponent",
},
{
"position": {
"end": {
"column": 26,
"line": 1,
"offset": 25,
},
"start": {
"column": 21,
"line": 1,
"offset": 20,
},
},
"type": "text",
"value": " text",
},
],
"position": {
"end": {
"column": 21,
"column": 26,
"line": 1,
"offset": 20,
"offset": 25,
},
"start": {
"column": 1,
Expand All @@ -391,9 +439,9 @@ exports[`Attributes > html-characters 1`] = `
],
"position": {
"end": {
"column": 21,
"column": 26,
"line": 1,
"offset": 20,
"offset": 25,
},
"start": {
"column": 1,
Expand Down Expand Up @@ -437,12 +485,28 @@ exports[`Attributes > html-characters-unquoted 1`] = `
},
"type": "textComponent",
},
{
"position": {
"end": {
"column": 24,
"line": 1,
"offset": 23,
},
"start": {
"column": 19,
"line": 1,
"offset": 18,
},
},
"type": "text",
"value": " text",
},
],
"position": {
"end": {
"column": 19,
"column": 24,
"line": 1,
"offset": 18,
"offset": 23,
},
"start": {
"column": 1,
Expand All @@ -455,9 +519,9 @@ exports[`Attributes > html-characters-unquoted 1`] = `
],
"position": {
"end": {
"column": 19,
"column": 24,
"line": 1,
"offset": 18,
"offset": 23,
},
"start": {
"column": 1,
Expand Down Expand Up @@ -501,12 +565,28 @@ exports[`Attributes > id-suger 1`] = `
},
"type": "textComponent",
},
{
"position": {
"end": {
"column": 16,
"line": 1,
"offset": 15,
},
"start": {
"column": 11,
"line": 1,
"offset": 10,
},
},
"type": "text",
"value": " text",
},
],
"position": {
"end": {
"column": 11,
"column": 16,
"line": 1,
"offset": 10,
"offset": 15,
},
"start": {
"column": 1,
Expand All @@ -519,9 +599,9 @@ exports[`Attributes > id-suger 1`] = `
],
"position": {
"end": {
"column": 11,
"column": 16,
"line": 1,
"offset": 10,
"offset": 15,
},
"start": {
"column": 1,
Expand Down Expand Up @@ -623,9 +703,9 @@ exports[`Attributes > invlid-binding 1`] = `
{
"position": {
"end": {
"column": 9,
"column": 14,
"line": 1,
"offset": 8,
"offset": 13,
},
"start": {
"column": 6,
Expand All @@ -634,14 +714,14 @@ exports[`Attributes > invlid-binding 1`] = `
},
},
"type": "text",
"value": "{:}",
"value": "{:} text",
},
],
"position": {
"end": {
"column": 9,
"column": 14,
"line": 1,
"offset": 8,
"offset": 13,
},
"start": {
"column": 1,
Expand All @@ -654,9 +734,9 @@ exports[`Attributes > invlid-binding 1`] = `
],
"position": {
"end": {
"column": 9,
"column": 14,
"line": 1,
"offset": 8,
"offset": 13,
},
"start": {
"column": 1,
Expand Down Expand Up @@ -850,12 +930,28 @@ exports[`Attributes > value-with-space 1`] = `
},
"type": "textComponent",
},
{
"position": {
"end": {
"column": 24,
"line": 1,
"offset": 23,
},
"start": {
"column": 19,
"line": 1,
"offset": 18,
},
},
"type": "text",
"value": " text",
},
],
"position": {
"end": {
"column": 19,
"column": 24,
"line": 1,
"offset": 18,
"offset": 23,
},
"start": {
"column": 1,
Expand All @@ -868,9 +964,9 @@ exports[`Attributes > value-with-space 1`] = `
],
"position": {
"end": {
"column": 19,
"column": 24,
"line": 1,
"offset": 18,
"offset": 23,
},
"start": {
"column": 1,
Expand Down
Loading

0 comments on commit 3d41472

Please sign in to comment.