Skip to content

Commit

Permalink
Render markdown channels, timestamp and message separately
Browse files Browse the repository at this point in the history
fixes #123
  • Loading branch information
cdupuis committed Aug 24, 2018
1 parent eafc687 commit 3cbf391
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 21 deletions.
19 changes: 3 additions & 16 deletions src/sdm/ui/ConsoleMessageClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,9 @@ export class ConsoleMessageClient implements MessageClient, SlackMessageClient {
* @param {string} markdown
*/
private writeToChannel(channels: string[] | string, markdown: string) {
const outputText = withinRenderableSize(markdown) ?
marked(` **${channels}** ${this.dateString()} ` + markdown, this.markedOptions) :
markdown;
return this.sender(chalk.gray("#") + outputText);
const outputText = ` ${marked(`**${channels}**`, this.markedOptions).trim()} ${this.dateString()} ${
marked(markdown, this.markedOptions).trim()}`;
return this.sender(chalk.gray("#") + outputText + "\n");
}

public dateString() {
Expand All @@ -160,15 +159,3 @@ export class ConsoleMessageClient implements MessageClient, SlackMessageClient {
}

}

/**
* Make a guess whether the `marked` program can render this in markdown in a reasonable amount of time.
* We have observed that it can take 60s to render a twelve-item list of sufficient interestingness.
* See: https://github.com/atomist/sdm-local/issues/123
*/
function withinRenderableSize(markdown: string): boolean {
if (!markdown) {
return true;
}
return (markdown.length < 1000) && (markdown.split("\n").length < 12);
}
20 changes: 15 additions & 5 deletions test/cli/ui/ConsoleMessageClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@ import { ConsoleMessageClient } from "./../../../src/sdm/ui/ConsoleMessageClient
describe("message formatting", () => {
it("doesn't freeze on this dangerous string", async () => {

// This attachment once caused the printing programs to freeze forever.
// 3s for 12
// 10s for 13
// 50s for 14
// 16 never returns within our span of patiences
const suspiciousAttachment: Attachment = {
author_name: "Commands",
fallback: "Commands",
Expand Down Expand Up @@ -54,5 +49,20 @@ describe("message formatting", () => {

await subject.addressChannels({ text: "I am safe", attachments: [suspiciousAttachment] }, "general");
assert(output.includes("WhereAmI"), "It's OK if it didn't render it in markdown, but it should display the whole attachment");
process.stdout.write(output);
});

it("render multi line markdown correct", async () => {
const text = `**test some
bold text**`;

let output = "";
const subject = new ConsoleMessageClient("general", async s => { output = output + s; }, {} as any);

await subject.addressChannels({ text }, "general");
assert(output.includes(`test some
bold text`));
process.stdout.write(output);
});

});

0 comments on commit 3cbf391

Please sign in to comment.