conorhastings / react-native-syntax-highlighter

a syntax highlighter for react native using https://github.com/conorhastings/react-syntax-highlighter under the hood
MIT License
168 stars 24 forks source link

Line numbers #30

Open samuelgoldenbaum opened 3 years ago

samuelgoldenbaum commented 3 years ago

Before I go down the rabbit hole of trying to implement line numbers, is there a reason they were not implemented—anything I need to be aware of?

Desintegrator commented 2 years ago

also looking for this function. Were you manage to implement this? @samuelgoldenbaum

rb-x commented 1 year ago

Here is a partial solution if it can help :

const totalLines = codeString.length;
const maxDigits = totalLines.toString().length;
const formattedCodeString = codeString.map((line, index) => {
  const lineNumber = (index + 1).toString().padStart(maxDigits, ' ');
  return `${lineNumber} | ${line}`;
}).join('\n');
gdxie1 commented 1 month ago

This version actually support showLineNumber, also there is some problems about the format of linenumber. Here is my solution temporarily.

function createNativeElement({ node, stylesheet, key, defaultColor, fontFamily, fontSize = 12 }) { const { properties, type, tagName: TagName, value } = node; const startingStyle = { fontFamily, fontSize, height: fontSize + 15, paddingRight: 30 };

if (type === 'text') { return ( <Text key={key} style={Object.assign({ color: defaultColor }, startingStyle)}

{value} ); } else if (TagName) { const childrenCreator = createChildren({ stylesheet, fontSize, fontFamily }); const style = createStyleObject( properties.className, Object.assign( { color: defaultColor }, properties.style, startingStyle ), stylesheet ); const children = childrenCreator(node.children, style.color || defaultColor); if (properties.className.indexOf("linenumber") != -1) { return <><Text key={key} style={[style, {}]}>{children} </> } if (properties.className.length) { return children } return {children}; } }