Created
April 6, 2019 02:27
-
-
Save danielepolencic/6280af77bcdf0e7f43f3ce8358bcb9e4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import marked, { MarkedOptions } from 'marked' | |
import { cat } from 'shelljs' | |
const content = cat('src/architecture/03_bootstrap.md').toString() | |
function code(lang: string) { | |
return (token: marked.Token) => { | |
return token.type === 'code' && token.lang === lang | |
} | |
} | |
function contains(regex: RegExp) { | |
return (token: marked.Token) => { | |
return token.hasOwnProperty('text') && regex.test((token as any).text) | |
} | |
} | |
class Kellogs { | |
private tokens: marked.Token[] | |
private constructor(tokens: marked.Token[]) { | |
this.tokens = tokens | |
} | |
static load(markdown: string) { | |
return Kellogs.of(marked.lexer(content)) | |
} | |
static of(tokens: marked.Token[]) { | |
return new Kellogs(tokens) | |
} | |
code(lang: string) { | |
return Kellogs.of(this.tokens.filter(code(lang))) | |
} | |
contains(regex: RegExp) { | |
return Kellogs.of(this.tokens.filter(contains(regex))) | |
} | |
eq(index: number) { | |
return Kellogs.of(this.tokens[index] ? [this.tokens[index]] : []) | |
} | |
text() { | |
return this.tokens.map(token => { | |
return token.hasOwnProperty('text') && (token as any).text | |
}).join('') | |
} | |
} | |
const $ = Kellogs.load(content) | |
console.log($.code('bash').contains(/kubectl get pods/).text()) | |
console.log($.code('bash').contains(/kubectl/).text()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment