Skip to content

Commit

Permalink
use a custom flowconfig when generating the site
Browse files Browse the repository at this point in the history
Summary:
the new website plugin removed a feature where we generate a temporary
flow root and .flowconfig, so that the snippets run in an isolated space.
without this, flow looks for a .flowconfig in the current working directory,
whatever that happens to be.

this diff adds back the old code and changes the plugin into a custom markdown
processor plugin instead of monkeypatching the builtin markdown converter
itself.

Reviewed By: gabelevi

Differential Revision: D4838999

fbshipit-source-id: 30a7e64ef4374e9c668ee6198403d29be913d3f7
  • Loading branch information
mroch authored and facebook-github-bot committed Apr 11, 2017
1 parent f20d45f commit 9962167
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 14 deletions.
5 changes: 5 additions & 0 deletions website/_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ repository: facebook/flow

permalink: /blog/:categories/:year/:month/:day/:title/

markdown: FlowMarkdown

flow:
path: 'flow'

Expand Down Expand Up @@ -49,6 +51,9 @@ exclude:
- .asset-cache
- yarn.lock

keep_files:
- static/master

assets:
sources:
- _assets/css
Expand Down
60 changes: 46 additions & 14 deletions website/_plugins/highlighter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,19 @@

module Jekyll
module Converters
class Markdown < Converter
alias old_convert convert
class Markdown::FlowMarkdown
class << self
attr_accessor :tempdir
end

def initialize(config)
super(config)
@config = config
@flow_cache = {}
@markdown = Jekyll::Converters::Markdown::KramdownParser.new config
end

def convert(content)
old_convert(render(content))
@markdown.convert(render(content))
end

def render(content)
Expand Down Expand Up @@ -55,12 +58,20 @@ def match_pragma(content)
content.match(/^(\/\/ *@flow|\/\*\s*@flow)/)
end

def run_flow(command, stdin, success_codes)
out, err, status = Open3.capture3(
"#{@config['flow']['path'] || 'flow'} #{command}",
:stdin_data => stdin
)
if status.exited? and success_codes.include?(status.exitstatus)
response = JSON.parse(out)
else
raise "flow ast failed (exit #{status.exitstatus}): #{err}"
end
end

def get_tokens(content)
response = Open3.popen3("flow ast --tokens") {|stdin, stdout|
stdin.puts(content)
stdin.close
JSON.parse(stdout.gets)
}
response = run_flow("ast --tokens", content, [0])
response['comments'].each do |comment|
normalize_comment comment
end
Expand All @@ -82,11 +93,11 @@ def normalize_comment(comment)

def get_errors(content)
return [] unless match_pragma(content)
response = Open3.popen3("flow check-contents --json") {|stdin, stdout|
stdin.puts(content)
stdin.close
JSON.parse(stdout.gets)
}
response = run_flow(
"check-contents --json --root #{self.class.tempdir}",
content,
[0, 2]
)
response['errors']
end

Expand Down Expand Up @@ -250,4 +261,25 @@ def print_hover_errors(errors)
end
end
end

Hooks.register :site, :pre_render do |site|
tempdir = Dir.mktmpdir
Converters::Markdown::FlowMarkdown.tempdir = tempdir
File.open(File.join(tempdir, '.flowconfig'), 'w') {|f|
f.write(
<<-EOF.gsub(/^ {10}/, '')
[options]
suppress_comment=\\\\(.\\\\|\\n\\\\)*\\\\$DocIssue
max_header_tokens=1
EOF
)
}
end

Hooks.register :site, :post_render do |site|
tempdir = Converters::Markdown::FlowMarkdown.tempdir
flow = site.config['flow']['path'] || 'flow'
`#{flow} stop #{tempdir}`
FileUtils.remove_entry_secure tempdir
end
end

0 comments on commit 9962167

Please sign in to comment.