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

[Code blocks] Clean up, split into multiple modules #217

Merged
merged 3 commits into from
Oct 17, 2022
Merged
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
2 changes: 2 additions & 0 deletions script/server
Original file line number Diff line number Diff line change
@@ -3,6 +3,8 @@

set -e

rm -rf _site

# When the second command in the foreground is killed with Ctrl-c, the first
# command in the background is killed also.
# Based on https://unix.stackexchange.com/a/204619
2 changes: 1 addition & 1 deletion src_js/Config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Storage from './utils/Storage';
import { CodeblockVariant } from './components/main_content/types';
import { CodeblockVariant } from './components/main_content/enhanced_code_blocks/codeblockConsts';

const SUBTHEME_NAME_STORAGE_KEY = 'spec_subtheme_name';
const SUBTHEME_MODE_STORAGE_KEY = 'spec_subtheme_mode';
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { parseCodeHighlightRanges } from '../parseCodeHighlightRanges';

describe('parseCodeHighlightRanges', () => {
test('empty string', () => {
expect(parseCodeHighlightRanges('', 30)).toEqual(new Set());
});

test('invalid string', () => {
expect(parseCodeHighlightRanges('spam and eggs', 30)).toEqual(new Set());
});

test('invalid comma-separated string', () => {
expect(parseCodeHighlightRanges('spam,eggs', 30)).toEqual(new Set());
});

test('single line number', () => {
expect(parseCodeHighlightRanges('24', 30)).toEqual(new Set([24]));
});

test('single line number out of bounds', () => {
expect(parseCodeHighlightRanges('32', 30)).toEqual(new Set());
});

test('single line number at upper bound', () => {
expect(parseCodeHighlightRanges('30', 30)).toEqual(new Set([30]));
});

test('single line number at lower bound', () => {
expect(parseCodeHighlightRanges('1', 30)).toEqual(new Set([1]));
});

test('single range', () => {
expect(parseCodeHighlightRanges('18-21', 30)).toEqual(
new Set([18, 19, 20, 21]),
);
});

test('single range completely out of bounds', () => {
expect(parseCodeHighlightRanges('32-38', 30)).toEqual(new Set());
});

test('single range partially out of bounds', () => {
expect(parseCodeHighlightRanges('28-32', 30)).toEqual(new Set());
});

test('single range with inverted bounds', () => {
expect(parseCodeHighlightRanges('28-26', 30)).toEqual(new Set());
});

test('multiple single numbers', () => {
expect(parseCodeHighlightRanges('4,0,6', 30)).toEqual(new Set([4, 6]));
});

test('multiple ranges', () => {
expect(parseCodeHighlightRanges('14-15,24-32,12-14', 30)).toEqual(
new Set([12, 13, 14, 15]),
);
});

test('multiple ranges and lines', () => {
expect(parseCodeHighlightRanges('4, 24-27, 3-5', 30)).toEqual(
new Set([3, 4, 5, 24, 25, 26, 27]),
);
});
});
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import useEnhancedCodeBlocks, {
parseCodeHighlightRanges,
} from '../useEnhancedCodeBlocks';
import useEnhancedCodeBlocks from '../useEnhancedCodeBlocks';

jest.mock('../../../Config', () => ({
jest.mock('../../../../Config', () => ({
USE_LEGACY_CODE_BLOCKS: false,
}));

@@ -134,67 +132,3 @@ describe('useEnhancedCodeBlocks', () => {
});
});
});

describe('parseCodeHighlightRanges', () => {
test('empty string', () => {
expect(parseCodeHighlightRanges('', 30)).toEqual(new Set());
});

test('invalid string', () => {
expect(parseCodeHighlightRanges('spam and eggs', 30)).toEqual(new Set());
});

test('invalid comma-separated string', () => {
expect(parseCodeHighlightRanges('spam,eggs', 30)).toEqual(new Set());
});

test('single line number', () => {
expect(parseCodeHighlightRanges('24', 30)).toEqual(new Set([24]));
});

test('single line number out of bounds', () => {
expect(parseCodeHighlightRanges('32', 30)).toEqual(new Set());
});

test('single line number at upper bound', () => {
expect(parseCodeHighlightRanges('30', 30)).toEqual(new Set([30]));
});

test('single line number at lower bound', () => {
expect(parseCodeHighlightRanges('1', 30)).toEqual(new Set([1]));
});

test('single range', () => {
expect(parseCodeHighlightRanges('18-21', 30)).toEqual(
new Set([18, 19, 20, 21]),
);
});

test('single range completely out of bounds', () => {
expect(parseCodeHighlightRanges('32-38', 30)).toEqual(new Set());
});

test('single range partially out of bounds', () => {
expect(parseCodeHighlightRanges('28-32', 30)).toEqual(new Set());
});

test('single range with inverted bounds', () => {
expect(parseCodeHighlightRanges('28-26', 30)).toEqual(new Set());
});

test('multiple single numbers', () => {
expect(parseCodeHighlightRanges('4,0,6', 30)).toEqual(new Set([4, 6]));
});

test('multiple ranges', () => {
expect(parseCodeHighlightRanges('14-15,24-32,12-14', 30)).toEqual(
new Set([12, 13, 14, 15]),
);
});

test('multiple ranges and lines', () => {
expect(parseCodeHighlightRanges('4, 24-27, 3-5', 30)).toEqual(
new Set([3, 4, 5, 24, 25, 26, 27]),
);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
export enum CodeblockVariant {
ENHANCED = 'enhanced',
NO_LINE_NUMBERS = 'no-line-numbers',
LEGACY = 'legacy',
}

/**
* The class used on each <td> element that represents the contents of the code
* block.
*/
export const CODEBLOCK_LINE_CLASS = 'primer-spec-code-block-line-code';

/**
* We use the following class to ensure that we don't double-process code
* blocks.
*/
export const CODEBLOCK_PROCESSED_CLASS = 'primer-spec-code-block-processed';

/**
* Since we want to linkify code block titles, this is the class used to
* identify them to AnchorJS.
*/
export const CODEBLOCK_TITLE_CLASS = 'primer-spec-code-block-title';

/**
* We perform special handling for blocks in the `console` language: If a user
* clicks the line number, the entire line will be highlighted EXCLUDING the
* prompt (`$`) at the beginning, if it exists.
* See the special handling in `createCodeBlockLine()`.
*/
export const LANGUAGE_CONSOLE = 'console';
Loading