Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removed best practice recommendation for mid-word bold and italics, a… #41

Merged
merged 2 commits into from
Aug 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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