Skip to content

Commit

Permalink
add renderMarkdown to ICompiler (#16)
Browse files Browse the repository at this point in the history
* add renderMarkdown to ICompiler
just the markdown piece, useful for plugins that don't need full `renderBlock`
* KssPlugin markdownifies documentation + markup for syntax highlighting
  • Loading branch information
giladgray authored Mar 15, 2017
1 parent fad5bdd commit 50f7550
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
4 changes: 3 additions & 1 deletion src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ export class Compiler implements ICompiler {
return { content, metadata, renderedContent };
}

public renderMarkdown = (markdown: string) => marked(markdown, this.markedOptions);

/**
* Converts the content string into an array of `ContentNode`s. If the
* `contents` option is `html`, the string nodes will also be rendered with
Expand All @@ -51,7 +53,7 @@ export class Compiler implements ICompiler {
private renderContents(content: string, reservedTagWords: string[]) {
const splitContents = this.parseTags(content, reservedTagWords);
return splitContents
.map((node) => typeof node === "string" ? marked(node, this.markedOptions) : node)
.map((node) => typeof node === "string" ? this.renderMarkdown(node) : node)
.filter((node) => node !== "");
}

Expand Down
27 changes: 18 additions & 9 deletions src/plugins/kss.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,16 @@ export interface IKssModifier {
export interface IKssExample {
/** Raw documentation string. */
documentation: string;
/** HTML markup for example, with `{{.modifier}}` templates. */
/**
* Raw HTML markup for example with `{{.modifier}}` templates,
* to be used to render the markup for each modifier.
*/
markup: string;
/**
* Syntax-highlighted version of the markup HTML, to be used
* for rendering the markup itself with pretty colors.
*/
markupHtml: string;
/** Array of modifiers supported by HTML markup. */
modifiers: IKssModifier[];
/** Unique reference for addressing this example. */
Expand All @@ -39,10 +47,10 @@ export class KssPlugin implements IPlugin<IKssPluginData> {
public constructor(private options: kss.IOptions) {
}

public compile(cssFiles: IFile[], { objectify }: ICompiler) {
public compile(cssFiles: IFile[], dm: ICompiler) {
const styleguide = this.parseFiles(cssFiles);
const sections = styleguide.sections().map(convertSection);
const css = objectify(sections, (s) => s.reference);
const sections = styleguide.sections().map((s) => convertSection(s, dm));
const css = dm.objectify(sections, (s) => s.reference);
return { css };
}

Expand All @@ -57,18 +65,19 @@ export class KssPlugin implements IPlugin<IKssPluginData> {
}
}

function convertSection(section: kss.ISection): IKssExample {
function convertSection(section: kss.ISection, dm: ICompiler): IKssExample {
return {
documentation: section.description(),
documentation: dm.renderMarkdown(section.description()),
markup: section.markup() || "",
modifiers: section.modifiers().map(convertModifier),
markupHtml: dm.renderMarkdown(`\`\`\`html\n${section.markup() || ""}\n\`\`\``),
modifiers: section.modifiers().map((mod) => convertModifier(mod, dm)),
reference: section.reference(),
};
}

function convertModifier(mod: kss.IModifier): IKssModifier {
function convertModifier(mod: kss.IModifier, dm: ICompiler): IKssModifier {
return {
documentation: mod.description(),
documentation: dm.renderMarkdown(mod.description()),
name: mod.name(),
};
}
5 changes: 5 additions & 0 deletions src/plugins/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ export interface ICompiler {
* is.
*/
renderBlock: (blockContent: string, reservedTagWords?: string[]) => IBlock;

/**
* Render a string of markdown to HTML, using the options from `Documentalist`.
*/
renderMarkdown: (markdown: string) => string;
}

export interface IPlugin<T> {
Expand Down

1 comment on commit 50f7550

@blueprint-bot
Copy link

Choose a reason for hiding this comment

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

add renderMarkdown to ICompiler (#16)

Preview: docs

Please sign in to comment.