Skip to content

Commit

Permalink
Merge pull request #11 from jackyzha0/rich-text
Browse files Browse the repository at this point in the history
  • Loading branch information
jackyzha0 authored Mar 16, 2022
2 parents 415364d + 10bebf1 commit 53df774
Show file tree
Hide file tree
Showing 7 changed files with 232 additions and 105 deletions.
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,19 @@ interface Config {
* A mode that designates what form the input text is in and should be interpreted as. Defaults to 'text'.
*/
textMode?: TextMode;
/**
* Determines the wrapper element type for HTML elements. Defaults to 'span'.
*/
htmlContainerTag?: keyof HTMLElementTagNameMap;
/**
* Only valid when textMode is 'text'. Used to insert HTML element like blockquotes, line breaks, bold, and emphasis in plain text mode.
*/
specialCharacters?: TextReplacements;
}
```

You would use this by passing a custom configuration object into the function in order to override any of the defaults above. For example, this is how you would create telescopic text with custom HTML tags:

```javascript
const content = `
* Some <b>rich</b> text
Expand All @@ -104,7 +113,35 @@ const poemContent = createTelescopicTextFromBulletedList(content, config);

You can check out a more detailed example in `demo/index.html`

If you are using plain 'text' as the textMode, you can also define an object containing special characters and the rules for how to replace them.

```typescript
interface TextReplacements {
// Each entry is keyed by its regex string match
// It defines a function that takes in the current line of text as well as its parent node
// and
[key: string]: (lineText: string) => HTMLElement
}

// for example, here's a text replacement rule for bolding text that is wrapped with *
"\\*(.*)\\*": (lineText) => {
const el = document.createElement("strong");
el.appendChild(document.createTextNode(lineText));
return el;
}
```

By default, only these special characters have text replacements
- line breaks (`---`)
- bold (`*...*`)
- emphasis (`_..._`)
To disable this, you can pass in an empty object for special characters:

```typescript
const poemContent = createTelescopicTextFromBulletedList(content, {
specialCharacters: {}
});
```

## Types
```typescript
Expand Down
12 changes: 9 additions & 3 deletions lib/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,17 @@
color: var(--telescope-text-color);
}

#telescope blockquote {
#telescope#telescope blockquote {
margin-left: 0;
padding-left: 1rem;
border-left: 3px solid var(--telescope-text-color);
display: block !important;
display: block;
}

#telescope#telescope hr {
border: 1px solid var(--telescope-text-color);
margin: 1em 0;
display: block;
}

#telescope .details {
Expand All @@ -39,4 +45,4 @@
#telescope .details *, #telescope p * {
list-style: none;
display: inline;
}
}
14 changes: 11 additions & 3 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ declare enum TextMode {
*/
Html = "html"
}
declare const TextModeValues: TextMode[];
declare function isTextMode(e: any): e is TextMode[keyof TextMode];
interface Config {
/**
* Character used to separate entries on the same level. Defaults to a single space (" ")
Expand All @@ -43,12 +43,20 @@ interface Config {
*/
textMode?: TextMode;
/**
*
* Determines the wrapper element type for HTML elements. Defaults to 'span'.
*/
htmlContainerTag?: keyof HTMLElementTagNameMap;
/**
* Only valid when textMode is 'text'. Used to insert HTML element like blockquotes, line breaks, bold, and emphasis in plain text mode.
*/
specialCharacters?: TextReplacements;
}
declare type CreateTelescopicTextConfig = Pick<Config, "shouldExpandOnMouseOver" | "textMode" | "htmlContainerTag">;
declare type CreateTelescopicTextConfig = Pick<Config, "shouldExpandOnMouseOver" | "textMode" | "htmlContainerTag" | "specialCharacters">;
declare let _lastHoveredTime: number;
interface TextReplacements {
[key: string]: (lineText: string) => HTMLElement;
}
declare const DefaultReplacements: TextReplacements;
declare function _hydrate(line: Content, node: any, config?: CreateTelescopicTextConfig): void;
declare function _createTelescopicText(content: Content[], config?: CreateTelescopicTextConfig): HTMLDivElement;
/*****************/
Expand Down
70 changes: 48 additions & 22 deletions lib/index.js

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

Loading

0 comments on commit 53df774

Please sign in to comment.