Skip to content

Commit

Permalink
feat(*): add option to output Markdown as plain-text (#470)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nickersoft committed Sep 28, 2023
1 parent 5686f95 commit 71ca0d5
Show file tree
Hide file tree
Showing 20 changed files with 258 additions and 71 deletions.
17 changes: 11 additions & 6 deletions cli/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,17 @@ var App = &cli.App{
Usage: "If a definition cannot be found, attempt to split the query into words of at least length S and look up each word separately. Can be relatively slow.",
Value: 0,
},
&cli.BoolFlag{
Name: "no-process",
Usage: "Skip converting Markdown to HTML",
Value: false,
Action: func(c *cli.Context, value bool) error {
types.SetMarkdownProcessingEnabled(!value)
&cli.StringFlag{
Name: "markdown",
Usage: "strategy for rendering Markdown strings",
Value: "html",
Action: func(c *cli.Context, value string) error {
if value != "text" && value != "html" && value != "disable" {
return fmt.Errorf("Invalid markdown strategy: %s. Must be one of: text, html, disable", value)
}

types.SetMarkdownProcessingStrategy(types.MarkdownStrategy(value))

return nil
},
},
Expand Down
13 changes: 9 additions & 4 deletions cli/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,15 @@ func service(c *cli.Context) error {
queries := make([]string, payload.QueriesLength())
follow := payload.Follow()
split := int(payload.Split())
noProcess := payload.NoProcess()

if noProcess {
types.SetMarkdownProcessingEnabled(false)
markdown := payload.Markdown()

switch markdown {
case MarkdownStrategyHTML:
types.SetMarkdownProcessingStrategy(types.MarkdownStrategyHTML)
case MarkdownStrategyText:
types.SetMarkdownProcessingStrategy(types.MarkdownStrategyText)
case MarkdownStrategyDisable:
types.SetMarkdownProcessingStrategy(types.MarkdownStrategyDisable)
}

for i := 0; i < payload.QueriesLength(); i++ {
Expand Down
41 changes: 34 additions & 7 deletions cli/service_generated.go

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

7 changes: 6 additions & 1 deletion flatbuffers/service.fbs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,16 @@ enum ODictMethod:byte {
Ready = 8
}

enum MarkdownStrategy:short {
Disable = 0,
Text = 1,
HTML = 2
}

table LookupPayload {
follow:bool;
split:int;
no_process:bool;
markdown:MarkdownStrategy;
queries:[string];
}

Expand Down
15 changes: 9 additions & 6 deletions js/src/__generated__/lookup-payload.ts

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

7 changes: 7 additions & 0 deletions js/src/__generated__/markdown-strategy.ts

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

1 change: 1 addition & 0 deletions js/src/__generated__/service.ts

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

20 changes: 19 additions & 1 deletion js/src/dictionary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Builder } from "flatbuffers";
import { CompilePayload } from "./__generated__/compile-payload.js";
import {
LookupPayload,
MarkdownStrategy,
SearchPayload,
SplitPayload,
WritePayload,
Expand Down Expand Up @@ -163,11 +164,28 @@ class Dictionary {

const queriesV = LookupPayload.createQueriesVector(builder, queriesS);

let markdown: MarkdownStrategy | undefined;

if (options.markdownStrategy === "html") {
markdown = MarkdownStrategy.HTML;
}

if (options.markdownStrategy === "disable") {
markdown = MarkdownStrategy.Disable;
}

if (options.markdownStrategy === "text") {
markdown = MarkdownStrategy.Text;
}

LookupPayload.startLookupPayload(builder);
LookupPayload.addQueries(builder, queriesV);
LookupPayload.addFollow(builder, follow ?? false);
LookupPayload.addSplit(builder, split ?? 0);
LookupPayload.addNoProcess(builder, options.skipProcessing ?? false);

if (markdown) {
LookupPayload.addMarkdown(builder, markdown);
}

const payload = LookupPayload.endLookupPayload(builder);

Expand Down
4 changes: 3 additions & 1 deletion js/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ import { POS } from "./__generated__";

export type Query = { word: string; fallback: string } | string;

type MarkdownStrategy = "disable" | "html" | "text";

export interface DictionaryOptions {
defaultSplitThreshold?: number;
}

export interface LookupOptions {
split?: number;
skipProcessing?: boolean;
markdownStrategy?: MarkdownStrategy;
follow?: boolean;
}

Expand Down
62 changes: 43 additions & 19 deletions js/test/__snapshots__/dictionary.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -183,42 +183,31 @@ exports[`Dictionary lookup can split terms 1`] = `
]
`;

exports[`Dictionary lookup looks up terms properly 1`] = `
exports[`Dictionary lookup considers markdown strategies correctly 1`] = `
[
[
{
"etymologies": [
{
"description": "Latin root",
"senses": {
"n": {
"v": {
"definitions": [
{
"examples": [
"There goes a cat!",
],
"notes": [
{
"examples": [
"Some example",
],
"value": "Some definition note",
},
],
"value": "a cat",
"value": "This **is** a _markdown_ test",
},
],
},
},
},
],
"term": "cat",
"term": "markdown",
},
],
]
`;

exports[`Dictionary lookup skips markdown processing if specified 1`] = `
exports[`Dictionary lookup considers markdown strategies correctly 2`] = `
[
[
{
Expand All @@ -229,7 +218,7 @@ exports[`Dictionary lookup skips markdown processing if specified 1`] = `
"v": {
"definitions": [
{
"value": "This <strong>is</strong> a <em>markdown</em> test",
"value": "This is a markdown test",
},
],
},
Expand All @@ -242,7 +231,7 @@ exports[`Dictionary lookup skips markdown processing if specified 1`] = `
]
`;

exports[`Dictionary lookup skips markdown processing if specified 2`] = `
exports[`Dictionary lookup considers markdown strategies correctly 3`] = `
[
[
{
Expand All @@ -253,7 +242,7 @@ exports[`Dictionary lookup skips markdown processing if specified 2`] = `
"v": {
"definitions": [
{
"value": "This **is** a _markdown_ test",
"value": "This <strong>is</strong> a <em>markdown</em> test",
},
],
},
Expand All @@ -266,6 +255,41 @@ exports[`Dictionary lookup skips markdown processing if specified 2`] = `
]
`;

exports[`Dictionary lookup looks up terms properly 1`] = `
[
[
{
"etymologies": [
{
"description": "Latin root",
"senses": {
"n": {
"definitions": [
{
"examples": [
"There goes a cat!",
],
"notes": [
{
"examples": [
"Some example",
],
"value": "Some definition note",
},
],
"value": "a cat",
},
],
},
},
},
],
"term": "cat",
},
],
]
`;

exports[`Dictionary restarts if the process was killed 1`] = `
[
[
Expand Down
17 changes: 14 additions & 3 deletions js/test/dictionary.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,22 @@ describe("Dictionary", () => {
expect(result).toMatchSnapshot();
});

it("skips markdown processing if specified", async () => {
const result1 = await dict2.lookup("markdown", { skipProcessing: false });
const result2 = await dict2.lookup("markdown", { skipProcessing: true });
it("considers markdown strategies correctly", async () => {
const result1 = await dict2.lookup("markdown", {
markdownStrategy: "disable",
});

const result2 = await dict2.lookup("markdown", {
markdownStrategy: "text",
});

const result3 = await dict2.lookup("markdown", {
markdownStrategy: "html",
});

expect(result1).toMatchSnapshot();
expect(result2).toMatchSnapshot();
expect(result3).toMatchSnapshot();
});
});

Expand Down
Loading

0 comments on commit 71ca0d5

Please sign in to comment.