Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SanityImageAsset: Alternative text + include in SanityImageAsset reference object #68

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions src/Sanity.Linq/BlockContent/SanityHtmlSerializers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ public Task<string> SerializeDefaultBlockAsync(JToken input, SanityOptions sanit
var listItemEnd = new StringBuilder();

var text2 = new StringBuilder();


// get style
var tag = "";
if (input["style"]?.ToString() == "normal")
{
tag = "p";
}
else
else
{
//default to span
tag = input["style"]?.ToString() ?? "span";
Expand Down Expand Up @@ -99,7 +99,7 @@ public Task<string> SerializeDefaultBlockAsync(JToken input, SanityOptions sanit
foreach (var mark in child["marks"])
{
var sMark = mark?.ToString();
var markDef = markDefs?.FirstOrDefault(m => m["_key"]?.ToString() == sMark);
var markDef = markDefs?.FirstOrDefault(m => m["_key"]?.ToString() == sMark);
if (markDef != null)
{
if (TrySerializeMarkDef(markDef, context, ref start, ref end))
Expand Down Expand Up @@ -144,6 +144,8 @@ public Task<string> SerializeImageAsync(JToken input, SanityOptions options)
{
var asset = input["asset"];
var imageRef = asset?["_ref"]?.ToString();
var assetValue = asset?["value"];
var imageAltText = assetValue?["altText"];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps ?? "" should be added to the end of this line to avoid imageAltText being null. Alternatively the alt tag should only be added if altImageText isn't null on the line further down.


if (asset == null || imageRef == null)
{
Expand All @@ -168,18 +170,15 @@ public Task<string> SerializeImageAsync(JToken input, SanityOptions options)
url.Append(imageParts[2] + "."); // dimensions.
url.Append(imageParts[3]); // file extension
url.Append(parameters.ToString()); // ?crop etc..
return Task.FromResult($"<figure><img src=\"{url}\"/></figure>");

return Task.FromResult($"<figure><img src=\"{url}\" alt=\"{imageAltText}\"/></figure>");
}
public Task<string> SerializeTableAsync(JToken input, SanityOptions options)
{
var html = "";



return Task.FromResult(html);
}

protected virtual bool TrySerializeMarkDef(JToken markDef, object context, ref StringBuilder start, ref StringBuilder end) => false;
}
}
}
3 changes: 2 additions & 1 deletion src/Sanity.Linq/CommonTypes/SanityBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ public SanityBlock() : base()

public object[] Children { get; set; } = new object[] { };

[Include]
public SanityReference<SanityImageAsset> Asset { get; set; } = new SanityReference<SanityImageAsset> { };

public int? Level { get; set; }

public string ListItem { get; set; }
}
}
}
3 changes: 2 additions & 1 deletion src/Sanity.Linq/CommonTypes/SanityImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ public SanityImage() : base()
SanityType = "image";
}

[Include]
public SanityReference<SanityImageAsset> Asset { get; set; }

public SanityImageCrop Crop { get; set; }

public SanityImageHotspot Hotspot { get; set; }
}
}
}
5 changes: 4 additions & 1 deletion src/Sanity.Linq/CommonTypes/SanityImageAsset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,14 @@ public SanityImageAsset() : base()
{
Type = "sanity.imageAsset";
}

[JsonProperty("altText")]
public string AltText { get; set; }
}

public class SanityImageAssetReference : SanityImageAsset
{
[JsonProperty("_ref")]
public string Ref { get; set; }
}
}
}
14 changes: 12 additions & 2 deletions src/Sanity.Linq/JsonConverters/SanityReferenceTypeConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public override void WriteJson(JsonWriter writer, object value, JsonSerializer s

//Get reference from object
var valRef = type.GetProperty("Ref").GetValue(value) as string;
object Value = null;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bit picky here, but we should use camelCase instead of PascalCase for local variable names.


// Alternatively, get reference from Id on nested Value
if (string.IsNullOrEmpty(valRef))
Expand All @@ -82,20 +83,29 @@ public override void WriteJson(JsonWriter writer, object value, JsonSerializer s
}
}
}
else
{
var propValue = type.GetProperty("Value");
if (propValue != null)
{
Value = propValue.GetValue(value);
}
}

// Get _key property (required for arrays in sanity editor)
var keyProp = type.GetProperties().FirstOrDefault(p => p.Name.ToLower() == "_key" || ((p.GetCustomAttributes(typeof(JsonPropertyAttribute), true).FirstOrDefault() as JsonPropertyAttribute)?.PropertyName?.Equals("_key")).GetValueOrDefault());
var weakProp = type.GetProperties().FirstOrDefault(p => p.Name.ToLower() == "_weak" || ((p.GetCustomAttributes(typeof(JsonPropertyAttribute), true).FirstOrDefault() as JsonPropertyAttribute)?.PropertyName?.Equals("_weak")).GetValueOrDefault());
var valKey = keyProp?.GetValue(value) as string ?? Guid.NewGuid().ToString();
var valWeak = weakProp?.GetValue(value) as bool? ?? null;


if (!string.IsNullOrEmpty(valRef))
{
serializer.Serialize(writer, new { _ref = valRef, _type = "reference", _key = valKey, _weak = valWeak });
serializer.Serialize(writer, new { _ref = valRef, _type = "reference", _key = valKey, _weak = valWeak, value = Value });
return;
}
}
serializer.Serialize(writer, null);
}
}
}
}