Skip to content

Commit a2b22c1

Browse files
GearsDatapackslpil
authored andcommitted
Fix search data generation
1 parent 60e36fe commit a2b22c1

16 files changed

+63
-28
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,3 +428,7 @@
428428
- The compiler now provides a clearer error message when a function's return type
429429
is mistakenly declared using `:` instead of `->`.
430430
([Gurvir Singh](https://github.com/baraich))
431+
432+
- Fixed a bug where the data generated for searching documentation was in the
433+
wrong format, preventing it from being used by Hexdocs search.
434+
([Surya Rose](https://github.com/GearsDatapacks))

compiler-core/src/docs.rs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -223,12 +223,14 @@ pub fn generate_html<IO: FileSystemReader>(
223223
let arguments = constructor
224224
.arguments
225225
.iter()
226-
.map(|argument| format!("{}\n{}", argument.name, argument.doc))
226+
.map(|argument| {
227+
format!("{}\n{}", argument.name, argument.text_documentation)
228+
})
227229
.join("\n");
228230

229231
format!(
230232
"{}\n{}\n{}",
231-
constructor.definition, constructor.text_documentation, arguments
233+
constructor.raw_definition, constructor.text_documentation, arguments
232234
)
233235
})
234236
.join("\n");
@@ -239,26 +241,26 @@ pub fn generate_html<IO: FileSystemReader>(
239241
title: type_.name.to_string(),
240242
content: format!(
241243
"{}\n{}\n{}\n{}",
242-
type_.definition,
244+
type_.raw_definition,
243245
type_.text_documentation,
244246
constructors,
245247
import_synonyms(&module.name, type_.name)
246248
),
247249
reference: format!("{}.html#{}", module.name, type_.name),
248250
})
249251
});
250-
values.iter().for_each(|constant| {
252+
values.iter().for_each(|value| {
251253
search_items.push(SearchItem {
252254
type_: SearchItemType::Value,
253255
parent_title: module.name.to_string(),
254-
title: constant.name.to_string(),
256+
title: value.name.to_string(),
255257
content: format!(
256258
"{}\n{}\n{}",
257-
constant.definition,
258-
constant.text_documentation,
259-
import_synonyms(&module.name, constant.name)
259+
value.raw_definition,
260+
value.text_documentation,
261+
import_synonyms(&module.name, value.name)
260262
),
261-
reference: format!("{}.html#{}", module.name, constant.name),
263+
reference: format!("{}.html#{}", module.name, value.name),
262264
})
263265
});
264266

@@ -368,7 +370,7 @@ pub fn generate_html<IO: FileSystemReader>(
368370
),
369371
});
370372

371-
// lunr.min.js, search_data.json and index.js
373+
// lunr.min.js, search-data.json and index.js
372374

