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

Add lines number for source code #28

Open
yalh76 opened this issue Mar 22, 2019 · 1 comment
Open

Add lines number for source code #28

yalh76 opened this issue Mar 22, 2019 · 1 comment

Comments

@yalh76
Copy link
Contributor

yalh76 commented Mar 22, 2019

distbin-read

@gobengo
Copy link
Owner

gobengo commented Mar 22, 2019

A decent enough solution might just be to write a fresh function that takes the result of highlighting the code (an input HTML string that is a <code> or its innerHTML, parses/rewrites it, then returns an HTML string that includes line numbers.
Use it here. https://github.com/gobengo/distbin/blob/master/src/distbin-html/an-activity.ts#L45

starting point:

const highlightedHtmlToLines = (html: string): Array<string> => html.split('\n'); // will probably need a more html-aware implementation
type HtmlString = string
type HtmlRewriter = (in: HtmlString ) => HtmlString ;
const addLineNumbers: HtmlRewriter = (highlightedCode: HtmlString): HtmlString => {
  const lines = highlightedHtmlToLines(highlightedCode)
  // two-column table with cells: lineNumber, actualLineHtml
  return `
  <table>
  ${
    lines
    .map((lineStr, lineIndex) => `
      <tr>
        <td class="lineNumber">${lineIndex}</td>
        <td class="lineOfCode>${lineStr}</td>
      </tr>`
    .join('\n')
  }
  </table>
  `
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants