|
1 | 1 | use std::collections::HashMap; |
2 | 2 |
|
3 | 3 | use anyhow::{bail, Result}; |
4 | | -use tokio::{runtime::Handle, task}; |
5 | 4 |
|
6 | 5 | mod author; |
7 | 6 | mod callout; |
@@ -46,48 +45,44 @@ impl<'a> Fenced<'a> { |
46 | 45 | /// otherwise return URL preview error HTML string to remind user we have error. |
47 | 46 | /// |
48 | 47 | /// If the fenced is unsupported, we simply return `None`. |
49 | | - pub fn render_code_block(self, block: &'a str) -> Option<String> { |
| 48 | + #[tokio::main(flavor = "current_thread")] |
| 49 | + pub async fn render_code_block(self, block: &'a str) -> Option<String> { |
50 | 50 | match self.name { |
51 | 51 | URL_PREVIEW => { |
52 | 52 | let url = block.trim(); |
53 | 53 |
|
54 | | - // Block in place to execute async task |
55 | | - task::block_in_place(|| { |
56 | | - Handle::current().block_on(async { |
57 | | - let (first_preview, mut rx) = { |
58 | | - // parking_lot RwLock guard isn't async-aware, |
59 | | - // we should keep this guard drop in this scope. |
60 | | - let data = data::read(); |
61 | | - if let Some(info) = data.get_preview(url) { |
62 | | - let html = UrlPreviewBlock::new(self.options, url, info) |
63 | | - .render() |
64 | | - .unwrap(); |
65 | | - return Some(html); |
66 | | - } |
67 | | - |
68 | | - data.preview_url(url) |
69 | | - }; |
70 | | - rx.changed() |
71 | | - .await |
72 | | - .expect("URL preview watch channel receive failed."); |
73 | | - let event = rx.borrow(); |
74 | | - match event.to_owned().expect("Url preview didn't initialized.") { |
75 | | - PreviewEvent::Finished(info) => { |
76 | | - let html = UrlPreviewBlock::new(self.options, url, info) |
77 | | - .render() |
78 | | - .unwrap(); |
79 | | - if first_preview { |
80 | | - println!("URL previewed: {url}"); |
81 | | - } |
82 | | - Some(html) |
83 | | - } |
84 | | - PreviewEvent::Failed(err) => { |
85 | | - // Return a preview error block. |
86 | | - Some(UrlPreviewError(url, &err).render().unwrap()) |
87 | | - } |
| 54 | + let (first_preview, mut rx) = { |
| 55 | + // parking_lot RwLock guard isn't async-aware, |
| 56 | + // we should keep this guard drop in this scope. |
| 57 | + let data = data::read(); |
| 58 | + if let Some(info) = data.get_preview(url) { |
| 59 | + let html = UrlPreviewBlock::new(self.options, url, info) |
| 60 | + .render() |
| 61 | + .unwrap(); |
| 62 | + return Some(html); |
| 63 | + } |
| 64 | + |
| 65 | + data.preview_url(url) |
| 66 | + }; |
| 67 | + rx.changed() |
| 68 | + .await |
| 69 | + .expect("URL preview watch channel receive failed."); |
| 70 | + let event = rx.borrow(); |
| 71 | + match event.to_owned().expect("Url preview didn't initialized.") { |
| 72 | + PreviewEvent::Finished(info) => { |
| 73 | + let html = UrlPreviewBlock::new(self.options, url, info) |
| 74 | + .render() |
| 75 | + .unwrap(); |
| 76 | + if first_preview { |
| 77 | + println!("URL previewed: {url}"); |
88 | 78 | } |
89 | | - }) |
90 | | - }) |
| 79 | + Some(html) |
| 80 | + } |
| 81 | + PreviewEvent::Failed(err) => { |
| 82 | + // Return a preview error block. |
| 83 | + Some(UrlPreviewError(url, &err).render().unwrap()) |
| 84 | + } |
| 85 | + } |
91 | 86 | } |
92 | 87 | CALLOUT => { |
93 | 88 | let html = CalloutBlock::new(self.options, block).render().unwrap(); |
|
0 commit comments