Might be able to take some inspiration from this JavaScript library: https://github.com/concordancejs/concordance. Worth a mention in the README, anyway.