UNPKG

react-native-code-highlighter

Version:
63 lines (62 loc) 1.98 kB
"use strict"; import React, { useMemo } from "react"; import { ScrollView, StyleSheet, Text, View } from "react-native"; import SyntaxHighlighter from "react-syntax-highlighter"; import { trimNewlines } from "trim-newlines"; import { getRNStylesFromHljsStyle } from "./../utils/styles"; import { jsx as _jsx } from "react/jsx-runtime"; export const CodeHighlighter = ({ children, textStyle, hljsStyle, scrollViewProps, containerStyle, ...rest }) => { const stylesheet = useMemo(() => getRNStylesFromHljsStyle(hljsStyle), [hljsStyle]); const getStylesForNode = node => { const classes = node.properties?.className ?? []; return classes.map(c => stylesheet[c]).filter(c => !!c); }; const renderNode = (nodes, keyPrefix = "row") => nodes.reduce((acc, node, index) => { const keyPrefixWithIndex = `${keyPrefix}_${index}`; if (node.children) { const styles = StyleSheet.flatten([textStyle, { color: stylesheet.hljs?.color }, getStylesForNode(node)]); acc.push(/*#__PURE__*/_jsx(Text, { style: styles, children: renderNode(node.children, `${keyPrefixWithIndex}_child`) }, keyPrefixWithIndex)); } if (node.value) { acc.push(trimNewlines(String(node.value))); } return acc; }, []); const renderer = props => { const { rows } = props; return /*#__PURE__*/_jsx(ScrollView, { ...scrollViewProps, horizontal: true, contentContainerStyle: [stylesheet.hljs, scrollViewProps?.contentContainerStyle, containerStyle], children: /*#__PURE__*/_jsx(View, { onStartShouldSetResponder: () => true, children: renderNode(rows) }) }); }; return /*#__PURE__*/_jsx(SyntaxHighlighter, { ...rest, renderer: renderer, CodeTag: View, PreTag: View, style: {}, testID: "react-native-code-highlighter", children: children }); }; export default CodeHighlighter; //# sourceMappingURL=CodeHighlighter.js.map