Skip to content

Commit

Permalink
chore: upgrade deps (#125)
Browse files Browse the repository at this point in the history
  • Loading branch information
atomiks authored Nov 30, 2023
1 parent 8371d87 commit bd6c1ca
Show file tree
Hide file tree
Showing 12 changed files with 925 additions and 169 deletions.
984 changes: 867 additions & 117 deletions package-lock.json

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,23 +65,23 @@
"jest-file-snapshot": "^0.5.0",
"jest-mock": "^29.5.0",
"mdast-util-to-hast": "^12.1.0",
"prettier": "^2.8.8",
"prettier": "^3.1.0",
"remark": "^14.0.2",
"rollup": "^2.60.2",
"rome": "12.1.3",
"shiki": "^0.14.0",
"ts-jest": "^29.1.0",
"typescript": "^4.9.5",
"vite": "^4.3.9",
"vitest": "^0.32.2",
"@types/hast": "^2.0.0"
"vitest": "^0.32.2"
},
"dependencies": {
"@types/hast": "^3.0.3",
"hash-obj": "^4.0.0",
"hast-util-to-string": "^3.0.0",
"parse-numeric-range": "^1.3.0",
"hast-util-to-string": "^2.0.0",
"unist-util-visit": "^4.0.0",
"rehype-parse": "^8.0.3",
"unified": "^10.1.2"
"rehype-parse": "^9.0.0",
"unified": "^11.0.4",
"unist-util-visit": "^5.0.0"
}
}
10 changes: 5 additions & 5 deletions src/chars/charsHighlighter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ export function charsHighlighter(
options: CharsHighlighterOptions,
onVisitHighlightedChars?: (
element: CharsElement,
id: string | undefined
) => void
id: string | undefined,
) => void,
) {
const { ranges = [] } = options;
const textContent = toString(element);
Expand All @@ -46,7 +46,7 @@ export function charsHighlighter(
element,
chars,
startIndex,
ignoreChars
ignoreChars,
);

// maybe throw / notify due to failure here
Expand All @@ -57,15 +57,15 @@ export function charsHighlighter(
elementsToWrap,
options,
ignoreChars,
onVisitHighlightedChars
onVisitHighlightedChars,
);

// re-start from the 'last' node (the chars or part of them may exist
// multiple times in the same node)
// account for possible extra nodes added from split with - 2
startIndex = Math.max(
elementsToWrap[elementsToWrap.length - 1].index - 2,
0
0,
);

textContent = element.children
Expand Down
4 changes: 2 additions & 2 deletions src/chars/getElementsToHighlight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export function getElementsToHighlight(
element: Element,
chars: string,
startIndex = 0,
ignoreChars = false
ignoreChars = false,
): Array<{ element: Element; index: number }> {
const toWrap = [];
let charsSoFar = '';
Expand All @@ -34,7 +34,7 @@ export function getElementsToHighlight(
// ignore any previously matched chars within
hasOwnProperty(
maybeElement.properties ?? {},
'rehype-pretty-code-visited'
'rehype-pretty-code-visited',
)
) {
continue;
Expand Down
6 changes: 3 additions & 3 deletions src/chars/wrapHighlightedChars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ export function wrapHighlightedChars(
ignoreWord: boolean,
onVisitHighlightedChars?: (
element: CharsElement,
id: string | undefined
) => void
id: string | undefined,
) => void,
) {
if (!elementsToWrap || elementsToWrap.length === 0) {
return;
Expand All @@ -35,7 +35,7 @@ export function wrapHighlightedChars(
tagName: 'span',
properties: { 'data-highlighted-chars-wrapper': '' },
children: elementsToWrap.map(({ element }) => element),
}
},
);

const element = parentElement.children[elementsToWrap[0].index];
Expand Down
33 changes: 20 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ function toFragment(
lineNumbersMaxDigits = 1,
onVisitTitle,
onVisitCaption,
}: ToFragmentProps
}: ToFragmentProps,
) {
element.tagName = inline ? 'span' : 'div';
// User can replace this with a real Fragment at runtime
Expand All @@ -55,8 +55,11 @@ function toFragment(
const code = pre.children[0];

// Remove class="shiki"
if (Array.isArray(pre.properties?.className) && pre.properties?.className.includes('shiki')) {
const className = pre.properties.className.filter(c => c !== 'shiki');
if (
Array.isArray(pre.properties?.className) &&
pre.properties?.className.includes('shiki')
) {
const className = pre.properties.className.filter((c) => c !== 'shiki');
pre.properties.className = className.length > 0 ? className : undefined;
}

Expand Down Expand Up @@ -140,7 +143,7 @@ const globalHighlighterCache = new Map<
const hastParser = unified().use(rehypeParse, { fragment: true });

export default function rehypePrettyCode(
options: Options = {}
options: Options = {},
): void | Transformer<Root, Root> {
const {
grid = true,
Expand All @@ -166,7 +169,7 @@ export default function rehypePrettyCode(
onVisitHighlightedChars,
getHighlighter,
},
{ algorithm: 'sha1' }
{ algorithm: 'sha1' },
);
let highlighterCache = globalHighlighterCache.get(optionsHash);
if (!highlighterCache) {
Expand Down Expand Up @@ -239,12 +242,13 @@ export default function rehypePrettyCode(
const color =
highlighter
.getTheme()
.settings.find(({ scope }: { scope?: string[] }) =>
scope?.includes(tokensMap[meta.slice(1)] ?? meta.slice(1))
.settings.find(
({ scope }: { scope?: string[] }) =>
scope?.includes(tokensMap[meta.slice(1)] ?? meta.slice(1)),
)?.settings.foreground ?? 'inherit';

trees[mode] = hastParser.parse(
`<pre><code><span style="color:${color}">${strippedValue}</span></code></pre>`
`<pre><code><span style="color:${color}">${strippedValue}</span></code></pre>`,
);
} else {
let html;
Expand Down Expand Up @@ -353,7 +357,7 @@ export default function rehypePrettyCode(
} catch (e) {
// Fallback to plain text if a language has not been registered
trees[mode] = hastParser.parse(
highlighter.codeToHtml(strippedValue, 'txt')
highlighter.codeToHtml(strippedValue, 'txt'),
);
}
}
Expand All @@ -377,7 +381,7 @@ export default function rehypePrettyCode(
}

const lineNumbersStartAtMatch = reverseString(meta).match(
/(?:\}(\d+){)?srebmuNeniLwohs(?!(.*)(\/))/
/(?:\}(\d+){)?srebmuNeniLwohs(?!(.*)(\/))/,
);
const startNumberString = lineNumbersStartAtMatch?.[1];
if (startNumberString) {
Expand All @@ -399,8 +403,11 @@ export default function rehypePrettyCode(
element.children = [{ type: 'text', value: ' ' }];
}

const className = element.properties.className.filter(c => c !== 'line');
element.properties.className = className.length > 0 ? className : undefined;
const className = element.properties.className.filter(
(c) => c !== 'line',
);
element.properties.className =
className.length > 0 ? className : undefined;
element.properties['data-line'] = '';
onVisitLine?.(element as LineElement);

Expand All @@ -416,7 +423,7 @@ export default function rehypePrettyCode(
element,
words,
wordOptions,
onVisitHighlightedChars
onVisitHighlightedChars,
);

lineNumbersMaxDigits++;
Expand Down
4 changes: 2 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export function isShikiTheme(value: any): value is IShikiTheme {
}

export function isElement(
value: ElementContent | Element | Root | RootContent | null
value: ElementContent | Element | Root | RootContent | null | undefined,
): value is Element {
return value ? value.type === 'element' : false;
}
Expand All @@ -18,7 +18,7 @@ export function isText(value: ElementContent | null): value is Text {

export function hasOwnProperty(
object: Record<string, unknown>,
string: string
string: string,
) {
return {}.hasOwnProperty.call(object, string);
}
2 changes: 1 addition & 1 deletion test/chars-highlighter-playground/index.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!DOCTYPE html>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
Expand Down
14 changes: 7 additions & 7 deletions test/chars-highlighter-playground/src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ function App() {
highlighter.current.codeToHtml(
editorRef.current.getDoc().getValue('\n'),
mode,
DEFAULT_THEME
)
DEFAULT_THEME,
),
);
}
if (highlighter.current) {
Expand All @@ -82,8 +82,8 @@ function App() {
plugins: [htmlParser],
}),
'html',
DEFAULT_THEME
)
DEFAULT_THEME,
),
);
}
}, [mode]);
Expand All @@ -94,7 +94,7 @@ function App() {
container.innerHTML = highlighter.current.codeToHtml(
editorRef.current.getDoc().getValue('\n'),
mode,
DEFAULT_THEME
DEFAULT_THEME,
);
}
let options = {
Expand All @@ -114,11 +114,11 @@ function App() {
`Something went wrong with highlighting!
expect: ${container.textContent}
got: ${value}`
got: ${value}`,
);
}
},
[word, wordNumbers, value, mode]
[word, wordNumbers, value, mode],
);

React.useEffect(() => {
Expand Down
2 changes: 1 addition & 1 deletion test/chars-highlighter-playground/src/main.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ ReactDOM.render(
<React.StrictMode>
<App />
</React.StrictMode>,
document.getElementById('root')
document.getElementById('root'),
);
18 changes: 9 additions & 9 deletions test/fixtures.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,22 @@ const getTheme = (multiple) => {
const singleTheme = JSON.parse(
readFileSync(
join(__dirname, '../node_modules/shiki/themes/github-dark.json'),
'utf-8'
)
'utf-8',
),
);

const multipleTheme = {
dark: JSON.parse(
readFileSync(
join(__dirname, '../node_modules/shiki/themes/github-dark.json'),
'utf-8'
)
'utf-8',
),
),
light: JSON.parse(
readFileSync(
join(__dirname, '../node_modules/shiki/themes/github-light.json'),
'utf-8'
)
'utf-8',
),
),
};
return multiple ? multipleTheme : singleTheme;
Expand Down Expand Up @@ -108,7 +108,7 @@ const runFixture = async (fixture, fixtureName, getHighlighter) => {
getHighlighter,
});

const htmlString = prettier.format(html, { parser: 'html' });
const htmlString = await prettier.format(html, { parser: 'html' });
return { htmlString, resultHTMLPath };
};

Expand All @@ -127,7 +127,7 @@ describe('Single theme', () => {
const { htmlString, resultHTMLPath } = await runFixture(
fixture,
fixtureName,
getHighlighter
getHighlighter,
);

expect(defaultStyle + htmlString).toMatchFile(resultHTMLPath);
Expand All @@ -151,7 +151,7 @@ describe('Multiple theme', () => {
const { htmlString, resultHTMLPath } = await runFixture(
fixture,
fixtureName,
getHighlighter
getHighlighter,
);

expect(defaultStyle + htmlString).toMatchFile(resultHTMLPath);
Expand Down
3 changes: 1 addition & 2 deletions test/fixtures/caption.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ caption="Immutable variable declaration"
const code = true;
```

caption="Immutable variable declaration"
title="./components/index.js"
caption="Immutable variable declaration" title="./components/index.js"

```js caption="Immutable variable declaration" title="./components/index.js"
const caption = 'immutable';
Expand Down

0 comments on commit bd6c1ca

Please sign in to comment.