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

feature/builder docs #78

Merged
merged 50 commits into from
May 24, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
bb0325f
chore: updated to latest dependencies
yankeeinlondon Mar 7, 2022
2932b47
chore: added files to help contributors better stick to existing stan…
yankeeinlondon Mar 7, 2022
d5318f4
feature: added GreyMatterOptions interface
yankeeinlondon Mar 7, 2022
9362cec
chore: add simple test to show usage
yankeeinlondon Mar 7, 2022
38ce61f
reverted prettier and linter configuration
yankeeinlondon Mar 8, 2022
9c8fd33
chore: formating
antfu Mar 9, 2022
cd3fe39
chore: initial implementation and tests
yankeeinlondon Mar 14, 2022
04b4da2
Feature/baseline builders (#1)
yankeeinlondon Mar 20, 2022
8ccb3a5
Merge remote-tracking branch 'upstream/main' into develop
yankeeinlondon Mar 20, 2022
ecc13b5
fix: fixed faulty merge on options resolver
yankeeinlondon Mar 20, 2022
ba7b3f1
fix: fixed the regression in README; makes it more like current version
yankeeinlondon Mar 21, 2022
09da7c3
chore: updated docs for MetaBuilder
yankeeinlondon Mar 21, 2022
a7f6057
fix: remove reference to `replaceAll` to remove testing errors
yankeeinlondon Mar 21, 2022
495f6ed
chore: improved BuilderApi docs
yankeeinlondon Mar 21, 2022
23b5b1b
chore: `@types` folder to `types`
yankeeinlondon Mar 21, 2022
326e6e6
Merge remote-tracking branch 'upstream/main' into develop
yankeeinlondon Mar 21, 2022
d702862
Merge remote-tracking branch 'upstream/main' into develop
yankeeinlondon Mar 27, 2022
bf36ee2
fix: ensures that wrapper classes are set regardless of whether wrapp…
yankeeinlondon Mar 27, 2022
52e6688
test: added tests for HEAD functionality and re-introduce code block …
yankeeinlondon Mar 27, 2022
93756c5
test: added tests to test behavior of exposeFrontmatter prop on Vue2 …
yankeeinlondon Mar 28, 2022
8cb16ed
fix: fixed lint regression
yankeeinlondon Mar 28, 2022
01fe38c
refactor: changed the pipeline to support async tasks
yankeeinlondon Apr 6, 2022
d130534
chore: conversion of whole pipeline to TaskEither functions
yankeeinlondon Apr 8, 2022
55701e8
chore: pipeline fully converted to async
yankeeinlondon Apr 8, 2022
514692c
chore: switch all tests over to use of async pipeline
yankeeinlondon Apr 9, 2022
92fef87
chore: added happy-dom utility's and started line number highlighting
yankeeinlondon Apr 12, 2022
9816b17
chore: all advanced tests for wrapWithText() are passing
yankeeinlondon Apr 13, 2022
fd24b67
chore: better abstractions for happy-dom utilities
yankeeinlondon Apr 16, 2022
017ff8d
Merge remote-tracking branch 'upstream/main' into feature/code-builder
yankeeinlondon Apr 16, 2022
3a2c5ee
chore: adjusted doc structure and simplified props parsing
yankeeinlondon Apr 18, 2022
88a632c
chore: refinements to happy-wrapper
yankeeinlondon Apr 20, 2022
060e529
chore: mild DOM structure improvements in code layout
yankeeinlondon Apr 22, 2022
e443922
fix: got frontmatter exports working again!
yankeeinlondon Apr 22, 2022
d3e9459
Merge remote-tracking branch 'up2/main' into feature/feature/fix-exports
yankeeinlondon Apr 22, 2022
377ba2e
chore: formatting updates
yankeeinlondon Apr 22, 2022
e7fae2a
Merge branch 'feature/feature/fix-exports' into develop
yankeeinlondon Apr 23, 2022
3e0b96d
Merge branch 'develop' into feature/code-builder
yankeeinlondon Apr 23, 2022
3daf785
test: updated snapshots
yankeeinlondon Apr 23, 2022
d681382
chore: refactor of example app
yankeeinlondon Apr 23, 2022
4dabe59
chore: improved some core tests around script hoisting, head function…
yankeeinlondon May 5, 2022
828d4a6
chore: tabular formatting 95% working
yankeeinlondon May 13, 2022
496c298
chore: tuned the prism color themes a bit
yankeeinlondon May 19, 2022
c52a88b
- fix: frontmatter exports (#67)
yankeeinlondon May 20, 2022
a4571b3
chore: lint check/fiix, update happy-dom
yankeeinlondon May 24, 2022
a6af9de
Merge remote-tracking branch 'up2/main'
yankeeinlondon May 24, 2022
10f6d3d
chore: added build dependency to build happy-wrapper
yankeeinlondon May 24, 2022
d24c0aa
chore: removed "happy-wrapper" to it's own repo
yankeeinlondon May 24, 2022
d426e5c
chore: remove extraneous global link file
yankeeinlondon May 24, 2022
3ea4813
docs: updated builder docs to be (hopefully) more clear about pipelin…
yankeeinlondon May 24, 2022
d522dd9
Merge branch 'main' into feature/builder-docs
yankeeinlondon May 24, 2022
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
Prev Previous commit
Next Next commit
chore: added happy-dom utility's and started line number highlighting
  • Loading branch information
yankeeinlondon committed Apr 12, 2022
commit 92fef87b3761812e0ad25c959cf696a62f98e531
6 changes: 3 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions src/builders/code/code.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { PipelineStage } from '../../types'
import { createBuilder } from '../createBuilder'
import { fence } from './mdi'
import type { CodeOptions } from './types/code-types'
import type { CodeOptions, PrismOptions } from './types/code-types'
import { Highlighter } from './types/code-types'

const PRISM_DEFAULTS = {
const PRISM_DEFAULTS: Partial<PrismOptions> = {
engine: Highlighter.prism,
plugins: [],
init: () => {
// do nothing by default
},
defaultLanguageForUnknown: undefined,
defaultLanguageForUnknown: 'bash',
defaultLanguageForUnspecified: 'bash',
defaultLanguage: undefined,
}
Expand Down
4 changes: 2 additions & 2 deletions src/builders/code/mdi/establishHighlighter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import type {
CodeOptions,
HighlighterFunction,
LineClassFn,
PrismLanguage,
PrismOptions,
ShikiOptions,
} from '../types'
import { usesPrismHighlighting } from '../utils'
import type { PrismLanguage } from '../utils'
import { usesPrismHighlighting } from '../utils/highlighting'
import { getPrismHighlighter } from './prism'
import { getShikiHighlighter } from './shiki'

Expand Down
166 changes: 17 additions & 149 deletions src/builders/code/mdi/fence.ts
Original file line number Diff line number Diff line change
@@ -1,155 +1,21 @@
import { pipe } from 'fp-ts/lib/function'
import type MarkdownIt from 'markdown-it'
import type Token from 'markdown-it/lib/token'
import type { Pipeline, PipelineStage } from '../../../types'
import {
convertLinesToCodeBlock,
extractInfo,
highlightLines,
parseLines,
renderFence,
resolveLanguage,
showLineNumbers,
useHighlighter, userRules,
} from '../pipeline'
import type {
CodeFenceMeta,
CodeOptions,
HighlighterFunction,
LineClassFn,
Modifier,
} from '../types'
import {
isLineCallback,
isValidLanguage,
usesPrismHighlighting,
} from '../utils'
import { establishHighlighter } from './establishHighlighter'

function typedValue(value: string) {
return value.startsWith('"')
? value.endsWith('"') ? value.slice(1, -1) : value.slice(1)
: value === 'true'
? true
: value === 'false'
? false
: !Number.isNaN(value) ? Number(value) : value
}

/**
* Converts the Markdown-IT `Token` into a `CodeFenceMeta` data structure
*/
function extractInfo(t: Token): CodeFenceMeta {
const matches = t.info.trim().match(/((!|#|\*){0,2})(\w+)\s+({.*}){0,1}(.*)$/)
let props: Record<string, any> = {}
let modifiers: Modifier[] = []
let lang = t.info

if (matches) {
const [, m, , l, obj, csv] = matches
if (obj) {
try {
props = JSON.parse(obj)
}
catch {
props = {}
console.warn(`- a fenced code block appeared to have a configuration object but failed to be parsed. When using an object notation always ensure it is valid JSON or alternatively you can use the CSV name/value style of assignment: ${obj}`)
}
}
else if (csv) {
props = csv.split(',').reduce((acc, i) => {
const [key, ...rest] = i.trim().split(/[:=]/)
return { ...acc, [key.trim()]: typedValue(rest.join().trim()) }
}, {})
}

if (m)
modifiers = Array.from(m) as Modifier[]

if (l)
lang = l
}

return {
data: t.content,
tag: t.tag,
lang,
props,
modifiers,
markup: t.markup,
}
}

/**
* Looks for invalid and non-existant language settings and uses fallbacks
* where available
*/
const resolveLanguage = (o: CodeOptions) => (fence: CodeFenceMeta) => {
if (fence.lang) {
if (isValidLanguage(o.engine, fence)) {
fence.lang = usesPrismHighlighting(o)
? o.defaultLanguageForUnknown || o.defaultLanguage || ''
: fence.lang
}
}

if (!fence.lang) {
fence.lang = usesPrismHighlighting(o)
? o.defaultLanguageForUnspecified || o.defaultLanguage
: fence.lang
}

return fence
}

/**
* If configured to highlight lines (default is `true`) and line
* annotation is found in code block then it will be
*/
const highlightLines = (_o: CodeOptions) => (fence: CodeFenceMeta) => {
return fence
}

/**
* If configured for line numbers, the appropriate HTML will be added
* to bring this in.
*/
const showLineNumbers = (o: CodeOptions) => (fence: CodeFenceMeta) => {
return o.lineNumbers
? {
...fence,
props: {
...fence.props,
class: [...(fence.props?.class || '').split(/\s+/), 'line-numbers-mode'].join(' '),
},
}
: fence
}

const userRules = (when: 'before' | 'after', p: Pipeline<PipelineStage.parser>, o: CodeOptions) => (fence: CodeFenceMeta): CodeFenceMeta => {
return o[when] ? o[when](fence, p, o) : fence
}

const klass = (o: CodeOptions, fence: CodeFenceMeta): LineClassFn => {
const { lineClass } = o
const { data, lang } = fence
return lineClass
? isLineCallback(lineClass)
? (line: string) => ['line', lineClass(line, data, lang)].join(' ')
: typeof lineClass === 'string'
? (_: string) => lineClass
: (_: string) => ''
: (_: string) => 'line'
}

/**
* Use Prism or Shiki to modify the code block into stylized HTML
*/
const useHighlighter = (h: HighlighterFunction, o: CodeOptions) => (fence: CodeFenceMeta): CodeFenceMeta => {
return {
...fence,
data: h(fence.data, fence.lang, klass(o, fence)),
}
}

/**
* Renders the HTML which results from the code block transform pipeline
*/
const renderFence = (fence: CodeFenceMeta): string => {
return `<pre class='${[`language-${fence.lang}`, fence.props.class?.trim()].filter(i => i).join(' ')}'${fence.props.style ? ` style='${fence.props.style}'` : ''}><code>
${fence.data.trim()}
</code></pre>`
}
import { establishHighlighter } from './establishHighlighter'

/**
* A higher-order function which receives payload and options for context up front
Expand All @@ -166,18 +32,20 @@ export const fence = async(payload: Pipeline<PipelineStage.parser>, options: Cod
// fence mutation pipeline
const fence = pipe(
extractInfo(state[idx]),

userRules('before', payload, options),

resolveLanguage(options),
useHighlighter(highlighter, options),
// the 'code' property is converted from a string to a DOM tree
parseLines,
highlightLines(options),
showLineNumbers(options),

userRules('after', payload, options),
// the 'code' property is converted back to a string
convertLinesToCodeBlock,
renderFence,
)

return renderFence(fence)
return fence
}
}
}
75 changes: 0 additions & 75 deletions src/builders/code/mdi/highlighting.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/builders/code/mdi/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export * from './highlighting'
export * from './prism'
export * from './shiki'
export * from './fence'
Expand Down
3 changes: 2 additions & 1 deletion src/builders/code/mdi/prism.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/* eslint-disable @typescript-eslint/no-var-requires */
import type { Grammar } from 'prismjs'
import { escapeHtml } from 'markdown-it/lib/common/utils'
import type { LineClassFn, PrismLanguage, PrismOptions } from '../types'
import type { LineClassFn, PrismOptions } from '../types'
import type { PrismLanguage } from '../utils'

function wrap(line: string, klass: LineClassFn) {
return `<span class="${klass(line)}">${line}</span>`
Expand Down
3 changes: 0 additions & 3 deletions src/builders/code/mdi/shiki.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,6 @@ export async function getShikiHighlighter(options: ShikiOptions) {
)
}

// // ensure language is loaded
// await shiki.loadLanguage(lang)

return {
highlight(code: string, lang: Lang, klass: LineClassFn): string {
const modes = themes.map((t) => {
Expand Down
22 changes: 22 additions & 0 deletions src/builders/code/pipeline/convertLinesToCodeBlock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { pipe } from 'fp-ts/lib/function'
import type { CodeBlockMeta } from '../types'
import { getHtmlFromCodeLines, getHtmlFromNode, wrapCodeBlock } from '../utils'

/**
* Converts the lines of DOM elements back to a singular HTML string which
* represents the `code` block
*/
export function convertLinesToCodeBlock(fence: CodeBlockMeta<'lines'>): CodeBlockMeta<'complete'> {
const code = fence.wrapLines
? pipe(
fence.lines,
wrapCodeBlock(fence.wrapLines),
getHtmlFromNode,
)
: getHtmlFromCodeLines(fence.lines)

return {
...fence,
code,
}
}
Loading