Skip to content

Commit

Permalink
feat: Change automatic line breaks to optional
Browse files Browse the repository at this point in the history
  • Loading branch information
akabekobeko committed Jan 17, 2021
1 parent ffb9f75 commit bebc736
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 26 deletions.
26 changes: 24 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,47 @@ import rehypeStringify from 'rehype-stringify';
import unified, { Processor } from 'unified';
import { hast as clearHtmlLang } from './plugins/clear-html-lang';
import { replace as handleReplace, ReplaceRule } from './plugins/replace';
import markdown from './revive-parse';
import { reviveParse as markdown } from './revive-parse';
import html from './revive-rehype';
import { debug } from './utils/debug';

/**
* Option for convert Markdown to a stringify (HTML).
*/
export interface StringifyMarkdownOptions {
/** Custom stylesheet path/URL. */
style?: string | string[];
/** Output markdown fragments. */
partial?: boolean;
/** Document title (ignored in partial mode). */
title?: string;
/** Document language (ignored in partial mode). */
language?: string;
/** Replacement handler for HTML string. */
replace?: ReplaceRule[];
/** Converts line breaks to `<br>`. */
autoLineBreaks?: boolean;
}

export interface Hooks {
afterParse: ReplaceRule[];
}

/**
* Create Unified processor for MDAST and HAST.
* @param options Options.
* @returns Unified processor.
*/
export function VFM({
style = undefined,
partial = false,
title = undefined,
language = undefined,
replace = undefined,
autoLineBreaks = false,
}: StringifyMarkdownOptions = {}): Processor {
const processor = unified()
.use(markdown)
.use(markdown({ autoLineBreaks }))
.data('settings', { position: false })
.use(html);

Expand All @@ -47,6 +63,12 @@ export function VFM({
return processor;
}

/**
* Convert Markdown to a stringify (HTML).
* @param markdownString Markdown string.
* @param options Options.
* @returns HTML string.
*/
export function stringify(
markdownString: string,
options: StringifyMarkdownOptions = {},
Expand Down
53 changes: 37 additions & 16 deletions src/revive-parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,40 @@ import { mdast as section } from './plugins/section';
import { mdast as toc } from './plugins/toc';
import { inspect } from './utils/debug';

export default [
[markdown, { gfm: true, commonmark: true }],
fencedBlock,
ruby,
breaks,
[footnotes, { inlineNotes: true }],
math,
attr,
slug,
section,
code,
toc,
frontmatter,
metadata,
inspect('mdast'),
] as unified.PluggableList<unified.Settings>;
/**
* Options for Markdown conversion.
*/
export interface MarkdownOptions {
/** Converts line breaks to `<br>`. */
autoLineBreaks: boolean;
}

/**
* Create MDAST parsers.
* @param options Options.
* @returns Parsers.
*/
export const reviveParse = (options: MarkdownOptions) => {
const results = [
[markdown, { gfm: true, commonmark: true }],
fencedBlock,
ruby,
[footnotes, { inlineNotes: true }],
math,
attr,
slug,
section,
code,
toc,
frontmatter,
metadata,
inspect('mdast'),
] as unified.PluggableList<unified.Settings>;

if (options.autoLineBreaks) {
// Line breaks are processed immediately after ruby
results.splice(3, 0, breaks);
}

return results;
};
31 changes: 23 additions & 8 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import * as lib from '../src';
import { ReplaceRule } from '../src/plugins/replace';

function partial(body: string) {
return lib.stringify(body, { partial: true });
/**
* Run VFM stringify in partial mode.
* @param body Markdown string that becomes `<body>` part.
* @param autoLineBreaks Converts line breaks to `<br>`.
* @returns HTML string.
*/
function partial(body: string, autoLineBreaks = false) {
return lib.stringify(body, { partial: true, autoLineBreaks });
}

// Snippet
Expand Down Expand Up @@ -66,13 +72,16 @@ it('handle role', () => {

it('reject incorrect fences', () => {
expect(
partial(`
partial(
`
::::appendix
:::::nested
# Title
:::::
::::
`),
`,
true,
),
).toBe(
`<p>::::appendix<br>
:::::nested<br>
Expand All @@ -82,13 +91,16 @@ it('reject incorrect fences', () => {
);

expect(
partial(`
partial(
`
:::appendix
:::::nested
# Title
:::::
:::
`),
`,
true,
),
).toBe(
`<div class="appendix"><p>:::::nested<br>
# Title<br>
Expand Down Expand Up @@ -143,9 +155,12 @@ A

it('handle hard line break', () => {
expect(
partial(`
partial(
`
a
b`),
b`,
true,
),
).toBe(`<p>a<br>
b</p>`);
});
Expand Down
1 change: 1 addition & 0 deletions tests/inline/ruby.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,6 @@ it(
└─2 text "b|c}"
`,
`<p>{a<br>\nb|c}</p>`,
{ autoLineBreaks: true, partial: true },
),
);
8 changes: 8 additions & 0 deletions tests/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import unistInspect from 'unist-util-inspect';
import { StringifyMarkdownOptions, VFM } from '../src';

/**
* Utility for testing MDAST and HTML strings generated from Markdown.
* @param input Markdown string.
* @param expectedMdast Expected MDAST string.
* @param expectedHtml Expected HTML string.
* @param options Option for convert Markdown to VFM (HTML).
*/
export const buildProcessorTestingCode = (
input: string,
expectedMdast: string,
Expand All @@ -11,6 +18,7 @@ export const buildProcessorTestingCode = (
title: undefined,
language: undefined,
replace: undefined,
autoLineBreaks: false,
},
) => (): any => {
const vfm = VFM(options).freeze();
Expand Down

0 comments on commit bebc736

Please sign in to comment.