373375
files.push(OutputFile {
374376
path: Utf8PathBuf::from("js/lunr.min.js"),
@@ -382,7 +384,7 @@ pub fn generate_html<IO: FileSystemReader>(
382384
.expect("search index serialization");
383385

384386
files.push(OutputFile {
385-
path: Utf8PathBuf::from("search_data.json"),
387+
path: Utf8PathBuf::from("search-data.json"),
386388
content: Content::Text(search_data_json.to_string()),
387389
});
388390

@@ -599,6 +601,7 @@ struct Link {
599601
#[derive(PartialEq, Eq, PartialOrd, Ord, Debug)]
600602
struct TypeConstructor {
601603
definition: String,
604+
raw_definition: String,
602605
documentation: String,
603606
text_documentation: String,
604607
arguments: Vec<TypeConstructorArg>,
@@ -608,12 +611,14 @@ struct TypeConstructor {
608611
struct TypeConstructorArg {
609612
name: String,
610613
doc: String,
614+
text_documentation: String,
611615
}
612616

613617
#[derive(PartialEq, Eq, PartialOrd, Ord, Debug)]
614618
struct TypeDefinition<'a> {
615619
name: &'a str,
616620
definition: String,
621+
raw_definition: String,
617622
documentation: String,
618623
constructors: Vec<TypeConstructor>,
619624
text_documentation: String,
@@ -626,6 +631,7 @@ struct TypeDefinition<'a> {
626631
struct DocsValues<'a> {
627632
name: &'a str,
628633
definition: String,
634+
raw_definition: String,
629635
documentation: String,
630636
text_documentation: String,
631637
source_url: String,

compiler-core/src/docs/printer.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ impl Printer<'_> {
111111
type_definitions.push(TypeDefinition {
112112
name,
113113
definition: print(self.custom_type(name, parameters, constructors, *opaque)),
114+
raw_definition: self
115+
.raw(|this| this.custom_type(name, parameters, constructors, *opaque)),
114116
documentation: markdown_documentation(documentation),
115117
text_documentation: text_documentation(documentation),
116118
deprecation_message: match deprecation {
@@ -124,6 +126,7 @@ impl Printer<'_> {
124126
.iter()
125127
.map(|constructor| TypeConstructor {
126128
definition: print(self.record_constructor(constructor)),
129+
raw_definition: self.raw(|this| this.record_constructor(constructor)),
127130
documentation: markdown_documentation(&constructor.documentation),
128131
text_documentation: text_documentation(&constructor.documentation),
129132
arguments: constructor
@@ -133,6 +136,7 @@ impl Printer<'_> {
133136
.map(|(argument, label)| TypeConstructorArg {
134137
name: label.trim_end().to_string(),
135138
doc: markdown_documentation(&argument.doc),
139+
text_documentation: text_documentation(&argument.doc),
136140
})
137141
.filter(|arg| !arg.doc.is_empty())
138142
.collect(),
@@ -161,6 +165,7 @@ impl Printer<'_> {
161165
type_definitions.push(TypeDefinition {
162166
name,
163167
definition: print(self.type_alias(name, type_, parameters).group()),
168+
raw_definition: self.raw(|this| this.type_alias(name, type_, parameters).group()),
164169
documentation: markdown_documentation(documentation),
165170
text_documentation: text_documentation(documentation),
166171
constructors: vec![],
@@ -177,6 +182,23 @@ impl Printer<'_> {
177182
type_definitions
178183
}
179184

185+
/// Print a definition without HTML highlighting, such as for search data
186+
fn raw<'a, F>(&mut self, definition: F) -> String
187+
where
188+
F: FnOnce(&mut Self) -> Document<'a>,
189+
{
190+
let options = self.options;
191+
// Turn off highlighting for this definition
192+
self.options = PrintOptions {
193+
print_highlighting: false,
194+
print_html: false,
195+
};
196+
let result = print(definition(self));
197+
// Restore previous options
198+
self.options = options;
199+
format!("```\n{result}\n```")
200+
}
201+
180202
pub fn value_definitions<'a>(
181203
&mut self,
182204
source_links: &SourceLinker,
@@ -209,6 +231,8 @@ impl Printer<'_> {
209231
value_definitions.push(DocsValues {
210232
name,
211233
definition: print(self.function_signature(name, arguments, return_type)),
234+
raw_definition: self
235+
.raw(|this| this.function_signature(name, arguments, return_type)),
212236
documentation: markdown_documentation(documentation),
213237
text_documentation: text_documentation(documentation),
214238
source_url: source_links.url(*location),
@@ -236,6 +260,7 @@ impl Printer<'_> {
236260
value_definitions.push(DocsValues {
237261
name,
238262
definition: print(self.constant(name, type_)),
263+
raw_definition: self.raw(|this| this.constant(name, type_)),
239264
documentation: markdown_documentation(documentation),
240265
text_documentation: text_documentation(documentation),
241266
source_url: source_links.url(*location),

compiler-core/src/docs/snapshots/gleam_core__docs__tests__canonical_link.snap

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ expression: "compile_with_markdown_pages(config, modules, pages,\nCompileWithMar
320320
<script src="./js/lunr.min.js?v=GLEAM_VERSION_HERE"></script>
321321
<script src="./js/index.js?v=0"></script>
322322
<script>
323-
fetch("./search_data.json?v=0")
323+
fetch("./search-data.json?v=0")
324324
.then(response => response.json())
325325
.then(data => window.Gleam.initSearch(data));
326326
</script>
@@ -687,7 +687,7 @@ expression: "compile_with_markdown_pages(config, modules, pages,\nCompileWithMar
687687
<script src="./js/lunr.min.js?v=GLEAM_VERSION_HERE"></script>
688688
<script src="./js/index.js?v=0"></script>
689689
<script>
690-
fetch("./search_data.json?v=0")
690+
fetch("./search-data.json?v=0")
691691
.then(response => response.json())
692692
.then(data => window.Gleam.initSearch(data));
693693
</script>
@@ -1054,7 +1054,7 @@ expression: "compile_with_markdown_pages(config, modules, pages,\nCompileWithMar
10541054
<script src="../../js/lunr.min.js?v=GLEAM_VERSION_HERE"></script>
10551055
<script src="../../js/index.js?v=0"></script>
10561056
<script>
1057-
fetch("../../search_data.json?v=0")
1057+
fetch("../../search-data.json?v=0")
10581058
.then(response => response.json())
10591059
.then(data => window.Gleam.initSearch(data));
10601060
</script>

compiler-core/src/docs/snapshots/gleam_core__docs__tests__discarded_arguments_are_not_shown.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ expression: "compile(config, modules)"
352352
<script src="./js/lunr.min.js?v=GLEAM_VERSION_HERE"></script>
353353
<script src="./js/index.js?v=0"></script>
354354
<script>
355-
fetch("./search_data.json?v=0")
355+
fetch("./search-data.json?v=0")
356356
.then(response => response.json())
357357
.then(data => window.Gleam.initSearch(data));
358358
</script>

compiler-core/src/docs/snapshots/gleam_core__docs__tests__docs_of_a_type_constructor_are_not_used_by_the_following_function.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ expression: "compile(config, modules)"
423423
<script src="./js/lunr.min.js?v=GLEAM_VERSION_HERE"></script>
424424
<script src="./js/index.js?v=0"></script>
425425
<script>
426-
fetch("./search_data.json?v=0")
426+
fetch("./search-data.json?v=0")
427427
.then(response => response.json())
428428
.then(data => window.Gleam.initSearch(data));
429429
</script>

compiler-core/src/docs/snapshots/gleam_core__docs__tests__hello_docs.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ expression: "compile(config, modules)"
353353
<script src="./js/lunr.min.js?v=GLEAM_VERSION_HERE"></script>
354354
<script src="./js/index.js?v=0"></script>
355355
<script>
356-
fetch("./search_data.json?v=0")
356+
fetch("./search-data.json?v=0")
357357
.then(response => response.json())
358358
.then(data => window.Gleam.initSearch(data));
359359
</script>

compiler-core/src/docs/snapshots/gleam_core__docs__tests__ignored_argument_is_called_arg.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ expression: "compile(config, modules)"
352352
<script src="./js/lunr.min.js?v=GLEAM_VERSION_HERE"></script>
353353
<script src="./js/index.js?v=0"></script>
354354
<script>
355-
fetch("./search_data.json?v=0")
355+
fetch("./search-data.json?v=0")
356356
.then(response => response.json())
357357
.then(data => window.Gleam.initSearch(data));
358358
</script>

compiler-core/src/docs/snapshots/gleam_core__docs__tests__internal_definitions_are_not_included.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ expression: "compile(config, modules)"
322322
<script src="./js/lunr.min.js?v=GLEAM_VERSION_HERE"></script>
323323
<script src="./js/index.js?v=0"></script>
324324
<script>
325-
fetch("./search_data.json?v=0")
325+
fetch("./search-data.json?v=0")
326326
.then(response => response.json())
327327
.then(data => window.Gleam.initSearch(data));
328328
</script>

compiler-core/src/docs/snapshots/gleam_core__docs__tests__long_function_wrapping.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ function for a fallback value.</p>
426426
<script src="./js/lunr.min.js?v=GLEAM_VERSION_HERE"></script>
427427
<script src="./js/index.js?v=0"></script>
428428
<script>
429-
fetch("./search_data.json?v=0")
429+
fetch("./search-data.json?v=0")
430430
.then(response => response.json())
431431
.then(data => window.Gleam.initSearch(data));
432432
</script>

0 commit comments

Comments
 (0)