<commit_before>async = require 'async'
fs = require 'fs'
hljs = require 'highlight.js'
marked = require 'marked'
path = require 'path'
url = require 'url'
yaml = require 'js-yaml'

hljs.configure {classPrefix: ''} # keep compatibility with old stylesheets (pre hljs 8.0.0)

# monkeypatch to add url resolving to marked
if not marked.InlineLexer.prototype._outputLink?
  marked.InlineLexer.prototype._outputLink = marked.InlineLexer.prototype.outputLink
  marked.InlineLexer.prototype._resolveLink = (href) -> href
  marked.InlineLexer.prototype.outputLink = (cap, link) ->
    link.href = @_resolveLink link.href
    return @_outputLink cap, link

resolveLink = (content, uri, baseUrl) ->
  ### Resolve *uri* relative to *content*, resolves using
      *baseUrl* if no matching content is found. ###
  uriParts = url.parse uri
  if uriParts.protocol
    # absolute uri
    return uri
  else
    # search pathname in content tree relative to *content*
    nav = content.parent
    path = uriParts.pathname?.split('/') or []
    while path.length and nav?
      part = path.shift()
      if part == ''
        # uri begins with / go to contents root
        nav = nav.parent while nav.parent
      else if part == '..'
        nav = nav.parent
      else
        nav = nav[part]
    if nav?.getUrl?
      return nav.getUrl() + [uriParts.hash]
    return url.resolve baseUrl, uri

parseMarkdownSync = (content, markdown, baseUrl, options) ->
  ### Parse *markdown* found on *content* node of contents and
  resolve links by navigating in the content tree. use *baseUrl* as a last resort
  returns html. ###

  marked.InlineLexer.prototype._resolveLink = (uri) ->
    resolveLink content, uri, baseUrl

  options.highlight = (code, lang) ->
    try 
      result = if lang and hljs.getLanguage(lang) then hljs.highlight(lang, code) else hljs.highlightAuto(code)
      return result.value
    catch error
      return code
  
  marked.setOptions options
  return marked markdown

module.exports = (env, callback) ->

  class MarkdownPage extends env.plugins.Page

    constructor: (@filepath, @metadata, @markdown) ->

    getLocation: (base) ->
      uri = @getUrl base
      return uri[0..uri.lastIndexOf('/')]

    getHtml: (base=env.config.baseUrl) ->
      ### parse @markdown and return html. also resolves any relative urls to absolute ones ###
      options = env.config.markdown or {}
      return parseMarkdownSync this, @markdown, @getLocation(base), options
<commit_msg>Make auto-highlight opt-in<commit_after>async = require 'async'
fs = require 'fs'
hljs = require 'highlight.js'
marked = require 'marked'
path = require 'path'
url = require 'url'
yaml = require 'js-yaml'

hljs.configure {classPrefix: ''} # keep compatibility with old stylesheets (pre hljs 8.0.0)

# monkeypatch to add url resolving to marked
if not marked.InlineLexer.prototype._outputLink?
  marked.InlineLexer.prototype._outputLink = marked.InlineLexer.prototype.outputLink
  marked.InlineLexer.prototype._resolveLink = (href) -> href
  marked.InlineLexer.prototype.outputLink = (cap, link) ->
    link.href = @_resolveLink link.href
    return @_outputLink cap, link

resolveLink = (content, uri, baseUrl) ->
  ### Resolve *uri* relative to *content*, resolves using
      *baseUrl* if no matching content is found. ###
  uriParts = url.parse uri
  if uriParts.protocol
    # absolute uri
    return uri
  else
    # search pathname in content tree relative to *content*
    nav = content.parent
    path = uriParts.pathname?.split('/') or []
    while path.length and nav?
      part = path.shift()
      if part == ''
        # uri begins with / go to contents root
        nav = nav.parent while nav.parent
      else if part == '..'
        nav = nav.parent
      else
        nav = nav[part]
    if nav?.getUrl?
      return nav.getUrl() + [uriParts.hash]
    return url.resolve baseUrl, uri

parseMarkdownSync = (content, markdown, baseUrl, options) ->
  ### Parse *markdown* found on *content* node of contents and
  resolve links by navigating in the content tree. use *baseUrl* as a last resort
  returns html. ###

  marked.InlineLexer.prototype._resolveLink = (uri) ->
    resolveLink content, uri, baseUrl

  options.highlight = (code, lang) ->
    try
      if lang is 'auto'
        return hljs.highlightAuto(code).value
      else if hljs.getLanguage lang
        return hljs.highlight(lang, code).value
    catch error
      return code

  marked.setOptions options
  return marked markdown

module.exports = (env, callback) ->

  class MarkdownPage extends env.plugins.Page

    constructor: (@filepath, @metadata, @markdown) ->

    getLocation: (base) ->
      uri = @getUrl base
      return uri[0..uri.lastIndexOf('/')]

    getHtml: (base=env.config.baseUrl) ->
      ### parse @markdown and return html. also resolves any relative urls to absolute ones ###
      options = env.config.markdown or {}
      return parseMarkdownSync this, @markdown, @getLocation(base), options

{'fraction_non_alphanumeric': 0.10763052208835341, 'fraction_numerical': 0.001606425702811245, 'mean_word_length': 3.147377185678601, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5909405', 'n_tokens_mistral': 1509, 'n_tokens_neox': 1461, 'n_words': 475}