Prepare navigation before building sites

I have a set of (non .yml) data files, which I’m traversing using the proxy command in config.rb. Now my problem is, I have to open each file once to obtain some information from it’s contents, in order to build the navigation, which is then included in each of the proxied files. Therefore, I end up with a N² problem, what could in reality be a N problem.

What is the best way to generate the navigation?
Can I generate a partial first, which is then included in each page that corresponds to a data file?

Could I use something like
configure :pre_build do
in config.rb?

To me, it would be most elegant, if I could generate this information in an array of hashes before build and then just use it. Although also saving it as intermediate .yml or .json file would be fine, I guess.

Thank you for any hint!