Skip to content

Commit

Permalink
Removed best practice recommendation for mid-word bold and italics, a… (
Browse files Browse the repository at this point in the history
#41)

* Removed best practice recommendation for mid-word bold and italics, as it should be the responsibility of the caller who is generating markdown for their target markdown viewer.

* Simplified test and wording.
  • Loading branch information
kgar committed Aug 26, 2023
1 parent ee4bc3b commit efc8d58
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 101 deletions.
17 changes: 17 additions & 0 deletions src/github-issues.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { tsMarkdown } from './rendering';

describe('https://github.com/kgar/ts-markdown/issues/40', () => {
describe('given text which contains multiple underscores within a single word', () => {
const paragraphText = `Hello, t_word_with_underscores`;

const entries = [
{
p: paragraphText,
},
];

test('should be the same text that was originally provided', () => {
expect(tsMarkdown(entries)).toBe(paragraphText);
});
});
});
76 changes: 0 additions & 76 deletions src/renderers/text.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,82 +13,6 @@ describe('given a text entry', () => {
});
});

/**
* Based on this recommendation: https://www.markdownguide.org/basic-syntax/#bold-best-practices
*/
describe('with mid-word bolding', () => {
const textEntry: TextEntry = {
text: ['He', { bold: 'll' }, 'o'],
};

test('renders a string with asterisks to denote mid-word bolding', () => {
expect(tsMarkdown([textEntry])).toBe('He**ll**o');
});
});

describe('with mid-word bolding and underscore indicator', () => {
const textEntry: TextEntry = {
text: ['He', { bold: 'll', indicator: '_' }, 'o'],
};

test('renders a string with asterisks to denote mid-word bolding, ignoring indicator setting per best practice', () => {
expect(tsMarkdown([textEntry])).toBe('He**ll**o');
});
});

/**
* Based on this recomendation: https://www.markdownguide.org/basic-syntax/#bold-best-practices
*/
describe('with mid-word italicizing', () => {
const data: MarkdownEntry[] = [
{
text: ['He', { italic: 'll' }, 'o'],
},
];

test('renders a string with asterisks to denote mid-word italicizing', () => {
expect(tsMarkdown(data)).toBe('He*ll*o');
});
});

describe('with mid-word tailicizing and underscore indicator', () => {
const textEntry: TextEntry = {
text: ['He', { italic: 'll', indicator: '_' }, 'o'],
};

test('renders a string with asterisks to denote mid-word italicizing, ignoring indicator setting per best practice', () => {
expect(tsMarkdown([textEntry])).toBe('He*ll*o');
});
});

describe('with mid-word bold and italics and underscore indicator', () => {
const textEntry: TextEntry = {
text: [
'He',
{ italic: { bold: 'll', indicator: '_' }, indicator: '_' },
'o',
],
};

test('renders a string with asterisks to denote mid-word bold and italicizing, ignoring indicator setting per best practice', () => {
expect(tsMarkdown([textEntry])).toBe('He***ll***o');
});
});

describe('with mid-word bold and italics and mixed indicators', () => {
const textEntry: TextEntry = {
text: [
'mid',
{ italic: { bold: 'word', indicator: '_' }, indicator: '*' },
'rich-text',
],
};

test('renders a string with asterisks to denote mid-word bold and italicizing, ignoring indicator setting per best practice', () => {
expect(tsMarkdown([textEntry])).toBe('mid***word***rich-text');
});
});

describe('with a superscript', () => {
const textEntry: TextEntry = {
text: ['H', { sup: '2' }, 'O'],
Expand Down
25 changes: 0 additions & 25 deletions src/rendering.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,34 +36,9 @@ export function tsMarkdown(
? options.onDocumentFootnoteAppended(data, document, options)
: document;

// TODO: Formalize a post-render callback option
document = correctInvalidMidWordBoldAndItalics(document);

return document;
}

/**
* Finds and corrects and mid-word bold/italics that use hyphens, changing the hyphens to asterisks, per best practice: https://www.markdownguide.org/basic-syntax/#bold-best-practices
*
* @param document the rendered document
* @returns document with mid-world bold and italics set to asterisks
*/
function correctInvalidMidWordBoldAndItalics(document: string): string {
return document
.replace(
/(?<pretext>[^\_^\s])(?<prefix1>[\_]{3})(?<text1>[^\s^\_]+)(?<suffix1>[\_]{3})|(?<prefix2>[\_]{3})(?<text2>[^\s^\_]+)(?<suffix2>[\_]{3})(?<posttext>[^\_^\s])/g,
'$<pretext>***$<text1>$<text2>***$<posttext>'
)
.replace(
/(?<pretext>[^\_^\s])(?<prefix1>[\_]{2})(?<text1>[^\s^\_]+)(?<suffix1>[\_]{2})|(?<prefix2>[\_]{2})(?<text2>[^\s^\_]+)(?<suffix2>[\_]{2})(?<posttext>[^\_^\s])/g,
'$<pretext>**$<text1>$<text2>**$<posttext>'
)
.replace(
/(?<pretext>[^\_^\s])(?<prefix1>[\_]{1})(?<text1>[^\s^\_]+)(?<suffix1>[\_]{1})|(?<prefix2>[\_]{1})(?<text2>[^\s^\_]+)(?<suffix2>[\_]{1})(?<posttext>[^\_^\s])/g,
'$<pretext>*$<text1>$<text2>*$<posttext>'
);
}

/**
* Reduces an array of markdown entries to a single string.
*
Expand Down

0 comments on commit efc8d58

Please sign in to comment.